Account:

package banking5;
//账户 public class Account {
protected double balance; public Account(double int_balance) {
balance = int_balance;
} public double getBlance() {
return balance;
} public boolean deposit(double amt) {
balance += amt;
return true;
} public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
System.out.println("余额不足");
return false;
}
}
}

Customer:

package banking5;

public class Customer {
private String firstName;
private String lastName;
private Account account; public Customer(String f, String l) {
firstName = f;
lastName = l;
} public String getFirstName() {
return firstName;
} public String getLastName() {
return lastName;
} public Account getAccount() {
return account;
} public void setAccount(Account acct) {
account = acct;
}
}

Bank:

package banking5;

public class Bank {

    private Customer[] customers;
private int numberOfCustomers; public Bank() {
customers = new Customer[5];
} public void addCustomer(String f, String l) {
Customer cust = new Customer(f, l);
customers[numberOfCustomers] = cust;
numberOfCustomers++;
} public int getNumberofCustomer() {
return numberOfCustomers;
} public Customer getCustomer(int index) {
return customers[index];
}
}

CheckingAccount:

package banking5;

//信用卡账户
public class CheckingAccount extends Account {
private double overdraftProtection;// 透支额度 public CheckingAccount(double balance) {
super(balance);
} public CheckingAccount(double balance, double protect) {
super(balance);
this.overdraftProtection = protect;
} public double getOverdraftProtection() {
return overdraftProtection;
} public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
} public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else if (overdraftProtection >= amt - balance) { overdraftProtection -= (amt - balance);
balance = 0;
return true;
} else {
System.out.println("额度不够");
return false;
}
}
}

SavingAccount:

package banking5;

public class SavingAccount extends Account {
private double interestRate;// 利率 public SavingAccount(double balance, double init_rate) {
super(balance);
this.interestRate = init_rate;
} public double getInterestRate() {
return interestRate;
} public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
} }

TestBanking5:

package TestBanking;
/*
* 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 banking5.Account;
import banking5.Bank;
import banking5.CheckingAccount;
import banking5.Customer;
import banking5.SavingAccount; public class TestBanking5 { public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account; // Create bank customers and their accounts
// System.out.println("Creating the customer Jane Smith.");
bank.addCustomer("Jane", "Simms");
// code
account = new SavingAccount(500.00, 0.03);
customer = bank.getCustomer(0);
customer.setAccount(account); System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
// code
System.out.println("Creating the customer Owen Bryant.");
// code
bank.addCustomer("Owner", "Bryant");
customer = bank.getCustomer(1);
account = new CheckingAccount(500.00);
customer.setAccount(account);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
// code System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
account = new CheckingAccount(500.00, 500.00);
customer.setAccount(account); System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
// code
System.out.println("Creating the customer Maria Soley.");
// code
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
// account =bank.getCustomer(2).getAccount(); System.out.println("Maria shares her Checking Account with her husband Tim.");
customer.setAccount(bank.getCustomer(2).getAccount()); System.out.println(); //
// Demonstrate behavior of various account types
// // Test a standard Savings Account
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomer(0);
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.getBlance()); System.out.println(); // Test a Checking Account w/o overdraft protection
System.out
.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer = bank.getCustomer(1);
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.getBlance()); System.out.println(); // Test a Checking Account with overdraft protection
System.out
.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer = bank.getCustomer(2);
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.getBlance()); System.out.println(); // Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
customer = bank.getCustomer(3);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Deposit 150.00: " + account.deposit(150.00));
System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastName() + ", " + customer.getFirstName()
+ "] has a balance of " + account.getBlance());
}
} 输出结果:

Creating the customer Jane Smith.
Creating her Savings Account with a 500.00 balance and 3% interest.
Creating the customer Owen Bryant.
Creating his Checking Account with a 500.00 balance and no overdraft protection.
Creating the customer Tim Soley.
Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.
Creating the customer Maria Soley.
Maria shares her Checking Account with her husband Tim.


Retrieving the customer Jane Smith with her savings account.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
余额不足
Withdraw 400.00: false
Customer [Simms, Jane] has a balance of 324.88


Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
额度不够
Withdraw 400.00: false
Customer [Bryant, Owner] has a balance of 324.88


Retrieving the customer Tim Soley with his checking account that has overdraft protection.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
Withdraw 400.00: true
Customer [Soley, Tim] has a balance of 0.0


Retrieving the customer Maria Soley with her joint checking account with husband Tim.
Deposit 150.00: true
额度不够
Withdraw 750.00: false
Customer [Soley, Maria] has a balance of 150.0

 

Bank5的更多相关文章

  1. esxi 5.1 由于断电错误无法启动 报错 bank5 invalid configuration

    由于着急,处理过程中也没有截图,这里简单的描写叙述下整个过程吧. IBM pcserver x3850 可能是机器太热的原因,中午无故掉电,导致esxi无法正常启动 启动时报错 bank5 inval ...

  2. S5PV210的内存分配研究分析

    S5PV210内存一般会使用SDRAM和DDR2 (DDR SDRAM),SDRAM的uboot启动网络已经有很多资料的,对于DDR2还有有很多疑惑,如果有错误的地方,请大家一定指出,醍醐灌顶,不胜感 ...

  3. (一)s3c2440 地址分配讲解 (很难很纠结)

    mini2440的地址怎么分配.mini2440处理器的地址怎么分配. S3C2440处理器可以使用的物理地址空间可以达到4GB,其中前1GB的地址(也就是0x0000 0000--0x4000 00 ...

  4. uboot在nandflash和norflash是如何运行的

    转自:http://www.aiuxian.com/article/p-2796357.html 电子产品如果没有了电,就跟废品没什么区别,是电赋予了他们生命,然而程序则是他们的灵魂. 小时候一直很好 ...

  5. mini2440裸机之MMU(二)(mmu.c) (转)

    分类: 嵌入式 http://blog.chinaunix.net/uid-26435987-id-3082166.html(转) /********************************* ...

  6. ARM--存储管理器

    初入领悟: 1. bank.L-bank的概念 2. s3c2440内部管理SDRAM寄存器配置 Frist part:原理分析 S3c2440为32位微处理器,其可访问空间为4G:但其中提供1G外设 ...

  7. arm汇编:ldr,str,ldm,stm,伪指令ldr

    ldr,str,ldm,stm的命名规律: 这几个指令命名看起来不易记住,现在找找规律. 指令 样本 效果 归纳名称解释 ldr Rd,addressing ldr r1,[r0] addressin ...

  8. CC2530存储空间——Code

    硬件平台:CC2530-F256 开发环境:IAR 8051(版本号 8.10) 參考: .<CC2530 User's Guide.pdf>(swru191c) .<IAR C/C ...

  9. 用jlink在mini2440上烧写uboot

    首先,附上我安装jlink驱动: http://download.csdn.net/detail/zzmno1/3776716#comment 以及我使用的uboot.bin文件下载地址: http: ...

随机推荐

  1. 1 ThinkPHP框架初识

    一.PHP主流框架介绍 主流的框架有laravel.symfony.thinkphp MVC和三层结构 MVC可以说是一种开发模式,三层结构是一种开发习惯,严格来讲,他们两者是完全不同的概念,但是在实 ...

  2. 题目分享J

    题意:从一棵树的树根出发,除树根外每个节点都有其能经过的最多次数与经过后会获得的价值(可能为负,最多只能领一次价值),问最终走回树根能获得的最大价值以及有无可达到此价值的多种走法(ps:一开始在树根就 ...

  3. UML笔记之类图

    1.类与类之间关系在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合( ...

  4. memcached线程模型

    直接上图: memcached使用多线程模型,一个master线程,多个worker线程,master和worker通过管道实现通信. 每个worker线程有一个队列,队列元素为CQ_ITEM. ty ...

  5. D. Count the Arrays 计数题

    D. Count the Arrays 也是一个计数题. 题目大意: 要求构造一个满足题意的数列. \(n\) 代表数列的长度 数列元素的范围 \([1,m]\) 数列必须有且仅有一对相同的数 存在一 ...

  6. 动态代理学习(二)JDK动态代理源码分析

    上篇文章我们学习了如何自己实现一个动态代理,这篇文章我们从源码角度来分析下JDK的动态代理 先看一个Demo: public class MyInvocationHandler implements ...

  7. java结合email实现自动推送

    1.获取表中最后一条数据 public static String demo() throws SQLException { String sql = "select * FROM baox ...

  8. java web程序员微信群

    关注微信公众号"程序员成长日志",回复关键字"java"扫码进群 本群主要为大家解决工作中遇到的问题遇到的问题发到群里大家集思广益平时可以瞎扯不定期红包

  9. Power BI Desktop心得

    我是用钉钉邮箱做账号登录Power BI Desktop的.我用Power BI Desktop,做排版和统计. Power BI由Power Query和Power Pivot组成,前者有M语言,后 ...

  10. [hdu5521 Meeting]最短路

    题意:有N个点,给定M个集合,集合Si里面的点两两之间的距离都为Ti,集合里面的所有点数之和<=1e6.有两个人分别在1和N处,求1个点使得两个人到这一点距离的最大值最小 思路:这题是裸的最短路 ...