Hullo! Please write the code for the following program using JAVA! Be sure to re
ID: 665996 • Letter: H
Question
Hullo! Please write the code for the following program using JAVA!
Be sure to read CAREFULLY before proceeding!!!
When done PLEASE compile and run to make sure the code WORKS!!!
Cheers!!!
Implement the object model shown in Figure 1-25 (attached) to implement a small banking application. Create five classes: Bank, BankAccount, SavingsAccount, CheckingAccount, and Customer. Bank Should be a singleton class. For simplicity, the bank stores bank accounts in one array and customers in another. All bank accounts are savings accounts or checking accounts, and any customer may have zero or one account of each type.
The difference between the two accounts affects only the withdraw method. The deposit method simply increments the balance. The withdraw method decrements the balance, but cannot always do so. A customer can withdraw from a SavingsAccount only an amount less than or equal to the current balance. For a CheckingAccount the logic is:
Withdraw the amount if less than or equal to the balance.
If the customer has a SavingsAccount, and the SavingsAccount has enough money to cover the shortfall in the CheckingAccount, transfer enough money from saving to checking to cover the withdrawal. Then withdraw the money.
If the customer has no savings account, or the total of both accounts is less than the withdrawal amount, issue an InsufficientFundsException exception(just print message if Exception has not been covered).
Add a main method to the Bank class to test. Create one customer and give that customer a savings account. Try a deposit and withdrawal. Give the customer a checking account and try another deposit. Try withdrawals that succeed without a transfer, that transfer founds from savings, and for which there are insufficient funds.
Bank BankAccount accountNo owner balance accountList has O..n customerList addCustomer addAccount deposit abstract withdraw has SavingsAccount CheckingAccount withdravw withdravw 0..n Customer id savingsAcct checkingAcct 0..1 0..1 has addSavingsAccount addCheckingAccount Figure 1-25 Object model for a banking applicationExplanation / Answer
SavingsAccount.java
public class SavingsAccount extends BankAccount {
//Constructors
public SavingsAccount(double initialBalance)
{
super(initialBalance);
}
public SavingsAccount(int acct, Customer owner, double initBalance)
{
super(acct, owner, initBalance);
}
//Methods
public void intrestTransaction()
{
if( this.balance >= (double)1000 )
{
this.balance = this.balance * (double)1.04;
}
}
}
Customer.java
import java.util.TreeMap;
class Customer
{
// ATTRIBUTES
protected String mName;
protected String mAddress;
protected int mId;
TreeMap<String, BankAccount> accountMap = new TreeMap<String, BankAccount>();
// CONSTRUCTOR
public Customer(String name, String address, int id)
{
this.mName = name;
this.mAddress = address;
this.mId = id;
}
// ADD ACCOUNTS
public void AddAccounts( CheckingAccount CheckingAcct, SavingsAccount SavingsAcct, SavingsAccount StudentLoanlAcct, SavingsAccount AutoLoanAcct)
{
accountMap.put("C", CheckingAcct);
accountMap.put("S", SavingsAcct);
accountMap.put("L", StudentLoanlAcct);
accountMap.put("A", AutoLoanAcct);
}
/**
* @return the Customer name
*/
public String getName()
{
return mName;
}
/**
* @param name the Customer name
*/
public void setName(String name)
{
this.mName = name;
}
/**
* @return the Customer address
*/
public String getAddress()
{
return mAddress;
}
/**
* @param address the Customer address
*/
public void setAddress(String address)
{
this.mAddress = address;
}
/**
* @return the Customer id
*/
public int getId()
{
return mId;
}
/**
* @param id the Customer id
*/
public void setId(int id)
{
this.mId = id;
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount {
// CONSTRUCTORS
public CheckingAccount(double initialBalance)
{
super(initialBalance);
}
public CheckingAccount(int acct, Customer owner, double initBalance)
{
super(acct, owner, initBalance);
}
// METHODS
public void overdraft( Double amount, BankAccount account2)
{
double difference = amount - this.balance;
this.setBalance(0.00);
this.ownersName.accountMap.get("S").setBalance( this.ownersName.accountMap.get("S").balance - difference - 20);
account2.setBalance( account2.balance + amount);
}
}
BankAccount.java
class BankAccount
{
// instance variables (protected to allow inheriting them)
/**
* A unique number that identifies the account
*/
protected int accountNumber;
/**
* The name of the person that this account belongs to
*/
protected Customer ownersName;
/**
* the current value (in dollars) of the money in this account
*/
protected double balance;
//constructors
/**
* Create an account with an initial balance.
* @param initialBalance The initial balance of the account
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
* Create an account with initial parameters.
* @param acct The account number
* @param owner The owner of the account
* @param initBalance The initial balance of the account
*/
public BankAccount(int acct, Customer owner, double initBalance)
{
accountNumber = acct;
ownersName = owner;
balance = initBalance;
}
// balance changing methods
/**
* Updates the current balance by adding in a given amount.
* Post condition: the new balance is increased by the amount.
* @param amount The amount to add
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
* Update the current balance by subtracting the given amount.
* Precondition: the current balance must have at least the amount in it.
* Postcondition: the new balance is decreased by the given amount.
* @param amount The amout to subtract
*/
public void withdraw(double amount)
{
if (balance >= amount)
balance = balance - amount;
}
// get and set methods
/**
* @return The available balance.
*/
public double getBalance( )
{
return balance;
}
/**
* @return The account number.
*/
public int getAccountNumber( )
{
return accountNumber;
}
/**
* @return The owner's name.
*/
public Customer getOwner( )
{
return ownersName;
}
// set: postconditions- these all are used to set new values for the instance variables
/**
* Set the balance.
* @param newBalance The new balance.
*/
public void setBalance(double newBalance )
{
balance = newBalance;
}
/**
* Set the acount number.
* @param newAcctNumber The new account number.
*/
public void setAccountNumber(int newAcctNumber )
{
accountNumber = newAcctNumber;
}
/**
* Set the new owner of the account.
* @param newOwner
*/
public void setOwner(Customer newOwner )
{
ownersName = newOwner;
}
}
Driver.java
import java.util.TreeMap;
import javax.swing.*;
class Driver {
public static void main(String[] args)
{
// create customer Zain and add appropriate accounts
Customer Zain = new Customer("Zain", "1111 Apple Rd", 1);
CheckingAccount zainChecking = new CheckingAccount(100, Zain, 0);
SavingsAccount zainSavings = new SavingsAccount(101, Zain, 0);
SavingsAccount zainStudentLoan = new SavingsAccount(102, Zain, 0);
SavingsAccount zainAutoLoan = new SavingsAccount(103, Zain, 0);
Zain.AddAccounts(zainChecking, zainSavings, zainStudentLoan, zainAutoLoan);
// create customer anthony and add approrpiate accoutns
Customer Anthony = new Customer("Anthony", "2222 Banana St", 2);
CheckingAccount anthonyChecking = new CheckingAccount(200, Anthony, 0);
SavingsAccount anthonySavings = new SavingsAccount(201, Anthony, 0);
SavingsAccount anthonyStudentLoan = new SavingsAccount(202, Anthony, 0);
SavingsAccount anthonyAutoLoan = new SavingsAccount(203, Anthony, 0);
Anthony.AddAccounts(anthonyChecking, anthonySavings, anthonyStudentLoan, anthonyAutoLoan);
// create a map to access customers
TreeMap<String, Customer> map = new TreeMap<String, Customer>();
map.put("1", Zain);
map.put("2", Anthony);
// begin the transaction process
String transaction = JOptionPane.showInputDialog("Please insert a transaction:");
processTransaction(transaction, map);
int done = 0;
while(done == 0)
{
String newTransaction = JOptionPane.showInputDialog("Would you like to proceed with another transaction? (Y\N)");
while(!newTransaction.equals("Y") && !newTransaction.equals("N"))
{
newTransaction = JOptionPane.showInputDialog("You've entered an incorrect value. Try Again Would you like to proceed with another transaction? (Y\N)");
}
if(newTransaction.equals("Y"))
{
transaction = JOptionPane.showInputDialog("Please insert a transaction:");
processTransaction(transaction, map);
}
else if(newTransaction.equals("N"))
{
done = 1;
}
}
printSummary(zainChecking, zainSavings, zainStudentLoan, zainAutoLoan, anthonyChecking, anthonySavings, anthonyStudentLoan, anthonyAutoLoan);
// end of transaction process
}
public static void processTransaction(String transaction, TreeMap<String, Customer> map)
{
String[] fields = new String[6];
fields = transaction.split("\s+");
if(fields[0].equals("1") || fields[0].equals("2"))
{
//----- GET CUSTOMER ------------------------------------------------------------------------------
Customer customer = ((Customer)map.get(fields[0]));
Object account = 0;
Object account2 = 0;
double amount = 0;
//----- GET ACCOUNT AND AMMOUNT AND CHECK FOR ACOUNT TYPE ERROR -----------------------------------
if( fields[1].equals("I") || fields[1].equals("G") )
{
if( fields[2].equals("C") || fields[2].equals("S") || fields[2].equals("A") || fields[2].equals("L") )
{
account = customer.accountMap.get(fields[2]);
}
else
{
System.out.println("Error: illegal account type codes ");
return;
}
if( fields.length != 3 )
{
System.out.println("Error: transaction has too many fields");
return;
}
}
else if( fields[1].equals("D") || fields[1].equals("W") || fields[1].equals("T") )
{
if( fields[3].equals("C") || fields[3].equals("S") || fields[3].equals("A") || fields[3].equals("L") )
{
account = customer.accountMap.get(fields[3]);
try
{
amount = Double.parseDouble(fields[2]);
}
catch(NumberFormatException e)
{
System.out.println("Error: input amount is not a valid value");
return;
}
if(amount < 0) // NEGATIVE AMOUNT
{
System.out.println("Error: negative input value for the amount");
return;
}
if( amount > ((BankAccount)account).getBalance() && fields[1].equals("W") && !fields[3].equals("C") )
{ // INSUFFICIENT FUNDS
System.out.println("Error: insufficient funds");
System.out.format("Current Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
return;
}
if( fields.length != 4 && !fields[1].equals("T") )
{ // TOO MANY FIELDS IN TRANSACTION. T IS HANDELED LATER ON
System.out.println("Error: transaction has too many fields");
return;
}
}
else
{
System.out.println("Error: illegal account type codes ");
return;
}
}
//----- PROCESS TRANSACTION------------------------------------------------------------------------
// DEPOSIT
if( fields[1].equals("D") )
{
((BankAccount)account).deposit(amount);
System.out.println("Transaction Success! Details:");
System.out.println(" Deposit of: $" + fields[2] + " into Custmer " + fields[0] + "'s " + fields[3] + " Account");
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
}
// WITHDRAW
else if( fields[1].equals("W"))
{
if( fields[3].equals("C") && ((BankAccount)account).getBalance() < amount && amount < ( ((BankAccount)account).getBalance() + ((BankAccount)account).ownersName.accountMap.get("C").getBalance() ) )
{
System.out.println("Initiating Overdraft....");
((CheckingAccount)account).overdraft(amount, ((BankAccount)account).ownersName.accountMap.get("C") );
System.out.println("....Overdraft Complete!");
System.out.println("Transaction Success! Details:");
System.out.println(" Withdrawl of: $" + fields[2] + " from Custmer " + fields[0] + "'s " + fields[3] + " Account");
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance() );
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account).ownersName.accountMap.get("S").getAccountNumber(), ((BankAccount)account).ownersName.accountMap.get("S").getBalance() );
return;
}
else if( fields[3].equals("C") && ((BankAccount)account).getBalance() < amount && amount > ( ((BankAccount)account).getBalance() + ((BankAccount)account).ownersName.accountMap.get("C").getBalance() ) )
{
System.out.println("Error: insufficient funds");
System.out.format("Current Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
return;
}
((BankAccount)account).withdraw(amount);
System.out.println("Transaction Success! Details:");
System.out.println(" Withdrawl of: $" + fields[2] + " from Custmer " + fields[0] + "'s " + fields[3] + " Account");
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
}
// INTEREST
else if( fields[1].equals("I"))
{
if( ((BankAccount)account).getBalance() < 1000 )
{
System.out.println("Error: insufficient funds to compute intrest");
return;
}
if( ( fields[2].equals("S") || fields[2].equals("L") || fields[2].equals("A") ) )
{
((SavingsAccount)account).intrestTransaction();
System.out.println("Transaction Success! Details:");
System.out.println(" Intrest Accumulation for Customer " + fields[0] + "'s " + fields[2] + " Account");
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
}
else
{
System.out.println("Error: can't compute intrest on checkings account");
return;
}
}
// TRANSFER
else if( fields[1].equals("T"))
{
account2 = customer.accountMap.get(fields[4]);
if( amount > ((BankAccount)account).getBalance() )
{
System.out.println("Error: insufficient funds");
return;
}
if( fields.length != 5 )
{
System.out.println("Error: transaction has too many fields");
return;
}
else
{
((BankAccount)account).withdraw(amount);
((BankAccount)account2).deposit(amount);
}
System.out.println("Transaction Success! Details:");
System.out.println(" Transfer of $" + fields[2] + " from Customer " + fields[0] + "'s " + fields[3] + " Account to " + fields[4] + " Account");
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
System.out.format(" New Balance for Account Number %d is: %.2f ", ((BankAccount)account2).getAccountNumber(), ((BankAccount)account2).getBalance());
}
// GET BALANCE
else if( fields[1].equals("G"))
{
System.out.format("Current Balance for Account Number %d is: %.2f ", ((BankAccount)account).getAccountNumber(), ((BankAccount)account).getBalance());
}
// ERROR
else
{
System.out.println("Error: illegal transaction type code");
return;
}
}
else
{
System.out.println("Error: illegal customer ID");
}
}
public static void printSummary(CheckingAccount zainChecking, SavingsAccount zainSavings, SavingsAccount zainStudentLoan, SavingsAccount zainAutoLoan, CheckingAccount anthonyChecking, SavingsAccount anthonySavings, SavingsAccount anthonyStudentLoan, SavingsAccount anthonyAutoLoan)
{
System.out.println(" The following are the final account statements: ");
System.out.println(" Customer 1's Checking Account:");
System.out.format(" Account Number - %d%n", zainChecking.getAccountNumber());
System.out.format(" Balance - $%.2f%n", zainChecking.getBalance());
System.out.println(" Customer 1's Savings Account:");
System.out.format(" Account Number - %d%n", zainSavings.getAccountNumber());
System.out.format(" Balance - $%.2f%n", zainSavings.getBalance());
System.out.println(" Customer 1's Sudent Loan Account:");
System.out.format(" Account Number - %d%n", zainStudentLoan.getAccountNumber());
System.out.format(" Balance - $%.2f%n",zainStudentLoan.getBalance());
System.out.println(" Customer 1's Auto Loan Account:");
System.out.format(" Account Number - %d%n", zainAutoLoan.getAccountNumber());
System.out.format(" Balance - $%.2f%n ", zainAutoLoan.getBalance());
System.out.println(" Customer 2's Checking Account:");
System.out.format(" Account Number - %d%n", anthonyChecking.getAccountNumber());
System.out.format(" Balance - $%.2f%n", anthonyChecking.getBalance());
System.out.println(" Customer 2's Savings Account:");
System.out.format(" Account Number - %d%n", anthonySavings.getAccountNumber());
System.out.format(" Balance - $%.2f%n", anthonySavings.getBalance());
System.out.println(" Customer 2's Sudent Loan Account:");
System.out.format(" Account Number - %d%n", anthonyStudentLoan.getAccountNumber());
System.out.format(" Balance - $%.2f%n",anthonyStudentLoan.getBalance());
System.out.println(" Customer 2's Auto Loan Account:");
System.out.format(" Account Number - %d%n", anthonyAutoLoan.getAccountNumber());
System.out.format(" Balance - $%.2f%n", anthonyAutoLoan.getBalance());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.