I wrote a simple Java bank application and I would like to get an review! I learned today about exceptions, so I tried to apply it on my code, but sadly I couldn't made it. I hope someone here could help me with that.
Account.java
private final String firstName;
private final String lastName;
private double balance;
private static int uid = 0;
private final String phoneNumber;
private final int id;
public Account(String firstName, String lastName, String phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.balance = 0.0;
uid++;
this.id = uid;
}
public String getFirstName(){
return this.firstName + "";
}
public String getLastName(){
return this.lastName + "";
}
public double getBalance(){
return this.balance;
}
public void setBalance(double newBalance) {
this.balance = newBalance;
}
public int getID(){
return this.id;
}
public String getPhoneNumber(){
return this.phoneNumber + "";
}
public void depositMoney(double depositAmount){
this.balance += depositAmount;
System.out.println("You have deposit " +depositAmount +" to your account." + "\n" +
"Balance is now: " +this.balance);
}
public void withdrawal(double withdrawalAmount){
if(this.balance < withdrawalAmount) {
System.out.println("You don't have enough funds.");
} else {
this.balance -= withdrawalAmount;
System.out.println("You have withdrawal " +withdrawalAmount + " from your account." + "\n" +
"Balance is now: " +this.balance);
}
}
public void moneyTransfer(Account thisAccount, Account toAccount, double amountToTransfer){
if(thisAccount.getBalance() > 0) {
toAccount.setBalance(toAccount.balance += amountToTransfer);
thisAccount.setBalance(this.balance -= amountToTransfer);
} else {
System.out.println("You don't have enough funds");
}
}
@Override
public String toString(){
return "Name: " + getFirstName() + "\n" +
"Last name: " +getLastName() +"\n" +
"Balance: " + getBalance() + "\n" +
"ID: " + getID();
}
Bank.java
private final List<Account> bankAccounts;
private final Scanner sc;
public Bank() {
bankAccounts = new ArrayList<>();
sc = new Scanner(System.in);
}
public Account isAccountExist(int accountID, String phoneNumber) {
for (Account account : bankAccounts) {
if (account.getID() == accountID && account.getPhoneNumber().equals(phoneNumber)) {
return account;
}
}
System.out.println("One of the details is incorrect");
return null;
}
//overload method -
public Account isAccountExist(String phoneNumber) {
for (Account account : bankAccounts) {
if (account.getPhoneNumber().equals(phoneNumber)) {
return account;
}
}
System.out.println("One of the details is incorrect");
return null;
}
public void registerAccount() {
System.out.println("First name?");
String firstName = sc.next();
System.out.println("Last name?");
String lastName = sc.next();
System.out.println("Phone number?");
String phoneNumber = sc.next();
if (isPhoneNumberCorrect(phoneNumber)) {
bankAccounts.add(new Account(firstName, lastName, phoneNumber));
System.out.println("You have created account successfully!" + "\n" +
"Your account ID is: " + bankAccounts.get(bankAccounts.size() - 1).getID());
}
}
public void loginAccount() {
System.out.println("Please enter your ID:");
int accountID = sc.nextInt();
System.out.println("Please enter your phone number:");
String phoneNumber = sc.next();
if (isPhoneNumberCorrect(phoneNumber)) {
Account selectedAccount = isAccountExist(accountID, phoneNumber);
boolean exitRequested = false;
while (!exitRequested) {
PrintService.existAccountMenu();
int choice = Integer.parseInt(sc.next());
switch (choice) {
case 1:
System.out.println(selectedAccount.toString());
break;
case 2:
System.out.println("Please enter deposit amount:");
double depositAmount = sc.nextDouble();
selectedAccount.depositMoney(depositAmount);
break;
case 3:
System.out.println("Please enter withdrawal amount:");
double withdrawalAmount = sc.nextDouble();
selectedAccount.withdrawal(withdrawalAmount);
break;
case 4:
System.out.println("Please enter the phone number of the account you want to transfer to: ");
String accountPhoneNumber = sc.next();
if (isPhoneNumberCorrect(accountPhoneNumber)) {
Account accountToTransfer = isAccountExist(accountPhoneNumber);
System.out.println("Enter the amount of money you would like to transfer:");
double moneyToTransfer = sc.nextDouble();
selectedAccount.moneyTransfer(selectedAccount, accountToTransfer, moneyToTransfer);
break;
}
case 5:
exitRequested = true;
break;
default:
System.out.println("Wrong input");
break;
}
}
}
}
public static boolean isPhoneNumberCorrect(String phoneNumber){
if(phoneNumber.length() != 10){
System.out.println("Phone number must be 10 digits.");
return false;
} else {
return true;
}
}
PrintService.java
public static void printMenu() {
System.out.println("Hello, press: " + "\n" +
"\r" + "1.Register" + "\n" +
"\r" + "2.Log in" + "\n" +
"\r" + "3.Exit");
}
public static void existAccountMenu(){
System.out.println("What would you like to do?" + "\n" +
"\r" + "1.Info" + "\n" +
"\r" + "2.Deposit money" + "\n" +
"\r" + "3.Withdrawal money" + "\n" +
"\r" + "4.Money transferring" + "\n" +
"\r" + "5.Exit");
}
Main.java
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Bank bank = new Bank();
boolean exitRequested = false;
while(!exitRequested) {
PrintService.printMenu();
int choice = Integer.parseInt(sc.next());
switch (choice) {
case 1:
bank.registerAccount();
break;
case 2:
bank.loginAccount();
break;
case 3:
exitRequested = true;
break;
default:
System.out.println("Wrong input");
break;
}
}
}
depositMoney
,withdrawal
, andmoneyTransfer
methods are not thread-safe, and in all three money can appear or disappear if used concurrently \$\endgroup\$