JavaCode(review)
Immutable class // Location Class- adventure game 이용
◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e
2020. 7. 7. 04:55
반응형
개념
https://codedragon.tistory.com/m/7930
https://limkydev.tistory.com/m/68
inner code can't be changed once they are created.
To avoid external modification. // for encapsulation and reducing errors.
private/ final field
setter X
variables는 constructor에
HashMap(Key,value) 이용한 경우
Location Class
package com.timbuchalka;
import java.util.HashMap;
import java.util.Map;
public class Location {
private final int locationNum; //private final modifier aa; V
private final String description;
private final Map<String, Integer> exits;
// V
public Location(int locationNum, String description, Map<String, Integer> exits) {
this.locationNum = locationNum;
this.description = description;
this.exits = new HashMap<String, Integer>(exits); //add method(addExit)대신 Map.put이용하려고.
this.exits.put("Q", 0);
}
// public void addExit(String direction, int location) {
// exits.put(direction, location);
// }
public int getLocationNum() {
return locationNum;
}
public String getDescription() {
return description;
}
public Map<String, Integer> getExits() {
return new HashMap<String, Integer>(exits);
}
}
Main
package com.timbuchalka;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
private static Map<Integer, Location> locations = new HashMap<Integer, Location>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//temExit(map.put)으로 값설정후, call constructor으로 tempExit 실행케 코딩 V
//값설정 +방향 설정
Map<String, Integer> tempExit = new HashMap<String, Integer>();
locations.put(0, new Location(0, "You are sitting in front of a computer learning Java",tempExit));
tempExit = new HashMap<String, Integer>(); // basic 방향공간 define
tempExit.put("W", 2);
tempExit.put("E", 3);
tempExit.put("S", 4);
tempExit.put("N", 5);
locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building",tempExit));
tempExit = new HashMap<String, Integer>();
tempExit.put("N", 5);
locations.put(2, new Location(2, "You are at the top of a hill",tempExit));
tempExit = new HashMap<String, Integer>();
tempExit.put("W", 1);
locations.put(3, new Location(3, "You are inside a building, a well house for a small spring",tempExit));
tempExit = new HashMap<String, Integer>();
tempExit.put("N", 1);
tempExit.put("W", 2);
locations.put(4, new Location(4, "You are in a valley beside a stream",tempExit));
tempExit = new HashMap<String, Integer>();
tempExit.put("S", 1);
tempExit.put("W", 2);
locations.put(5, new Location(5, "You are in the forest",tempExit));
//문장을 한글자로 인식하도록 값설정
Map<String, String> vocabulary = new HashMap<String, String>();
vocabulary.put("QUIT", "Q");
vocabulary.put("NORTH", "N");
vocabulary.put("SOUTH", "S");
vocabulary.put("WEST", "W");
vocabulary.put("EAST", "E");
//출력코드
int loc = 1;
while(true) {
System.out.println(locations.get(loc).getDescription());
tempExit.remove("S");
if(loc == 0) {
break;
}
Map<String, Integer> exits = locations.get(loc).getExits();
System.out.print("Available exits are ");
for(String exit: exits.keySet()) {
System.out.print(exit + ", ");
}
System.out.println();
//String console 입력
String direction = scanner.nextLine().toUpperCase();
if(direction.length() > 1) { //입력이 문장이라면
String[] words = direction.split(" ");
for(String word: words) {
if(vocabulary.containsKey(word)) {
direction = vocabulary.get(word);
break;
}
}
}
if(exits.containsKey(direction)) {
loc = exits.get(direction);
} else {
System.out.println("You cannot go in that direction");
}
}
}
}
//Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=50044:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Immutable-Classes-Source-code\out\production\Adventure com.timbuchalka.Main
You are standing at the end of a road before a small brick building
Available exits are Q, S, E, N, W,
1
You cannot go in that direction
You are standing at the end of a road before a small brick building
Available exits are Q, S, E, N, W,
west
You are at the top of a hill
Available exits are Q, N,
q
You are sitting in front of a computer learning Java
Process finished with exit code 0