public class MyStorage {
ArrayList<String> productList =
new ArrayList<>(Arrays.asList("등록 가능", "등록 가능", "등록 가능", "등록 가능", "등록 가능"));
ArrayList<Integer> productAmountList =
new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0));
Scanner sc = new Scanner(System.in);
...
제품명 리스트와 제품명의 수량 리스트를 ArrayList로 설정
제품명 리스트는 "등록 가능"으로 초기화, 크기는 5로 설정
제품수량 리스트는 0으로 초기화, 크기는 5로 설정
입력 받는 Scanner 객체를 iv로 설정
: 프로그램 메뉴를 설명해주는 메서드
public static void showMenu() {
System.out.println("=".repeat(25));
System.out.println("1. 물건 정보(제품명) 등록하기");
System.out.println("2. 물건 정보(제품명) 등록 취소하기");
System.out.println("3. 물건 넣기 (제품 입고)");
System.out.println("4. 물건 빼기 (제품 출고)");
System.out.println("5. 재고 조회");
System.out.println("6. 프로그램 종료");
System.out.println("=".repeat(25));
}
: 프로그램 메뉴 선택하는 메서드
public String chooseMenu() {
// 번호 선택을 int 형이 아닌 String 으로 받아서 문자를 잘못 입력해도 에러가 뜨지 않도록 구현
System.out.print("원하는 기능의 번호를 입력하세요. : ");
return sc.nextLine();
}
nextInt()로 했을 때 실수로 문자열을 입력하면 에러가 난다.
: 제품 등록하는 메서드
public void prodInput() {
System.out.println("현재 등록된 제품 목록");
// 제품 목록 출력해주는 향상된 for 문
for (String s : productList) {
System.out.println("> " + s);
}
outer:
while (true) {
System.out.print("등록할 제품명을 입력하세요 : ");
String product = sc.nextLine();
if (product.equals("등록 가능")) { // 제품명을 '등록 가능' 이라고 입력하면 제품명 다시 입력
System.out.println("제품명을 정확히 입력해주세요.");
continue;
}
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).equals("등록 가능")) { // 제품이 등록되면 while 문을 빠져나감
productList.set(i, product);
break outer;
} else if (productList.get(i).equals(product)) { // 이미 등록된 제품이면 for 문을 빠져나가 제품명 다시 입력
System.out.println("이미 등록된 제품입니다. 다시 입력해주세요.");
continue outer;
}
}
// 5가지 제품을 모두 등록해도 추가로 계속 등록할 수 있도록 구현
for (int i = 0; i < productList.size(); i++) {
if (!(productList.get(i).equals("등록 가능"))) {
productList.add(product);
// 제품을 추가할 때 개수도 0으로 초기화
productAmountList.add(0);
break outer;
}
}
}
System.out.println("등록이 완료되었습니다.");
System.out.println("현재 등록된 제품 목록");
for (String s : productList) {
System.out.println("> " + s);
}
}
제품명을 입력할 때 "등록 가능" 이라는 문자열을 입력하면 경고 문구 출력 후 다시 입력하도록 함
제품명이 정상적으로 입력되면 "등록 가능"을 입력한 제품명으로 바꾸고 outer: while 문을 빠져나감. 만약, 입력한 제품명이 이미 있다면 다시 입력하도록 함
제품명 리스트의 크기를 5로 초기화했지만 더 추가할 수 있도록 구현
: 제품 등록 취소하는 메서드
public void prodRemove() {
System.out.println("현재 등록된 제품 목록");
for (String s : productList) {
System.out.println("> " + s);
}
outer:
while (true) {
System.out.print("등록 취소할 제품명을 입력하세요 : ");
String product = sc.nextLine();
if (product.equals("등록 가능")) { // 제품명을 '등록 가능' 이라고 입력하면 제품명 다시 입력
System.out.println("제품명을 정확히 입력해주세요.");
continue;
}
for (int i = 0; i < productList.size(); i++) { // 제품을 등록 취소하면 다시 '등록 가능' 메시지 출력
if (productList.get(i).equals(product)) {
productList.set(i, "등록 가능");
productAmountList.set(i, 0); // 수량도 0으로 초기화
break outer;
}
}
// 입력한 제품명이 productList 에 없으면 경고 문구 출력
for (String s : productList) {
if (!(product.equals(s))) {
System.out.println("제품명을 정확히 입력해주세요.");
continue outer;
}
}
}
System.out.println("등록 취소가 완료되었습니다.");
System.out.println("현재 등록된 제품 목록");
for (String s : productList) {
System.out.println("> " + s);
}
}
입력한 제품명이 "등록 가능" 이거나 제품명이 리스트에 없으면 경고 문구 출력 후 다시 입력하도록 구현
입력한 제품명을 "등록 가능"으로 바꾸고 수량도 0으로 초기화
: 재고 조회하는 메서드
public void prodSearch() {
System.out.println("현재 등록된 제품 및 수량");
for (int i = 0; i < productList.size(); i++) {
System.out.println("> " + productList.get(i) + " :" + productAmountList.get(i) + "개");
}
}
: 제품의 수량을 추가시키는 메서드(입고)
public void prodAmountAdd() {
String product;
prodSearch();
outer:
while (true) {
System.out.print("수량을 추가할 제품을 입력해주세요. : ");
product = sc.nextLine();
// 제품명을 '등록 가능' 이라고 입력했을 때 경고 문구 출력
if (product.equals("등록 가능")) {
System.out.println("제품명을 정확히 입력해주세요.");
continue;
}
// 제품명을 제대로 입력했으면 while 문을 빠져 나온다.
for (String s : productList) {
if (product.equals(s)) break outer;
}
// 제품명을 제대로 입력하지 않았을 때 경고 문구 출력
System.out.println("제품명을 정확히 입력해주세요.");
}
System.out.print("추가할 수량을 입력해주세요. : ");
String amountStr = sc.nextLine();
int amount = Integer.parseInt(amountStr);
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).equals(product)) {
// productList 와 인덱스가 같은 productAmountList 에 입력한 amount 추가
productAmountList.set(i, productAmountList.get(i) + amount);
}
}
prodSearch();
}
제품명을 "등록 가능" 혹은 리스트에 없는 제품명을 입력했을 때 경고 문구 출력
입력한 제품명의 인덱스가 2인 경우, productAmountList
리스트의 인덱스 2에 수량을 입력 받은 수량을 더해준다.
이 메서드를 호출하고 수량 추가가 완료됐을 때 제품명과 수량이 출력되게 prodSearch()
메서드를 처음과 끝에 호출
: 제품의 수량을 감소시키는 메서드(출고)
public void prodAmountDecrease() {
String product, amountStr;
int amount;
prodSearch();
outer:
while (true) {
System.out.print("수량을 추가할 제품을 입력해주세요. : ");
product = sc.nextLine();
// 제품명을 '등록 가능' 이라고 입력했을 때 경고 문구 출력
if (product.equals("등록 가능")) {
System.out.println("제품명을 정확히 입력해주세요.");
continue;
}
// 제품명을 제대로 입력했으면 while 문을 빠져 나온다.
for (String s : productList) {
if (product.equals(s)) break outer;
}
// 제품명을 제대로 입력하지 않았을 때 경고 문구 출력
System.out.println("제품명을 정확히 입력해주세요.");
}
while (true) {
System.out.print("출고할 수량을 입력해주세요. : ");
amountStr = sc.nextLine();
amount = Integer.parseInt(amountStr);
// 입력한 출고할 수량이 입고된 수량보다 많으면 문구 출력하고 수량 다시 입력
if (productAmountList.get(productList.indexOf(product)) < amount) {
System.out.println("입고된 수량보다 많이 출고할 수 없습니다. 출고할 수량을 다시 입력해주세요.");
} else break; // 수량이 잘 입력됐으면 while 문 빠져나감
}
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).equals(product)) {
productAmountList.set(i, productAmountList.get(i) - amount);
}
}
prodSearch();
}
제품명을 "등록 가능" 혹은 리스트에 없는 제품명을 입력했을 때 경고 문구 출력
입력한 수량이 원래 입고되있던 수량보다 많으면 안되는 조건문 추가
이 메서드를 호출하고 수량 추가가 완료됐을 때 제품명과 수량이 출력되게 prodSearch()
메서드를 처음과 끝에 호출
public static void main(String[] args) {
System.out.println("[Item_Storage_V3]");
System.out.println(args[0] + " 점장님 어서오세요.");
MyStorage myStorage = new MyStorage();
while (true) {
showMenu();
char charNum = myStorage.chooseMenu().charAt(0);
switch (charNum) {
case '1':
myStorage.prodInput();
break;
case '2':
myStorage.prodRemove();
break;
case '3':
myStorage.prodAmountAdd();
break;
case '4':
myStorage.prodAmountDecrease();
break;
case '5':
myStorage.prodSearch();
break;
case '6':
System.exit(1);
default:
// 1 ~ 6 을 제외한 숫자뿐 만 아니라 문자를 입력 받아도 경고 문구 출력 후 다시 입력
System.out.println("번호를 잘못 입력하셨습니다. 다시 입력해주세요.");
}
}
}