Last active
September 17, 2022 11:08
-
-
Save soeunkk/45d34957eeccf1aa58e86d588e2beb68 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* 김소은 | |
* 과제3. 놀이동산 입장권 계산 프로그램 | |
* | |
* 입장권과 관련된 상수, 변수, 함수를 묶어 클래스를 만들었습니다. | |
* 입력값이 잘못됐으면 재입력할 수 있도록 구현하였습니다. | |
*/ | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
class AmusementParkTicket { | |
private static final int BASIC_ENTRANCE_FEE = 10000; // 기본 입장료 | |
private static final int GENERAL_DISCOUNT_ENTRANCE_FEE = 8000; // 일반 할인 입장료 | |
private static final int SPECIAL_DISCOUNT_ENTRANCE_FEE = 4000; // 특별 할인 입장료 | |
private static final int BABY_AGE = 3; | |
private static final int CHILD_AGE = 13; | |
private static final int AFTER_HOUR = 17; | |
private final int age; | |
private final int admissionHour; | |
private final char nationalMerit; | |
private final char welfareCard; | |
public static void checkValidAge(int age) { | |
if (age < 0) { | |
throw new InputMismatchException("나이는 0 이상의 값이어야 합니다."); | |
} | |
} | |
public static void checkValidHour(int hour) { | |
if (hour < 0 || hour > 23) { | |
throw new InputMismatchException("시간은 0 ~ 23 사이의 값이어야 합니다."); | |
} | |
} | |
public static void checkValidYN(char value) { | |
if (value != 'y' && value != 'n') { | |
throw new InputMismatchException("y 또는 n만 입력 가능합니다."); | |
} | |
} | |
public AmusementParkTicket(int age, int admissionHour, char nationalMerit, char welfareCard) { | |
this.age = age; | |
this.admissionHour = admissionHour; | |
this.nationalMerit = nationalMerit; | |
this.welfareCard = welfareCard; | |
} | |
public void printEntranceFee() { | |
System.out.printf("입장료: %d", calculateEntranceFee()); | |
} | |
public int calculateEntranceFee() { | |
if (age < BABY_AGE) { | |
return 0; | |
} | |
if (age < CHILD_AGE || admissionHour >= AFTER_HOUR) { | |
return GENERAL_DISCOUNT_ENTRANCE_FEE; | |
} | |
if (nationalMerit == 'y' || welfareCard == 'y') { | |
return SPECIAL_DISCOUNT_ENTRANCE_FEE; | |
} | |
return BASIC_ENTRANCE_FEE; | |
} | |
} | |
public class Test3 { | |
private static final Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
System.out.println("[입장권 계산]"); | |
AmusementParkTicket amusementParkTicket = inputAmusementParkTicket(); | |
amusementParkTicket.printEntranceFee(); | |
closeResource(); | |
} | |
private static AmusementParkTicket inputAmusementParkTicket() { | |
int age = inputAge(); | |
int admissionHour = inputAdmissionHour(); | |
char nationalMerit = inputNationalMerit(); | |
char welfareCard = inputWelfareCard(); | |
return new AmusementParkTicket(age, admissionHour, nationalMerit, welfareCard); | |
} | |
private static int inputAge() { | |
int age = 0; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("나이를 입력해 주세요.(숫자):"); | |
try { | |
checkIntType(); | |
age = scanner.nextInt(); | |
AmusementParkTicket.checkValidAge(age); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return age; | |
} | |
private static int inputAdmissionHour() { | |
int admissionHour = 0; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("입장시간을 입력해 주세요.(24-hour):"); | |
try { | |
checkIntType(); | |
admissionHour = scanner.nextInt(); | |
AmusementParkTicket.checkValidHour(admissionHour); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return admissionHour; | |
} | |
private static char inputNationalMerit() { | |
char nationalMerit = '-'; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("국가유공자 여부를 입력해 주세요.(y/n):"); | |
try { | |
nationalMerit = scanner.next().charAt(0); | |
AmusementParkTicket.checkValidYN(nationalMerit); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return nationalMerit; | |
} | |
private static char inputWelfareCard() { | |
char inputDescription = '-'; | |
boolean isValid = false; | |
while (! isValid) { | |
System.out.print("복지카드 여부를 입력해 주세요.(y/n):"); | |
try { | |
inputDescription = scanner.next().charAt(0); | |
AmusementParkTicket.checkValidYN(inputDescription); | |
isValid = true; | |
} catch (InputMismatchException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
return inputDescription; | |
} | |
private static void checkIntType() { | |
if (! scanner.hasNextInt()) { | |
removeBuffer(); | |
throw new InputMismatchException("숫자만 입력 가능합니다."); | |
} | |
} | |
private static void removeBuffer() { scanner.next(); } | |
private static void closeResource() { scanner.close(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment