import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
class Location{
private String city,latitude,longitude;
public Location(String city, String latitude, String longitude) {
// TODO Auto-generated constructor stub
this.city = city;
this.latitude = latitude;
this.longitude = longitude;
}
public String getCity() {return city;}
public String getLatitude() {return latitude;}
public String getLongitude() {return longitude;}
}
public class ex6 {
private Scanner sc = new Scanner(System.in);
private HashMap<String, Location> hm = new HashMap<String,Location>();
private void input() {
System.out.println("도시,경도,위도를 입력하세요");
for(int i=0;i<4;i++) {
System.out.print(">>");
String str = sc.nextLine();
String strSplit[] = str.split(", ");
String city = strSplit[0];
String latitude = strSplit[1];
String longitude = strSplit[2];
hm.put(city, new Location(city,latitude,longitude));
}
}
private void print() {
Set<String> keys = hm.keySet();
Iterator<String> it = keys.iterator();
System.out.println("--------------------------------");
while(it.hasNext()) {
String key = it.next();
Location value = hm.get(key);
System.out.print(key+"\t"+value.getLatitude()+"\t"+value.getLongitude());
System.out.println();
}
System.out.println("--------------------------------");
}
private void search() {
while(true) {
System.out.print("도시 이름 >>");
String search = sc.next();
if(search.equals("그만")) break;
Location l = hm.get(search);
if(l == null) System.out.println(search+"는 없습니다.");
else {
System.out.println(l.getCity()+"\t"+l.getLatitude()+"\t"+l.getLongitude());
}
}
}
public void run() {
input();
print();
search();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ex6 e = new ex6();
e.run();
}
}
결과
도시,경도,위도를 입력하세요
>>서울, 37, 126
>>LA, 34, -118
>>파리, 2, 48
>>시드니, 151, -33
--------------------------------
서울 37 126
LA 34 -118
파리 2 48
시드니 151 -33
--------------------------------
도시 이름 >>피리
피리는 없습니다.
도시 이름 >>파리
파리 2 48
도시 이름 >>그만
'명품JAVA프로그래밍 > 7장 제너릭과 컬렉션' 카테고리의 다른 글
[명품JAVA프로그래밍] 7장 실습문제 8번 (0) | 2022.02.18 |
---|---|
[명품JAVA프로그래밍] 7장 실습문제 7번 (0) | 2022.02.18 |
[명품JAVA프로그래밍] 7장 실습문제 5-2번 (0) | 2022.02.17 |
[명품JAVA프로그래밍] 7장 실습문제 5-1번 (0) | 2022.02.17 |
[명품JAVA프로그래밍] 7장 실습문제 4번 (0) | 2022.02.17 |