import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test1 {
static Map<String, String> map ; // 사전 만들기
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
input();
}
//이 안에서만 쓸거면 public을 붙이지 않아도 되긴 한다.
public static void input() {
System.out.println("1. insert 2. delete 3. update");
System.out.println("4. find 5. exit");
String line = sc.nextLine();
Integer i = Integer.parseInt(sc.nextLine());
// if (i == 1) insert(); // i 가 1 이면 인설트를 출력하겠다.
// else if (i ==2) delete(); // 2 이면 딜리트로 가겠다.
switch(i){
case 1:
insert(); break;
case 2:
delete(); break;
case 3:
update(); break;
case 4:
find(); break;
case 5:
break;
default:
input(); break;
}
}
public static void insert() { // 위에가 static이기 때문에 여기에 static없이 쓸 수 없다.
System.out.println("영단어 입력");
String key = sc.nextLine();
System.out.println("한국어 입력");
String value = sc.nextLine();
map.put(key, value);
}
public static void delete() {
}
public static void update() {
}
public static void find() {
}
}
if나 switch 둘중하나 쓰면 된다. 같은 것이다.
여기까진 뼈대를 만든 것.
Q. 이제 find를 만들어 봐라.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test1 {
static Map<String, String> map = new HashMap<>(); // 사전 만들기
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
input();
}
public static void input() {
System.out.println("1. insert 2. delete 3. update");
System.out.println("4. find 5. exit");
Integer i = Integer.parseInt(sc.nextLine());
switch(i){
case 1:
insert(); break;
case 2:
delete(); break;
case 3:
update(); break;
case 4:
find(); break;
case 5:
break;
default:
input(); break;
}
}
public static void insert() {
System.out.println("영단어 입력");
String key = sc.nextLine();
System.out.println("한국어 입력");
String value = sc.nextLine();
map.put(key, value);
input();
}
public static void delete() {
// 키를 보여주고
// 키를 입력하고 그거(value) 삭제
}
public static void update() {
// 키를 보여주고
// 키를 입력하고 그거(value) 업데이트
}
public static void find() {
// 키를 보여주고
// 키를 입력하고 그거(value) 찾기
System.out.println("키 입력");
String key = sc.nextLine();
if (map.containsKey(key))
System.out.println("key : " + key + ", value : " + map.get(key));
else
System.out.println("없는 단어");
input();
}
}
사전만들기니까
시스템을 돌리면 먼저 1번을 입력해서 만들 단어를 입력한다. 그리고 다시 인풋으로 돌아가게 해놨으니 입력값을 받는 화면이 뜬다.
그 후 4를 입력하면 키를 입력하라고 나온다. 여기에 내가 사전에 만든 바나나를 입력하면
어떤걸 입력했는지(key) 그래서 찾은게 뭔지(value)를 알려준다.
이렇게해서 delete, update를 만들 수 있다.
-> delete는 1번으로 가서 사전에 등록할 단어를 만들고 이걸 다시 지우고 싶을 때 2번을 가서 입력하면 그 단어가 지워질 수 있게 만들면 된다.
->update는 만들어 놓은 단어가 "바나나" 이라면 이를 "노란 바나나"로 바꾸고 싶을 때
1번에서 만들고 3번에서 다시 덮는 키를 친 수 4번에서 찾으면 "노란 바나나"가 출력되도록 하는 방식이다.
switch문을 main 밖에서도 쓰고 싶어서
아래와 같이 main에서 빼내 main 밖 맨 아래에로 옮겨주었다.
public static void modeSelect(int mode){
switch(mode){
case 1:
insert(); break;
case 2:
delete(); break;
case 3:
update(); break;
case 4:
find(); break;
case 5:
break;
default:
input(); break;
}
}
}
-> 이렇게 사용하기 위해 switch문을 밖으로 뺀 것이다.
public static void delete() {
// 키를 보여주고
// 키를 입력하고 그거(value) 삭제
String key = sc.nextLine();
if (map.containsKey(key))
map.remove(key);
else
System.out.println("키가 없어요");
input();
}
``
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test1 {
static Map<String, String> map = new HashMap<>(); // 사전 만들기
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
input();
}
public static void input() {
System.out.println("1. insert 2. delete 3. update");
System.out.println("4. find 5. exit");
Integer i = Integer.parseInt(sc.nextLine());
modeSelect(i);
}
public static void insert() {
System.out.println("영단어 입력");
String key = sc.nextLine();
System.out.println("한국어 입력");
String value = sc.nextLine();
map.put(key, value);
}
public static void delete() {
// 키를 보여주고
// 키를 입력하고 그거(value) 삭제
String key = sc.nextLine();
if (isValid(key))
map.remove(key);
else
System.out.println("키가 없어요");
input();
}
public static void update() {
// 키를 보여주고
// 키를 입력하고 그거(value) 업데이트
System.out.println("업데이트 할 단어 입력");
}
public static void find() {
// 키를 보여주고
// 키를 입력하고 그거(value) 찾기
System.out.println("키 입력");
String key = sc.nextLine();
if (isValid(key))
System.out.println("key : " + key + ", value : " + map.get(key));
input();
}
public static boolean isValid(String key) {
if (map.containsKey(key))
return true;
System.out.println("없는 단어");
return false;
}
public static void modeSelect(int mode){
switch(mode){
case 1:
insert(); break;
case 2:
delete(); break;
case 3:
update(); break;
case 4:
find(); break;
case 5:
break;
default:
input(); break;
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test1 {
static Map<String, String> map = new HashMap<>(); // 사전 만들기
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
input();
}
public static void input() {
System.out.println("1. insert 2. delete 3. update");
System.out.println("4. find 5. exit");
Integer i = Integer.parseInt(sc.nextLine());
modeSelect(i);
}
public static void insert() {
System.out.println("영단어 입력");
String key = sc.nextLine();
System.out.println("한국어 입력");
String value = sc.nextLine();
map.put(key, value);
}
public static void delete() {
// 키를 보여주고
// 키를 입력하고 그거(value) 삭제
String key = sc.nextLine();
if (isValid(key))
map.remove(key);
else
System.out.println("키가 없어요");
input();
}
public static void update() {
// 키를 보여주고
// 키를 입력하고 그거(value) 업데이트
System.out.println("업데이트 할 단어 입력");
String key = sc.nextLine();
System.out.println("바꿀내용 입력");
String value = sc.nextLine();
if (isValid(key)) map.put(key, value);
input();
}
public static void find() {
// 키를 보여주고
// 키를 입력하고 그거(value) 찾기
System.out.println("키 입력");
String key = sc.nextLine();
if (isValid(key))
System.out.println("key : " + key + ", value : " + map.get(key));
input();
}
public static boolean isValid(String key) {
if (map.containsKey(key))
return true;
System.out.println(key + "로된 단어는 없습니다.");
return false;
}
public static boolean isValid(String key, boolean isInsert) {
if (!(map.containsKey(key))) return true;
System.out.println(key + "는" + map.get(key) + "라고 있는 단어입니다.");
return false;
}
public static void modeSelect(int mode){
switch(mode){
case 1:
insert(); break;
case 2:
delete(); break;
case 3:
update(); break;
case 4:
find(); break;
case 5:
break;
default:
input(); break;
}
}
}