Queria evitar o uso repetitivo de if statements, e como nesse projeto havia uma quantidade especifica de opcoes que o usuario tinha que selecionar, eu me lembrei do switch, apresentado durante uma das aulas. Estou aberto a criticas construtivas, correcoes e feedback :). Eis o meu codigo:
import java.util.Scanner;
public class Bank {
public static void main(String[] args) {
Scanner askClient = new Scanner(System.in);
boolean quit = false;
String clientName = "Gabriel Nogueira Vieira Resende";
String accountType = "Checking account";
double balance = 1200.00;
while (!quit) {
String message = """
***********************
Your information:
Name: %s
Account type: %s
Initial balance: %2fUSD
***********************
Operations
1- Check your balance
2- Receive value
3- Transfer value
4- Exit
""".formatted(clientName, accountType, balance);
System.out.println(message);
System.out.println("Select an operation (1-4)");
int clientChoice = askClient.nextInt();
boolean clientChoiceIsValid = (clientChoice<=4 && clientChoice>0);
if(!clientChoiceIsValid){
System.out.println("Invalid input, please try again");
} else {
switch (clientChoice) {
case 1:
System.out.println(String.format("Your balance is %2fUSD", balance));
continue;
case 2:
System.out.println("Enter the value to be received:");
double valueReceived = askClient.nextDouble();
balance += valueReceived;
System.out.println("""
The value of %2f has been deposited to your account
Your balance is now %2f
""".formatted(valueReceived, balance));
continue;
case 3:
System.out.println("Enter the value to be transferred");
double valueTransferred = askClient.nextDouble();
boolean hasMoney = (valueTransferred<=balance);
if(hasMoney){
balance -= valueTransferred;
System.out.println("""
The transference was successful
Your balance is now %2f
""".formatted(balance));
} else {
System.out.println("""
Transference failed
Insufficient balance
""");
}
continue;
case 4:
quit = true;
break;
}
}
}
}
}