标签:
延续
Java基础 项目实例--Bank项目4
实验要求
1 实验题目 5:
2 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount
3
4
5 实验目的:
6 继承、多态、方法的重写。
7 提 示:
8 创建 Account 类的两个子类:SavingAccount 和 CheckingAccount 子类
9
10 a. 修改 Account 类;将 balance 属性的访问方式改为 protected
11
12 b. 创建 SavingAccount 类,该类继承 Account 类
13
14 c. 该类必须包含一个类型为 double 的 interestRate 属性
15
16 d. 该类必须包括带有两个参数(balance 和 interest_rate)的公有构造器。该 构 造器必须通过调用 super(balance)将 balance 参数传递给父类构造 器。
17
18 实现 CheckingAccount 类
19
20 1. CheckingAccount 类必须扩展 Account 类
21
22 2. 该类必须包含一个类型为 double 的 overdraftProtection 属性。
23
24 3. 该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调 用 super(balance)将 balance 参数传递给父类构造器。
25
26 4. 给类必须包括另一个带有两个参数(balance 和 protect)的公有构造器。该 构造器必须通过调用 super(balance)并设置 overdragtProtection 属性, 将 balance 参数传递给父类构造器。
27
28 5. CheckingAccount 类必须覆盖 withdraw 方法。此方法必须执行下列检 查。如 果当前余额足够弥补取款 amount,则正常进行。如果不够弥补但是 存在透支 保护,则尝试用 overdraftProtection 得值来弥补该差值 (balance-amount). 如果弥补该透支所需要的金额大于当前的保护级别。 则整个交易失败,但余 额未受影响。
View Code
编程后的笔记
1.迫切需要理清楚继承、多态、方法的重写的逻辑
2.针对每个人物对象, 首先先在Bank()中进行构造--调用addCustomer()方法实现类,然后 bank.getCustomer() 返回该customer 对象;
3.接着声明一个账户account类 ,调用方法 customer.setAccount()设置其本人的账户;
4.通过步骤2-3,把bank类 -customer类-account类的关系链接到了一起
5.这样设计,只需要一个bank类 数组即可!针对每个bank类 都可以使用get 获取到对应的account( ) ,再使用类customer获取到 类account.
---以上大概就是面向对象的解决办法
--其实面向过程也好,直接开个结构体数组,该存的都存上--粗暴(但很繁琐)!
UML 结构图
具体工程组织
具体代码实现:
Account.java
package Banking_5;
public class Account {
protected double balance;//余额 ,uml前该变量是 '-'
public Account(double init_balance){
balance=init_balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double b){this.balance=b;}
//存钱
public boolean deposit(double amt){
this.balance+=amt;return true;
}
//取钱
public Boolean withdraw(double amt) {
if(amt>this.balance)
return false;
else{
this.balance-=amt;
return true;
}
}
}
View Code
Bank.java
package Banking_5;
public class Bank {
private Customer[] customers ; //用于存放客户
private int numberofCustomers; //用于记录Customer的个数
public Bank(){
numberofCustomers=0;
customers = new Customer[100]; ///这里记得要初始化!不然要发生java.lang.NullPointerException
}
public void addCustomer(String f,String l){
int i= this.numberofCustomers;
customers[i]=new Customer(f,l);//新建一个构造对象
this.numberofCustomers++;
}
public int getNumOfCustomers() {
return numberofCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
}
View Code
CheckingAccount.java
package Banking_5;
public class CheckingAccount extends Account{
private double overdraft;//超额限额保护
public CheckingAccount(double balance,double overd){
super(balance);
this.overdraft=overd;
}
public void showinfo(){
System.out.println("您的余额:"+this.getBalance()+"\t"+
"您的可透支余额:"+this.overdraft);
}
public Boolean withdraw(double amt){
if(amt<=super.getBalance())
super.setBalance(super.getBalance()-amt );
else{
double val=amt-super.getBalance();
if(val<=this.overdraft)
{
super.setBalance(0);
this.overdraft-=val;
}
else{
System.out.println("该消费超过可透支额的限额");
return false;
}
}
return true;
}
}
View Code
Customer.java
package Banking_5;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(){}
public Customer(String f, String l){
this.firstName=f;
this.lastName=l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
View Code
SavingAccount.java
package Banking_5;
public class SavingAccount extends Account{
private double interest_Rate; //利率
public SavingAccount(double balance,double interest_Rate){
super(balance);
this.interest_Rate=interest_Rate;
}
}
View Code
TestBanking.java( 在测试包中的 测试类)
package TestBanks;
import Banking_5.*;
public class TestBanking_5 {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account;
//
// Create Bank customers and their account
System.out.println("Creating the customer Jane Smith.");
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
account = new SavingAccount(500,0.03);
customer.setAccount(account);
System.out.println("Creating the customer Owen Bryant.");
//code
bank.addCustomer("Owen","Bryant");
customer=bank.getCustomer(1);
account=new CheckingAccount(500,0);
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,500);
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.");
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
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.getBalance());
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.getBalance());
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.getBalance());
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.getBalance());
}
}
View Code
运行结果:
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, Owen] 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
View Code
标签:
来源: https://www.cnblogs.com/zhazhaacmer/p/9774033.html