Unit 5 Classes - Homework
public class BankAccount
{
String accountHolderName;
double balance;
static int totalAccounts;
public BankAccount()
{
accountHolderName = "Unknown";
balance = 0.0;
totalAccounts++;
}
public BankAccount(String accountHolderName, double balance)
{
this.accountHolderName = accountHolderName;
this.balance = balance;
}
void setAccountHolderName(String name)
{
this.accountHolderName = name;
}
void deposit(double amount)
{
if(amount > 0)
{
this.balance += amount;
}
}
void withdraw(double amount)
{
if(this.balance - amount > 0)
{
this.balance -= amount;
}
}
String getAccountHolderName()
{
return this.accountHolderName;
}
double getBalance()
{
return this.balance;
}
static int getTotalAccounts()
{
return totalAccounts;
}
}
public class BankApp
{
public static void runApp()
{
BankAccount JimAccount = new BankAccount();
BankAccount WillAccount = new BankAccount("Will",5);
BankAccount SamAccount = new BankAccount("Sam",200);
JimAccount.setAccountHolderName("Jim");
JimAccount.deposit(50);
WillAccount.withdraw(5);
SamAccount.deposit(5000);
WillAccount.withdraw(2000000);
System.out.println(JimAccount.getBalance() + " " + JimAccount.getAccountHolderName());
System.out.println(WillAccount.getBalance() + " " + WillAccount.getAccountHolderName());
System.out.println(SamAccount.getBalance() + " " + SamAccount.getAccountHolderName());
}
}
BankApp.runApp();
50.0 Jim
5.0 Will
5200.0 Sam