I created a simple bank program. The program asks for the user’s name and a starting balance. From there, the user can do 4 things, Check Balance, Add Funds, Transfer Funds and Exit the program. Check Balance simply returns the name of the user along with the remaining amount of funds in his account. Add Funds asks the user for an amount to put in his account. Transfer Funds asks the user for an amount to reduce from his account. This is a simple program that practices your understanding of objects and how to manipulate their fields. You can try Overriding a method to simplify one function of this program.
I posted this code in the hopes of getting people's opinion on if my program follows the standards of OOP and also for any improvements to how it is written. I am self-learning Java and would love any feedback so that I can learn from creating this program.
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean quit = false;
int choice;
String accountName;
double startBalance;
System.out.printf("%24s\n","BankApp v1.0");
System.out.println("=====================================");
System.out.println("Please enter your details below.");
System.out.println("Account Name: ");
accountName = scanner.nextLine();
System.out.println("Starting Balance: ");
startBalance = scanner.nextDouble();
Account account = new Account(accountName,startBalance);
while(!quit){
printMainMenu();
choice = scanner.nextInt();
switch(choice){
case 1:
System.out.printf("Account Name: %s\nAccount Balance: $%.2f\n",account.getAccountName(),
account.getAccountBal());
break;
case 2:
System.out.println("Enter amount to be added: ");
account.addFunds(scanner.nextDouble());
break;
case 3:
System.out.println("Enter amount to be transferred: ");
account.transferFunds(scanner.nextDouble());
break;
case 4:
quit = true;
break;
default:
System.out.println("Invalid choice.");
break;
}
}
scanner.close();
}
public static void printMainMenu(){
System.out.printf("%24s\n" +
"=====================================\n" +
"Please select an option:\n" +
"1 - Check Balance\n" +
"2 - Add Funds\n" +
"3 - Transfer Funds\n" +
"4 - Exit the program\n" +
"=====================================\n" +
"Choice: ","BankApp v1.0");
}
}
This is the Account class code.
public class Account {
private String accountName;
private double accountBal;
public Account(String accountName, double accountBal) {
if(accountBal < 0) {
System.out.println("Starting balance cannot be less than zero.\nBalance set to $0.00");
}else {
this.accountName = accountName;
this.accountBal = accountBal;
System.out.println("Account initialized.\nBalance set to $" + this.accountBal);
}
}
public String getAccountName() {
return accountName;
}
public double getAccountBal() {
return accountBal;
}
public void transferFunds(double withdrawal){
if(withdrawal > this.accountBal){
System.out.printf("Unable to transfer $%.2f. Balance is insufficient.\n",withdrawal);
}else if(withdrawal < 0){
System.out.println("Transfer amount must be greater than zero. Transfer failed.");
}else{
this.accountBal -= withdrawal;
System.out.printf("Transfer of $%.2f successful. Your new balance is $%.2f.\n",withdrawal,this.accountBal);
}
}
public void addFunds(double deposit){
if(deposit < 0){
System.out.println("Amount deposited must be greater than zero.");
}else {
this.accountBal += deposit;
System.out.printf("Deposit of $%.2f successful. Your new balance is $%.2f.\n",deposit,this.accountBal);
}
}
}