Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Tracey King   on Sep 16 In Java Category.

  
Question Answered By: Willie Gomez   on Sep 16

**Create the banking directory. Copy the previous Banking project
**files in this package directory.

**Modify the Customer class to handle the accounts association with
**generic multiplicity; just as you did for in exercise #2 in the
**Arrays module. It must include the public methods: addAccount
(Account), getAccount(int), and getNumOfAccounts().

**Complete the TestBanking Program
**This program creates a set of customers and accounts and generates
**a report of customers and their account balances.

**In the TestBanking.java file you will find comment blocks that
**start and end with /*** ... ***/. These comments indicate the
**location in the code that you must supply.

**Use the instanceof operator to test what type of account we have
**and set account_type to an appropriate value, such as "Savings
**Account" or "Checking Account".

**Print out the type of account and the balance. Feel free to use the
**currency_format formatter to generate a "currency string" for the
**balance.

**Compile and run this program. You should see the following output.
CUSTOMERS REPORT
================

**Customer: Simms, Jane
Savings Account: current balance is ñ 500.00
Checking Account: current balance is ñ 200.00

**Customer: Bryant, Owen
Checking Account: current balance is ñ 200.00

**Customer: Soley, Tim
Savings Account: current balance is ñ 1,500.00
Checking Account: current balance is ñ 200.00

**Customer: Soley, Maria
Checking Account: current balance is ñ 200.00
Savings Account: current balance is ñ 150.00





* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/

import banking.*;
import java.text.NumberFormat;

public class TestBanking {

public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer;
String account_type = "";


// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomer(2).getAccount(1));
customer.addAccount(new SavingsAccount(150.00, 0.05));

// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers();
cust_idx++ ) {
customer = bank.getCustomer(cust_idx);

System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());

for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts();
acct_idx++ ) {
Account account = customer.getAccount(acct_idx);


// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value,
such
**** as "Savings Account" or "Checking Account".
***/

if (account instanceof SavingsAccount){
account_type = "Savings Account";
}


if (account instanceof CheckingAccount)
account_type = "Checking Account";
}


System.out.println(account_type + ": current balance is " +
account.getBalance());

// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
}
}
}

// Account.java
package banking;

public class Account {

protected double balance;

public Account(double init_balance){
balance = init_balance;
}

public boolean deposit(double amount) {
balance = balance + amount;
return true;
}

public boolean withdraw(double amount) {
boolean result = true;
if ( balance < amount ) {
result = false;
} else {
balance = balance - amount;
}
return result;
}

public double getBalance(){
return balance;
}
}
// CheckingAccount
package banking;

public class CheckingAccount extends Account {
private double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
overdraftProtection = -1;
}
public CheckingAccount(double balance, double protect) {
super(balance);
overdraftProtection = protect;
}
public boolean withdraw(double amount) {
if ( amount <= balance) {
balance = balance - amount;
return true;
}
else
if (( overdraftProtection != -1) && (amount - balance) <=
overdraftProtection) {
balance = 0;
overdraftProtection = overdraftProtection + (balance -
amount);
return true;
}
else
{
return false;
}
}
}

// SavingsAccount
package banking;

public class SavingsAccount extends Account {


private double interestRate;



public SavingsAccount(double balance, double interest_rate){

super(balance);

}

}

// Customer
package banking;

public class Customer{

private String firstName;
private String lastName;
private Account account;



private int numberOfAccounts;
private Account [] accounts;
public double Accounts;








public Customer(String f, String l) {
firstName = f;
lastName = l;
}

public Customer(){
accounts = new Account[10];
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setAccount(Account acct) {
account = acct;
}





public double getAccount() {
return Accounts;
}

public int getNumOfAccounts(){
return numberOfAccounts;
}

public Account addAccount(int i){
return accounts[i];

}


}

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on can't compile the TestBanking.java Or get search suggestion and latest updates.


Tagged: