Backend/JAVA2 멘토시리즈

JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기

쏠솔랄라 2023. 4. 6. 18:53

 

 

알고리즘 짜기

 

: 문제 해결을 위한 절차적인 과정

 

 

ex1.

문제상황 : 배가 고파서 라면이 먹고 싶다

...

문제상황 해결 : 라면을 먹었다

 

문제상황 > 문제상황 해결을 위해 필요한 중간과정

 

->

1. 냄비에 물 올리기

2. 불 켜기

3. 면과 스프 넣기

4. 계란 넣기

5. 불 끄기

6. 접시에 내기

 

 


 

 

Exercise 

 

카페 프로그램
(1) 주문하기
(2) 취소하기
(3) 결제하기
(4) 끝내기

* 1차원배열, 제어문(조건문, 반복문)만 사용해 문제 풀이

 

 

STEP1 메뉴 및 기능 구성

 

(1) 주문하기

주문 가능한 메뉴 출력

주문 받을 메뉴 입력받기

주문한 메뉴의 가격을 총 금액에 누적

주문한 메뉴를 전체 메뉴리스트에 누적

주문한 개수를 한 개 증가

 

(2) 취소하기

주문한 메뉴 리스트 출력

취소한 메뉴 입력 받기

입력 받은 메뉴를 주문리스트에서 제거

입력 받은 메뉴의 금액을 총 금액에서 제거

주문한 개수 한 개 감소

 

(3) 결제하기

결제할 총 금액 출력

사용자에게 지불할 금액 입력 받기

지불한 금액과 총 금액을 비교해서

     만약 지불한 금액 < 총 금액 이면 "금액이 부족합니다" 출력 후 결제 취소

그게 아니라면 잔돈과 함께 계산완료 출력

총 금액은 0원으로 주문리스트를 비워준다

전체 개수를 0개로 초기화한다

 

=> 사용할 개념

무한반복 : 반복이 종료되지 않고 구현

문자열 배열

 

 

STEP2 필요한 변수 지정

 

int cnt=0; // 전체 개수를 저장할 변수

String orderList [ ] = new orderList [5]; // 주문한 메뉴를 저장할 문자열 배열 ; 주문은 5개까지만 받는 것으로 한다

int totPrice=0; // 총 금액을 저장할 변수

 

 

STEP3 메뉴 구성, 사용할 코드 구성

 

while문을 사용해 무한히 반복되는 카페 키오스크 구성

 

1.주문하기

주문할 메뉴 선택

totPrice+=금액; // 선택한 메뉴의 가격을 총 금액에 가산

orderList[cnt] = 주문한 메뉴; // 주문한 메뉴 배열에 담기

cnt++;

 

2.주문취소

취소할 메뉴 선택

totPrice-=금액;

배열에서 취소한 메뉴 삭제

 

3.결제하기

지불할 금액을 입력받아

지불금액이 결제금액보다 크면 잔액을 거슬러주고 종료 -> cnt와 totPrice를 0으로 돌리고 배열에 빈 값을 넣어준다

지불금액이 결제금액보다 작으면 잔액부족

 

4.끝내기

break;

 

 

STEP4 코드작성

import java.util.Scanner;

public class CafeKiosk1 {

public static void main(String[] args) {

 

Scanner sc = new Scanner(System.in);

 

int cnt=0;

String orderList [] = new String [5];

int totPrice=0;

 

while (true) {

System.out.print("============= CAFE ORDER HERE ============="

+ "\n1.주문하기 2.주문취소 3.결제하기 4.끝내기"

+ "\n메뉴 선택: ");

int selectMenu = sc.nextInt();

System.out.println();

 

if (selectMenu==1) {

System.out.print("================ 주문가능 메뉴 ================"

+ "\n1.에스프레소 3000원\n2.아메리카노 3500원\n3.카페라떼 3800원\n4.밀크티 4200원\n5.청귤에이드 4000원"

+ "\n-------------------------------------------"

+ "\n주문할 메뉴 선택: ");

int orderMenu = sc.nextInt();

String menu="";

int menuPrice=0;

 

if (orderMenu==1) {

menu="에스프레소";

menuPrice=3000;

totPrice+=menuPrice;

orderList [cnt] = menu;

cnt++;

System.out.println(menu + "가 주문 목록에 추가되었습니다.");

} else if (orderMenu==2) {

menu="아메리카노";

menuPrice=3500;

totPrice+=menuPrice;

orderList [cnt] = menu;

cnt++;

System.out.println(menu + "가 주문 목록에 추가되었습니다.");

} else if (orderMenu==3) {

menu="카페라떼";

menuPrice=3800;

totPrice+=menuPrice;

orderList [cnt] = menu;

cnt++;

System.out.println(menu + "가 주문 목록에 추가되었습니다.");

} else if (orderMenu==4) {

menu="밀크티";

menuPrice=4200;

totPrice+=menuPrice;

orderList [cnt] = menu;

cnt++;

System.out.println(menu + "가 주문 목록에 추가되었습니다.");

} else if (orderMenu==5) {

menu="청귤에이드";

menuPrice=4000;

totPrice+=menuPrice;

orderList [cnt] = menu;

cnt++;

System.out.println(menu + "가 주문 목록에 추가되었습니다.");

} else {

System.out.println("잘못된 입력입니다.");

}

System.out.println();

 

} else if (selectMenu==2) {

System.out.println("================== 주문 목록 ==================");

 

for (int i=0; i<cnt; i++) {

System.out.println("["+ (i+1) + "]" + orderList[i]);

}

System.out.print("-------------------------------------------"

+ "\n취소할 메뉴 번호 : ");

int cancelMenu = sc.nextInt();

String deleteMenu = orderList[cancelMenu-1];

 

if (cancelMenu>=1 && cancelMenu<=cnt) {

if (deleteMenu.equals("에스프레소")) {

totPrice-=3000;

} else if (deleteMenu.equals("아메리카노")) {

totPrice-=3500;

} else if (deleteMenu.equals("카페라떼")) {

totPrice-=3800;

} else if (deleteMenu.equals("밀크티")) {

totPrice-=4200;

} else if (deleteMenu.equals("청귤에이드")) {

totPrice-=4000;

}

cnt--;

} else {

System.out.println("잘못된 입력입니다.");

}

 

for (int i=cancelMenu-1; i<cnt; i++) {

orderList[i] = orderList[i+1];

}

 

System.out.println(deleteMenu + "가 주문목록에서 삭제되었습니다.");

System.out.println();

 

} else if (selectMenu==3) {

System.out.print("총 주문금액 : " + totPrice

+ "\n결제할 금액 : ");

int payment = sc.nextInt();

 

if (payment >= totPrice) {

System.out.println("결제가 완료되었습니다."

+ "\n거스름돈은 " + (payment-totPrice) + "원 입니다.");

totPrice=0;

for (int i=0; i<cnt; i++) {

orderList[i]="";

}

} else {

System.out.println("금액이 부족합니다.");

}

 

System.out.println();

} else if (selectMenu==4) {

System.out.println("프로그램을 종료합니다.");

break;

} else {

System.out.println("잘못된 입력입니다.");

}

 

}

 

}

}

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

 

JAVA Eclipse 09 제어문 : 반복문 for

https://developernew.tistory.com/102

 

JAVA Eclipse 10 제어문 : 반복문 while, do-while

https://developernew.tistory.com/103

 

JAVA Eclipse 11 제어문 : 반복문의 break, continue

https://developernew.tistory.com/106

 

JAVA Eclipse 12 배열 : 배열의 개념 및 사용

https://developernew.tistory.com/107

 

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기

https://developernew.tistory.com/115

 

JAVA Eclipse 14 배열 : 다차원배열

https://developernew.tistory.com/117

 

JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120