扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。

练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。

任务

  1. banking包下的创建Customer类。该类必须实现上面的UML图表中的模型。

a. 声明三个私有对象属性:firstName、lastNameaccount

b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(fl

c. 声明两个公有存取器来访问该对象属性,方法getFirstNamegetLastName返回相应的属性。

d. 声明setAccount 方法来对account属性赋值。

e. 声明getAccount 方法以获取account属性。

                        

  1. exercise2主目录里,编译运行这个TestBanking程序。应该看到如下输出结果:

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

package banking;

import java.util.List;

public class Customer extends Account
{
    //成员属性
    private String firstName ;
    private String lastName ;
    private double account ;
    
    //构造方法
    public Customer() 
    {
    
    }
    //构造方法
    public Customer(String f , String l) 
    {
        this.firstName = f ;
        this.lastName = l ;
    }
    
    //get set 
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public double getAccount() {
        return account;
    }

    public void setAccount(double account) {
        this.account = account;
    }
    
    public String toString() {
        return  firstName + ", " + lastName ;
    }

    
}
package banking;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestBanking {

    public static void main(String[] args) 
    {
        System.out.println("————————————————————————");
        
        //实例化顾客
        Customer cr = new Customer( ) ;
    
        cr.setFirstName("Jane");
        cr.setLastName("Smith");
        cr.setBalance(500);
        System.out.println("Creating the customer "+cr.getFirstName() +" "+cr.getLastName());
        System.out.println("Creating her account  with  a " +cr.getBalance()+" balance");
        System.out.println("Withdraw "+(cr.getBalance()-cr.withdraw(150)) );
        double x1 = cr.getBalance( ) ;
        System.out.println("Deposit "+(cr.deposit(22.5)-x1));
        System.out.println("Withdraw "+(cr.getBalance()-cr.withdraw(47.62)));
        System.out.println("Customer "+cr.getLastName()+" "+cr.getFirstName()+
                                    " has a balance of "+cr.getBalance());
    
        System.out.println("————————————————————————");
        
        
    }

}

原文地址:https://www.cnblogs.com/20gg-com/p/5905929.html