Created
May 19, 2025 02:57
-
-
Save seonho98/f7807094bc6f65927db8f8a0336c2297 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class ListAssignment { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
ArrayList contact = new ArrayList(); | |
String name, num; | |
boolean flag = true; | |
int searchIdx, beforeSize, afterSize; | |
while (flag) { | |
System.out.println("연락처 관리 시스템(add, remove, list, search, exit"); | |
System.out.print("명령 입력 : "); | |
String cmdStr = sc.nextLine(); | |
switch (cmdStr) { | |
case "add": | |
beforeSize = contact.size(); | |
System.out.print("이름 : "); | |
name = sc.nextLine(); | |
contact.add(name); | |
System.out.print("번호 : "); | |
num = sc.nextLine(); | |
contact.add(num); | |
afterSize = contact.size(); | |
if (beforeSize < afterSize) System.out.println("연락처 추가"); | |
else System.out.println("연락처 추가 실패"); | |
break; | |
case "remove": | |
beforeSize = contact.size(); | |
System.out.print("삭제할 이름 : "); | |
name = sc.nextLine(); | |
searchIdx = contact.indexOf(name); | |
contact.remove(searchIdx); | |
contact.remove(searchIdx + 1); | |
afterSize = contact.size(); | |
if(beforeSize>afterSize) System.out.println("연락처 삭제"); | |
else System.out.println("연락처 삭제 실패"); | |
break; | |
case "list": | |
break; | |
case "search": | |
System.out.print("검색할 이름 : "); | |
name = sc.nextLine(); | |
searchIdx = contact.indexOf(name); | |
System.out.println(contact.get(searchIdx) + " - " + contact.get(searchIdx + 1)); | |
break; | |
case "exit": | |
flag = false; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment