I recently built an improved version of a bank account program I made. It has no GUI or any interaction with users, but I simply built it to test it and play with it via main(). I'll post all my classes below and you can run the program yourself or just critique my code. I'll take any and all suggestions, so please leave corrections that you see (this code will most likely be rough, as I'm a beginner programmer altogether, but I'm still learning a lot).
Bank Class:
package improvedBank;
import java.util.*;
public class Bank {
public static ArrayList<Account> bankAccounts = new ArrayList<Account>();
static int time = 0;
int bankCash = 0;
public ArrayList<Account> addAcc(Account acc) {
bankAccounts.add(acc);
return bankAccounts;
}
public static void passTime(Account acc) {
time += 1;
acc.getInterest();
}
// testing code, not sure how to implement user control yet :/
public static void main(String[] args) {
Bank myBank = new Bank();
BusinessAccount billsAccount = new BusinessAccount("Bill's Donut Shop", "Bill", 372);
BusinessAccount joesAccount = new BusinessAccount("Joe's Italian Restaurant", "Joe", 568);
SavingsAccount dillonsAccount = new SavingsAccount("Dillon", 57);
SavingsAccount jessicasAccount = new SavingsAccount("Jessica", 72);
myBank.addAcc(billsAccount);
myBank.addAcc(joesAccount);
myBank.addAcc(dillonsAccount);
myBank.addAcc(jessicasAccount);
billsAccount.deposit(37);
System.out.println(bankAccounts);
dillonsAccount.withdraw(12);
jessicasAccount.deposit(12);
billsAccount.withdraw(4);
System.out.println(bankAccounts);
}
}
Account class (abstract class):
package improvedBank;
public abstract class Account {
public String accountType;
public String owner;
public int balance;
public int timeOpened;
public int getInterest() {
int newBal = (int) (balance * 0.15);
balance += newBal;
timeOpened += 1;
return balance & timeOpened;
}
public int deposit(int amount) {
Bank.passTime(this);
return balance += amount;
}
public int withdraw(int amount) {
Bank.passTime(this);
return balance -= amount;
}
public String toString() {
return "Account Type: " + this.accountType + " | Account owner: " + this.owner + " | Account balance: $" + this.balance + " | Account active for: " + timeOpened + " days";
}
}
SavingsAccount class (extends Account class):
package improvedBank;
public class SavingsAccount extends Account {
public SavingsAccount(String own, int cash) {
this.accountType = "Savings Account";
this.owner = own;
this.balance = cash;
System.out.println("Savings account created.");
Bank.passTime(this);
return;
}
}
BusinessAccount class (also extends Account class):
package improvedBank;
public class BusinessAccount extends Account {
String businessName;
public BusinessAccount(String bizName, String own, int cash) {
this.accountType = "Business Account";
this.businessName = bizName;
this.owner = own;
this.balance = cash;
System.out.println("Business account created");
Bank.passTime(this);
return;
}
}