Bank项目

Accout.java

/**
 * 修改 Account 类
4. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出overdraftException异常
5. 修改代码抛出新异常,指明“资金不足”以及不足数额(请求的数额 扣除 当前余额)
 */
//修改 Account 类
//a. 修改 deposit 方法返回 true(意味所有存款是成功的)。
//b. 修改 withdraw 方法来检查提款数目是否大于余额。如果amt小于balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回false。

public class Account {

//  创建 Account 类的两个子类:SavingAccount 和 CheckingAccount 子类

//  b. 创建 SavingAccount 类,该类继承 Account 类
//  c. 该类必须包含一个类型为 double 的 interestRate 属性
//  d. 该类必须包括带有两个参数(balance 和 interest_rate)的公有构造器。该构 造器必须通过调用 super(balance)将 balance 参数传递给父类构造器

    //  a. 修改 Account 类;将 balance 属性的访问方式改为 protected
    protected double balance;//余额,全局变量

    public Account(double init_balance){//初始化余额,参数传递给余额
        balance = init_balance;
    }

    public double getBalance(){//获取余额
        return balance;
    }

    public boolean deposit(double amt){//存款,增加金额,返回布尔值
        balance += amt;
        return true;
    }

    public void withdraw(double amt){//取款amt数额,减少余额,判断后,返回布尔值
        if (balance < amt) {//balance是余额 ,先判断           
            throw new OverdraftException("资金不足", (amt - balance));  //修改代码抛出新异常,指明“资金不足”以及不足数额(请求的数额 扣除 当前余额)       
        }
        this.balance -= amt;//判断过了再减少余额
    }

}

Bank.java

import java.util.*;

/**
 * 修改 Bank 类
修改 Bank 类,利用 ArrayList 实现多重的客户关系,不要忘记倒入必须的java.uti类
1. 将 Customer 属性的声明修改为List 类型,不再使用numberOfCustomers 属性。
2. 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用numberOfcustomers 属性
3. 修改 addCustomer 方法,使用 add 方法
4. 修改 getCustomer 方法,使用 get 方法
5. 修改 getNumofCustomer 方法,使用 size 方法
*/

//a. 创建 Bank 类
public class Bank {
//1. 将 Customer 属性的声明修改为List 类型,不再使用numberOfCustomers 属性。
    private List<Customer> customers;

//  2. 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用numberOfcustomers 属性
    private Bank(){
        customers = new ArrayList<Customer>();
    }

    // 修改 Bank 类,创建名为 getBanking 的公有静态方法,它返回一个 Bank类的实例。
    public static Bank getBank() {
        return bank;
    }

    // 单个的实例应是静态属性,且为私有。同样,Bank 构造器也应该是私有的
    private static Bank bank = new Bank();

//  3. 修改 addCustomer 方法,使用 add 方法  
    public void addCustomer(String f,String l){
        customers.add(new Customer(f, l));//List的add方法,存入元素
    }

//  4. 修改 getCustomer 方法,使用 get 方法
    public Customer getCustomer(int index) { 
        return customers.get(index);
    }

    public Iterator<Customer> getCustomers(){
        return customers.iterator();
    }

//  5. 修改 getNumofCustomer 方法,使用 size 方法
    public int getNumOfCustomers() {
        return customers.size();
    }

}

CheckingAccout.java

/**
1. CheckingAccount 类必须扩展 Account 类
2. 该类必须包含一个类型为 double 的 overdraftProtection 属性。
3. 该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调用 super(balance)将 balance 参数传递给父类构造器。
4. 给类必须包括另一个带有两个参数(balance 和 protect)的公有构造器。该构造器必须通过调用 super(balance)并设置 overdragtProtection 属性,将 balance 参数传递给父类构造器。
5. CheckingAccount 类必须覆盖 withdraw 方法。此方法必须执行下列检查。如 果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是存在透支 保护,则尝试用 overdraftProtection 得值来弥补该差值(balance-amount). 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余 额未受影响。
*/
public class CheckingAccount extends Account{
    private double overdraftProtection = -1;//透支保护.如果是-1,表示没有透支保护

    public CheckingAccount(double init_balance) {
        super(init_balance);
    }

    public CheckingAccount(double init_balance, double overdraftProtection) {
        super(init_balance);
        this.overdraftProtection = overdraftProtection;
    }   
//  修改 CheckingAccount 类
//  6. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出  overdraftException
//  7. 修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保
//  护的赤字,对这个异常使用“no overdraft protection”信息。
//第二是 overdraftProtection 数 额 不 足 以 弥 补 赤 字 : 对 这 个 异 常 可 使 用 ”Insufficient funds for overdraft protection” 信息
    public void withdraw(double amt) {
        //余额足够
        if (balance >= amt){//余额 大于 提取额
            balance -= amt;//余额 减少 提取额
        }else{

            if (overdraftProtection == -1){//没有透支保护
                throw new OverdraftException("no overdraft protection", amt - balance);
            }

            //透支保护足够
            if(overdraftProtection >= (amt - balance) ){//透支保护 大于 提取-余额
                overdraftProtection -= (amt - balance);//透支保护 减少 差额
                balance = 0;//余额清零
            }
            //余额不足,且透支保护不足
            else{
                throw new OverdraftException("Insufficient funds for overdraft protection", amt - balance);
            }
        }

    }
}

Customer.java

import java.util.*;

//修改 Customer 类
//6. 修改 Customer 类,使用 ArrayList 实现多重的账户关系。修改方法同上。

//创建Customer类
public class Customer {

//  a. 声明三个私有对象属性:firstName、lastName 和 account。
    private String firstName;
    private String lastName;
    //private Account[] accounts;//自定义的Account类 作为对象的属性
    //private int numberOfAccounts;//数组下标
    private List<Account> accounts;

//  b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)  
    public Customer(String f, String l) {//构造器      
        firstName = f;
        lastName = l;
//      accounts = new Account[2];
        accounts = new ArrayList<Account>();
    }

//  c. 声明两个公有存取器来访问该对象属性,
//  方法 getFirstName 和 getLastName   返回相应的属性。
    public String getFirstName() {
        return firstName;
    }   
    public String getLastName() {
        return lastName;
    }

//  addAccount(Account) 方法来对 accounts[] 赋值。 
    public void addAccount(Account account) {
        //accounts[numberOfAccounts ++] = account;//数组下标自增
        accounts.add(account);//从参数传进去
    }

//  e. 声明 getAccount 方法以获取 account 属性。
    public Account getAccount(int index) {
        //return accounts[index];
        return accounts.get(index);
    }

    public int getNumOfAccounts(){//获取数组accounts下标
//      return numberOfAccounts;
        return accounts.size();//
    }

    public Iterator<Account> getAccounts(){
        return accounts.iterator();
    }

}

CustomerReport.java

import java.util.*;

/**
可选:修改 CustomerReport 类
修改 CustomerReport 类,使用 Iterator 实现对客户的迭代
1. 在 Bank 类中,添加一个名为 getCustomers 的方法,该方法返回一个客户列 表上的 iterator
2. 在 Customer 类中,添加一个名为个体 Accounts 的方法,该方法返回一个帐 户列表上的 iterator
3. 修改 CustomerReport 类,使用一对嵌套的 while 循环(而不是使用嵌套的for 循环),在客户的 iterator 与帐户的 iterator 上进行迭代
4. 重新编译运行 TestBanking 程序,应看到与上面一样的输出结果
 */
public class CustomerReport {

    public void generateReport() {

        Bank bank = Bank.getBank();//从Bank获取bank对象 保证 单例模式      
        Customer customer = null;

        System.out.println("\t\t\tCUSTOMERS REPORT");
        System.out.println("\t\t\t================");

        Iterator<Customer> customerIterator = bank.getCustomers();
        //for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
        while(customerIterator.hasNext()){
            //customer = bank.getCustomer(cust_idx);
            customer = customerIterator.next();

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

            Iterator<Account> accountIterator = customer.getAccounts();

            while(accountIterator.hasNext()){
            //for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++) {
                //Account account = customer.getAccount(acct_idx);
                Account account = accountIterator.next();

                String account_type = "";

                // 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 SavingAccount) {
                    account_type = "Savings Account";
                } else if (account instanceof CheckingAccount) {
                    account_type = "Checking Account";
                }

                // 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.
                 ***/
                System.out.println(account_type + ": current balance is ¥"
                        + account.getBalance());
            }
        }

    }

}

OverdraftException.java

public class OverdraftException extends RuntimeException {


    private static final long serialVersionUID = 1L;
    /**
     * 创建 OverdraftException 类
1. 在 banking.domain 包中建立一个共有类 OverdraftException. 这个类扩展 Exception 类。
2. 添加一个 double 类型的私有属性 deficit.增加一个共有访问方法getDeficit
3. 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性
     */


    private double deficit;//亏损额

    public double getDeficit() {
        return deficit;
    }

    public OverdraftException(String msg,double deficit) {
        super(msg);
        this.deficit = deficit;
    }


}

SavingAccount.java

/**
    1. 创建 SavingAccount 类,该类继承 Account 类
//  2. 该类必须包含一个类型为 double 的 interestRate 属性
//  3. 该类必须包括带有两个参数(balance 和 interest_rate)的公有构造器。该构 造器必须通过调用 super(balance)将 balance 参数传递给父类构造器

 * @author Administrator
 *
 */
public class SavingAccount extends Account{
    private double interestRate;//加成员 利率

    public SavingAccount(double init_balance,double interestRate){
        super(init_balance);//调用父类的带参数构造器
        this.interestRate = interestRate;//传递参数
    }
}

TestBanking.java


public class TestBanking {

  public static void main(String[] args) {
    Bank     bank = Bank.getBank();
    Customer customer;
    CustomerReport report = new CustomerReport();

    // Create several customers and their accounts
    bank.addCustomer("Jane", "Simms");
    customer = bank.getCustomer(0);
    customer.addAccount(new SavingAccount(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 SavingAccount(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 SavingAccount(150.00, 0.05));

    // Generate a report
    report.generateReport();
  }
}
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值