Exercise 4 w/ Solution

01/08/2009 12:10

Exercise 4 - Creating Customer Accounts

 

BANKING

Customer

-firstname : String

-lastname : String

+account : Account

+Customer (f: String, l : String)

+getFirstName ( ) : String

+getLastName ( ) : String

+getAccount ( ) : Account

+setAccount(acct : Account)

 

Exercise objective – In this exercise you will expand the Banking project by adding a Customer class. A customer will contain one Account object.

  1. Create the Customer class in the file Customer.java under the banking directory. This directory represents the Java package structure for your program. This class must implement the model in the above UML diagram.
    1. Declare three private object attributes: firstName, lastName, and account.
    2. Declare a public constructor that takes two parameters (f and l) that populate the object attributes.
    3. Declare two public accessors for the object attributes; the methods getFirstName and getLastName return the appropriate attribute
    4. Declare the setAccount method to assign the account attribute.
    5. Declare the getAccount method to retrieve the account attribute.
  2. In the main exercise 4 directory, compile and run the TestBanking program. You should see the following output:

 

Creating the customer Jane Smith

Creating her account with a 500.00 balance.

Withdraw 150.00

Deposit 22.50

Withdraw 47.62

Customer [Smith, Jane] has a balance of 324.88

 

 

Main Code:

 

 

/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import banking.*;

public class TestBanking {

  public static void main(String[] args) {
    Customer customer;
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating the customer Jane Smith.");
    customer = new Customer("Jane", "Smith");
    System.out.println("Creating her account with a 500.00 balance.");
    customer.setAccount(new Account(500.00));
    account = customer.getAccount();

    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));

    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());
  }
}

 

CLASS CODE:


public class Customer{

private String firstname, lastname;
public Account account;
public Customer (String f, String l){
this.firstname = f;
this.lastname = l;

}
public String getFirstName(){
return this.firstname;
}
public String getLastName(){
return this.lastname;
}

public void setAccount(Account acct){
this.account=acct;
}
public Account getAccount()
{
return this.account;
}
}

 

 

 

 

Back