Vi o post de outra pessoa, e despertou o interesse de tentar aproveitar melhor o exercício. O que acham ?
Main :
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(menu());
while(true){
try{
System.out.println("Choose the payment type : ");
int paymentCode = scanner.nextInt();
PaymentType paymentType = PaymentType.identifyPaymentType(paymentCode);
System.out.println("Enter the purchase value : ");
double purchaseValue = scanner.nextInt();
Payment payment = new Payment(paymentType,purchaseValue);
double taxValue = payment.calculatePaymentTax();
if(paymentType!=PaymentType.PIX){
System.out.println("Purchase fee amount : " + taxValue);
}
System.out.println("Total value : " + (payment.getValue()+taxValue));
break;
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
scanner.nextLine();
}
}
}
public static String menu(){
return """
###########################;
1- Credit Card(3% of fee)
2- Debit Card(2% of fee)
3- Bank slip(2% of fee)
4- Pix(No fee);
###########################""";
}
}
Payment:
public class Payment {
PaymentType paymentType;
double value;
public Payment(PaymentType paymentType,double value) {
this.paymentType = paymentType;
this.value = value;
}
public double calculatePaymentTax(){
return switch(this.paymentType){
case CREDIT_CARD -> this.value*0.03;
case DEBIT_CARD -> this.value*0.02;
case BANK_SLIP -> this.value*0.01;
case PIX -> this.value*0;
};
}
public double getValue() {
return value;
}
}
PaymentType:
public enum PaymentType {
CREDIT_CARD,
DEBIT_CARD,
BANK_SLIP,
PIX;
public static PaymentType identifyPaymentType(int code){
return switch (code) {
case 1 -> PaymentType.CREDIT_CARD;
case 2 -> PaymentType.DEBIT_CARD;
case 3 -> PaymentType.BANK_SLIP;
case 4 -> PaymentType.PIX;
default -> throw new IllegalArgumentException("This is not a valid option");
};
}
}