7일차 영화관 실생활예제
이번엔 영화관 예매와 매점에서 간식을 사는 예제이다.
일단 까다로웠던 것은,
문자열을 정수로 변환하도록 하는 것과,
포인트와 금액을 차감하고 잔액을 조회하는 부분이었다.
***boolean : 참이나 거짓을 나타내는 값, 1byte
주로 프로그램의 흐름을 제어하는 변수로 사용되는 데이터타입
boolean은 그 결과로 true 또는 false이라는 논리값을 반환한다.
(실습예제)
String helloMsg= "★어서오세요 C*V입니다.★\n";
String menuMsg = "①예매하기\n②구매하기\n③포인트 조회\n④나가기\n";
String films = "①라이온킹(8:00)\n②스파이더맨(12:00)\n③사일런스(23:00)[청소년 관람불가]\n④뒤로가기\n";
String ageMsg = "[청소년 구매 불가 상품]\n나이를 입력하세요\n";
String foodmenuMsg = "메뉴를 선택해주세요.\n①팝콘(5,000원)\n②콜라(2,000원)\n③맥주(3,000원)\n④뒤로가기\n";
String ageMsg2 = "[청소년 구매 불가 상품]\n나이를 입력하세요\n";
int choice = 0;
int age = 0;
int money = 100_000;
int point = 0;
int t_price = 10000;
int popcorn = 5000, coke = 2000, beer = 3000;
boolean t_check;
/*
*
* 구매하기
* 1.팝콘
* 2.콜라
* 3.맥주
* 4.뒤로가기
* 포인트도 50%로
*/
while(true) {
t_check = true;
choice = Integer.parseInt(JOptionPane.showInputDialog(helloMsg+menuMsg));
if(choice ==4)break;
if(!(choice >=1 || choice <=3)) continue;
//잘못 입력했을 때 continue
switch(choice) {
//예매하기 영역
case 1:
//변수의 재사용 위의 choice이미 사용했기때문에 사용 가능함
if(money - t_price < 0) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다.");
continue;
//moeny 와 t_price를 비교하는게 아닌 빼면서 음수로 만들기
}
choice = Integer.parseInt(JOptionPane.showInputDialog(films));
if(choice == 1) {
JOptionPane.showMessageDialog(null, "라이온킹 예매완료(08:00).");
}else if(choice == 2) {
JOptionPane.showMessageDialog(null, "스파이더맨 예매완료(11:00).");
}else if(choice == 3) {
age = Integer.parseInt(JOptionPane.showInputDialog(ageMsg));
if (age>19) {
JOptionPane.showMessageDialog(null, "사일런트 예매완료(23:00).");
}else {
t_check = false;
JOptionPane.showMessageDialog(null, "다시 시도해주세요.");
}
}else {
JOptionPane.showMessageDialog(null, "메인 메뉴로 이동합니다.");
continue;
}
//예매완료 일때 해줄 애들
if(t_check) {
if(point>0) {
if(point-t_price>=0) {
point -= t_price;
}else {
money -= (t_price - point);
point = 0;
}
}else {
money -= t_price;
point += (int)(t_price*0.5);
}
JOptionPane.showMessageDialog(null, "현재 잔액:"+money+"원");
}
break;
//구매하기 영역
case 2:
choice = Integer.parseInt(JOptionPane.showInputDialog(foodmenuMsg));
if(!(choice >=1 || choice <=3)) continue;
if(choice ==4)break;
switch (choice) {
case 1 :
JOptionPane.showMessageDialog(null, "팝콘 구매완료.");
if(t_check) {
if(point>0) {
if(point-popcorn>=0) {
point -= popcorn;
}else {
money -= (popcorn - point);
point = 0;
}
}else {
money -= popcorn;
point += (int)(popcorn*0.5);
}
JOptionPane.showMessageDialog(null, "현재 잔액:"+money+"원");
}
break;
case 2 :
JOptionPane.showMessageDialog(null, "콜라 구매완료.");
if(t_check) {
if(point>0) {
if(point-coke>=0) {
point -= coke;
}else {
money -= (coke - point);
point = 0;
}
}else {
money -= coke;
point += (int)(coke*0.5);
}
JOptionPane.showMessageDialog(null, "현재 잔액:"+money+"원");
}
break;
case 3 :
age = Integer.parseInt(JOptionPane.showInputDialog(ageMsg2));
if (age > 19) {
JOptionPane.showMessageDialog(null, "맥주 구매완료.");
if(t_check) {
if(point>0) {
if(point-beer>=0) {
point -= beer;
}else {
money -= (beer - point);
point = 0;
}
}else {
money -= beer;
point += (int)(beer*0.5);
}
JOptionPane.showMessageDialog(null, "현재 잔액:"+money+"원");
}
}else {
t_check = false;
JOptionPane.showMessageDialog(null, "다시 시도해주세요");
}
}
break;
//포인트조회 영역
case 3:
JOptionPane.showMessageDialog(null, "잔여 포인트 : "+point+"점");
break;
}}}}
**배열을 배우기 전이어서 팝콘,콜라,맥주의 가격을 하나하나 변수를 만들어 설정했다.
Author And Source
이 문제에 관하여(7일차 영화관 실생활예제), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jamiview/7일차-영화관-실생활예제저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)