Java-练习1:Bank银行模拟程序(面向对象实现)

银行模拟程序

修改补充Account.java,Bank.java和Customer.java完成以下需求:
- 客户可以开立账户
- 客户可以从账户存入/取出资金
- 客户可以请求显示其每个帐户的交易和总计的报表
- 不同的账户以不同的方式计算利息
- 支票账户的固定利率为0.1%
- 储蓄账户的前1000美元的利率为0.1%,然后为0.2%
- Maxi-Savings账户的前1000美元的费率为2%,接下来的1,000美元的费率为5%,然后是10%
- 银行经理可以获得一份报告,显示客户列表以及他们拥有的帐户数量
- 银行经理可以获得一份报告,显示银行在所有账户上支付的利息总额

1,Account.java

import java.util.ArrayList;
import java.util.List;
public class Account {
    public static final int CHECKING = 0;  //支票账户
    public static final int SAVINGS = 1;	//储蓄账户
    public static final int MAXI_SAVINGS = 2;//Maxi-Saving账户

    private final int accountType;  //账户类型
    public List<Transaction> transactions;//交易
    private double Amount=0;     //账户余额(自己定义的)

    public Account(int accountType) {
        this.accountType = accountType;
        this.transactions = new ArrayList<Transaction>();
    }
    //存款,需对存款数<=0的情况进行异常处理,异常详情参考测试用例
    public void deposit(double amount) {
    	if(amount<=0)
    	{
    		throw new IllegalArgumentException("amount must be greater than zero");
    	}
    	else{
    		Amount+=amount;  //存款成功
    		Transaction transaction=new Transaction(amount,"deposit");
    		transactions.add(transaction);
    	}  
    }
    
    //取款,需对取款数和账户余额<=0的情况分别进行异常处理,异常详情参考测试用例
    public void withdraw(double amount) {
    	if(amount<=0)
    	{
    		throw new IllegalArgumentException("amount must be greater than zero");
    	}else if(amount>Amount)
    	{
    		throw new IllegalArgumentException("sumTransactions must be greater than zero");
    	}else{
    		Amount-=amount;  //取款成功
    		Transaction transaction=new Transaction(amount,"withdrawal");
    		transactions.add(transaction);
    	}      
    }
    
    //根据不同的账号类型,确定不同利率进行利息计算
    public double interestEarned() {
        double amount = sumTransactions();  //账户余额
        if(accountType==0)   	//支票账户
        {
        	amount=amount*0.001;
        }else if(accountType==1)   //储蓄账户
        {
        	if(amount>1000)
        	{
        		amount=1000*0.001+(amount-1000)*0.002;
        	}else{
        		amount=amount*0.001;
        	}      	
        }else if(accountType==2)   //Maxi-Saving账户
        {
        	if(amount>2000)
        	{
        		amount=1000*0.02+1000*0.05+(amount-2000)*0.1;
        	}else if(amount>1000 && amount<20000){
        		amount=1000*0.02+(amount-1000)*0.05;
        	}else{
        		amount=amount*0.02;
        	}        	
        }       	
        return amount;
    }
    
  //返回账号余额
    public double sumTransactions() {
    	 double amount = 0.0;
    	 amount=Amount;
         return amount;
    }
    
    public int getAccountType() {
        return accountType;
    }
}

 2,Customer.java

import java.util.ArrayList;
import java.util.List;
import static java.lang.Math.abs;
public class Customer {
    private String name;
    private List<Account> accounts;

    public Customer(String name) {
        this.name = name;
        this.accounts = new ArrayList<Account>();
    }

    public String getName() {     //返回客户名字
        return name;
    }
    
    public Customer openAccount(Account account) { //客户开户
        accounts.add(account);
        return this;
    }
    
    public int getNumberOfAccounts() {   //返回客户的账户总数
        return accounts.size();
    }
    //计算客户各账户的总利率
    public double totalInterestEarned() {
        double total = 0;
        for(Account str:accounts)
        {
        	total+=str.interestEarned();
        }
        return total;
    }
    
    //获取所有账户详情
    public String getStatement() {
        String statement = "";
        statement="Statement for "+name+"\n";
        double total=0;  //用户总金额
        for(Account str:accounts)			//对每个账户类型
        {
        	statement+="\n";
        	if(str.getAccountType()==0)   	//支票账户
            {
        		statement+="Checking Account\n";
            }else if(str.getAccountType()==1)   //储蓄账户
            {
            	statement+="Savings Account\n";
            }else if(str.getAccountType()==2)   //Maxi-Saving账户
            {
            	statement+="Maxi Savings Account\n";
            }       	
        	double atotal=0;  //账户总金额
        	for(Transaction trans:str.transactions)			//每个账户的所有交易
            {
        		 statement+="  "+trans.getType()+" "+toDollars(trans.getAmount())+"\n";
        		 if(trans.getType()=="deposit")
        		 {
        			 atotal+=trans.getAmount();
        		 }else if(trans.getType()=="withdrawal"){ 
        			 atotal-=trans.getAmount();
        		 }	 
             }
        	 statement+="Total "+toDollars(atotal)+"\n";
        	 total+=atotal;
        }
        statement+="\nTotal In All Accounts "+toDollars(total);
        return statement;
    }
        
    //格式化金额输出,无需修改,可直接引用
    private String toDollars(double d){
        return String.format("$%,.2f", abs(d));
    }
}

 3,Bank.java

import java.util.ArrayList;
import java.util.List;
public class Bank {
    private List<Customer> customers;

    public Bank() {
        customers = new ArrayList<Customer>();
    }

    public void addCustomer(Customer customer) {
        customers.add(customer);
    }
    
    //返回固定格式的客户列表,格式参考测试用例
    public String customerSummary() {
        String summary = "";
        summary = "Customer Summary";
        for(Customer str:customers)
        {
        	summary+="\n - "+str.getName()+" ("+str.getNumberOfAccounts();
        	int num=str.getNumberOfAccounts();
        	if(num>1)
        	{
        		summary+=" accounts)";
        	}else{
        		summary+=" account)";
        	}
        }
        //summary=summary.substring(0,summary.length()-2); //去掉最后一个客户后面的换行符\n
        return summary;
    }
    
    //返回支付所有客户的利息总和
    public double totalInterestPaid() {
        double total = 0;
        for(Customer str:customers)
        {
        	total+=str.totalInterestEarned();
        }
        return total;
    }
}

 4,DateProvider.java

import java.util.Calendar;
import java.util.Date;
public class DateProvider {
    private static DateProvider instance = null;

    public static DateProvider getInstance() {
        if (instance == null)
            instance = new DateProvider();
        return instance;
    }

    public Date now() {
        return Calendar.getInstance().getTime();
    }
}

5,Transaction.java

import java.util.Calendar;
import java.util.Date;
public class Transaction {
    public final double amount;

    private Date transactionDate;
    
    private String type;  //自己定义的

    public Transaction(double amount, String type) {
        this.amount = amount;
        this.transactionDate = DateProvider.getInstance().now();
        this.type=type;
    }

    public double getAmount() {	//自己定义的 
        return amount;
    }
    public String getType() {	//自己定义的 
        return type;
    }
}

1,AccountTest.java 测试类

import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class AccountTest {
	private static final double DOUBLE_DELTA = 1e-15;
	
	@Rule
	public ExpectedException thrown= ExpectedException.none();
	
    @Test(timeout=4000)
    public void test1() {
        Bank bank = new Bank();
        Account checkingAccount = new Account(Account.CHECKING);
        Customer bill = new Customer("Bill").openAccount(checkingAccount);
        bank.addCustomer(bill);
        thrown.expect(IllegalArgumentException.class);	//thrown.expect放在哪行,就表明预期该行以下的代码段将会抛出预期异常。
        thrown.expectMessage("amount must be greater than zero"); //设置预期异常的message
        checkingAccount.withdraw(-100.0);
    }
    
    @Test(timeout=4000)
    public void test2() {
        Bank bank = new Bank();
        Account checkingAccount = new Account(Account.CHECKING);
        Customer bill = new Customer("Bill").openAccount(checkingAccount);
        bank.addCustomer(bill);
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("sumTransactions must be greater than zero");
        checkingAccount.withdraw(100.0);
    }
    
    @Test(timeout=4000)
    public void test4() {
        Bank bank = new Bank();
        Account checkingAccount = new Account(Account.CHECKING);
        Customer bill = new Customer("Bill").openAccount(checkingAccount);
        bank.addCustomer(bill);
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("amount must be greater than zero");
        checkingAccount.deposit(-100.0);
    }
    
    @Test(timeout=4000)
    public void test5() {
        Bank bank = new Bank();
        Account checkingAccount = new Account(Account.MAXI_SAVINGS);
        bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));

        checkingAccount.deposit(1400.0);
        checkingAccount.withdraw(700.0);
        assertEquals(700.0, checkingAccount.sumTransactions(), DOUBLE_DELTA);
    }
}

2,CustomerTest.java 测试类

import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CustomerTest {
	
    private static final double DOUBLE_DELTA = 1e-15;
	
	@Test(timeout=4000)
	public void test1(){

        Account checkingAccount = new Account(Account.CHECKING);
        Account savingsAccount = new Account(Account.SAVINGS);
        Account maxisavingsAccount = new Account(Account.MAXI_SAVINGS);

        Customer henry = new Customer("Henry");
        henry.openAccount(checkingAccount);
        henry.openAccount(savingsAccount);
        henry.openAccount(maxisavingsAccount);
        checkingAccount.deposit(100.0);
        checkingAccount.withdraw(50.0);
        savingsAccount.deposit(4000.0);
        savingsAccount.withdraw(200.0);
        maxisavingsAccount.deposit(500);

        assertEquals("Statement for Henry\n" +
                "\n" +
                "Checking Account\n" +
                "  deposit $100.00\n" +
                "  withdrawal $50.00\n" +
                "Total $50.00\n" +
                "\n" +
                "Savings Account\n" +
                "  deposit $4,000.00\n" +
                "  withdrawal $200.00\n" +
                "Total $3,800.00\n" +
                "\n" +
                "Maxi Savings Account\n" +
                "  deposit $500.00\n" +
                "Total $500.00\n" +
                "\n" +
                "Total In All Accounts $4,350.00", henry.getStatement());
    }

	@Test(timeout=4000)
    public void test2(){
		Bank bank = new Bank();
	    Account checkingAccount1 = new Account(Account.MAXI_SAVINGS);
	    Account checkingAccount2 = new Account(Account.SAVINGS);
	    Customer bill = new Customer("Bill");
	    bank.addCustomer(bill.openAccount(checkingAccount1));
	    bank.addCustomer(bill.openAccount(checkingAccount2));
	    checkingAccount1.deposit(3000.0);
	    checkingAccount2.deposit(1000.0);
	    assertEquals(171.0, bill.totalInterestEarned(), DOUBLE_DELTA);
    }
}

3,BankTest.java 测试类



import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

import org.junit.Rule;
public class BankTest {
    private static final double DOUBLE_DELTA = 1e-15;    
	@Rule
	public ExpectedException thrown= ExpectedException.none();
	
    @Test(timeout=4000)
    public void test1() {
        Bank bank = new Bank();
        Customer john = new Customer("John");    		//新建用户		
        john.openAccount(new Account(Account.CHECKING)); //用户开户
        bank.addCustomer(john);							//添加用户

        assertEquals("Customer Summary\n - John (1 account)", bank.customerSummary()); //期望结果-实际结果-允许误差
    }

    @Test(timeout=4000)
    public void test2() {
        Bank bank = new Bank();
        Account checkingAccount1 = new Account(Account.CHECKING);
        Account checkingAccount2 = new Account(Account.SAVINGS);
        Customer bill = new Customer("Bill").openAccount(checkingAccount1);
        Customer tim = new Customer("Tim").openAccount(checkingAccount2);
        bank.addCustomer(bill);
        bank.addCustomer(tim);
        checkingAccount1.deposit(100.0);
        checkingAccount2.deposit(1000.0);
        
        assertEquals(1.1, bank.totalInterestPaid(), DOUBLE_DELTA);
    }

    @Test(timeout=4000)
    public void test3() {
        Bank bank = new Bank();
        Account checkingAccount = new Account(Account.SAVINGS);
        bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));

        checkingAccount.deposit(1500.0);

        assertEquals(2.0, bank.totalInterestPaid(), DOUBLE_DELTA);
    }

    @Test(timeout=4000)
    public void test4() {
        Bank bank = new Bank();
        Account checkingAccount = new Account(Account.MAXI_SAVINGS);
        bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));

        checkingAccount.deposit(3000.0);

        assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA);
    }
    
    @Test(timeout=4000)
    public void test5() {
        Bank bank = new Bank();
        Customer john = new Customer("John");
        Customer jack = new Customer("Jack");
        john.openAccount(new Account(Account.CHECKING));
        john.openAccount(new Account(Account.SAVINGS));
        jack.openAccount(new Account(Account.MAXI_SAVINGS));
        bank.addCustomer(john);
        bank.addCustomer(jack);
        assertEquals("Customer Summary\n - John (2 accounts)\n - Jack (1 account)", bank.customerSummary());
    } 
}

  • 1
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39450145

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值