c#银行系统编程_第1页
c#银行系统编程_第2页
c#银行系统编程_第3页
c#银行系统编程_第4页
c#银行系统编程_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、3、在2的基础上,实现下面的类图:Step1:创建Account类,作为各种账户的基类。然后用继承来创建两个专用的账户类:SavingAccount(储蓄账户)类和CheckingAccount(支票账户)类。(1)SavingAccount类,从Account派生其新特征:A 储蓄账户能获得利息,增加一个属性interestRate 表示利率。B 着时间的推移,储蓄账户可以获得利息,所以为SavingAccount类增加一个addInterest()方法(注:未在UML图中画出),用于把利息增加到原balance上。利息的计算规则为interestRate*balance;(2)Checki

2、ngAccount类,从Account派生新特征:A 帐户允许有透支额度,增加属性overdraftProtection表示最大透支额度。B 取款规则发生了改变:如果当前余额足够支付要提取的金额amount ,按照常规进行处理。如果当前余额不够,但帐户有透支额度,那么所差的部分作为透支处理。透支处理规则:比较amount(当前提款金额)和balance(当前帐户余额)若当前透支额amount-balanceoverdraftProtection,那么整个交易应该放弃,提款失败。否则提款后的balance应为0,提款后的最大透支额度overdraftProtection应该为原有最大透支额度减去

3、(amount-balance )。(3)再看Account类其withdraw行为要在其具体的子类中才能确定,故将其withdraw方法设计成一个抽象方法,由子类去改写。因此Account类也应重新设计成一个抽象类。Step2:具体实现参考:1、和上一题相比,Account类中的balance属性的访问控制修饰符变成了protected。其withdraw方法变成一个抽象方法,因此Account类是一个抽象类。2、SavingsAccount类设计1)定义一个double型的数据属性interestRate(利率)。 2)定义一个带有两个参数balance和interest_rate的构造方

4、法。这个构造方法通过super(balance)调用父类的构造方法完成对balance属性的初始化。3) 增加一个addInterest()方法,将所获利息加到balance上。4)实现父类中的withdraw抽象方法。 3、CheckingAccount类1)定义一个double 型的属性(最大透支额度)。2)定义一个带有一个balance 参数的构造方法。这个构造方法通过super(balance) 调用父类的构造方法。3)定义另外一个带有两个参数的构造方法。这个构造方法通过super(balance) 调用父类的构造方法,并且对属性overdraftProtection 进行设置。4)对

5、成员方法withdraw 进行改写,改写规则见Step1。Step3:仿照银行和客户之间的关系修改Customer类,使一个客户可以有多个帐号。Step4:编写测试程序,首先添加四个客户到银行,并分别为其添加帐号。打印报表。并对第一个客户的各个帐户进行操作以验证SavingsAccount和CheckingAccount类中withdraw方法的正确性。测试程序最终输出的结果如下: Customer Reports =Customer: Jane Smith SavingsAccount: Current balance is 500.0 Interest rate=0.03 Checking

6、Account: Current balance is 500.0 OverdraftProtection=0.0 CheckingAccount: Current balance is 500.0 OverdraftProtection=500.0Customer: Owen Bryant CheckingAccount: Current balance is 200.0 OverdraftProtection=0.0Customer: Tim Soley SavingsAccount: Current balance is 1500.0 Interest rate=0.03 Checkin

7、gAccount: Current balance is 200.0 OverdraftProtection=0.0Customer: Maria Soley CheckingAccount: Current balance is 200.0 OverdraftProtection=0.0 SavingsAccount: Current balance is 150.0 Interest rate=0.03Testing account of customer No.1.Withdraw 300.00 trueWithdraw 300.00 falseBalance of account NO

8、. 1 is 200.0Withdraw 300.00 trueWithdraw 300.00 falseBalance of account NO. 2 is 200.0Withdraw 300.00 trueWithdraw 300.00 trueBalance of account NO. 3 is 0.0代码using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 public abstract class Account protected double

9、balance; public Account(double balance) this.balance = balance; public double getBalance() return balance; public void deposit(double amount) balance += amount; public abstract void withdraw(double amount); public class SavingsAccount : Account private double interestRate; public SavingsAccount(doub

10、le b, double ir) : base(b) interestRate = ir; public override void withdraw(double amount) if (balance amount) balance -= amount; / return true; else Console.WriteLine(余额不足); /return false; public class CheckingAccount : Account private double overdraftProtection; public CheckingAccount(double b) :

11、base(b) public CheckingAccount(double b, double op) : base(b) overdraftProtection = op; public override void withdraw(double amount) if (balance - amount 0) balance = balance - amount; else if (balance + overdraftProtection amount) overdraftProtection -= amount - balance; balance = 0; else Console.W

12、riteLine(超支); public class Customer private int numberOfSavingsAccount, numberOfCheckingAccount; private string firstName; private string lastName; /private Account account; private SavingsAccount savingsaccount = new SavingsAccount10; private CheckingAccount checkingaccount = new CheckingAccount10;

13、 public Customer(string firstName, string lastName) this.firstName = firstName; this.lastName = lastName; public void addSavingsAccount(SavingsAccount acct) savingsaccountnumberOfSavingsAccount+ = acct; public void addCheckingAccount(CheckingAccount acct) checkingaccountnumberOfCheckingAccount+ = ac

14、ct; public SavingsAccount getSavingsAccount(int index) return savingsaccountindex; public CheckingAccount getCheckingAccount(int index) return checkingaccountindex; public int getNumberOfSavingsAccount() return numberOfSavingsAccount; public int getNumberOfCheckingAccount() return numberOfCheckingAc

15、count; public String getFirstName() return firstName; public String getLastName() return lastName; public class Bank private Customer customers; private int numberOfCustomers; public Bank() customers = new Customer100; numberOfCustomers = 0; public void addCustomer(string f, string l) customersnumbe

16、rOfCustomers+ = new Customer(f, l); public int getNumOfCustomers() return numberOfCustomers; public Customer getCustomer(int index) return customersindex; public class Testing public static void Main(String args) Bank bank = new Bank(); bank.addCustomer(Simms, Jane); bank.addCustomer(Bryant, Owen);

17、bank.addCustomer(Soley, Tim); bank.addCustomer(Soley, Maria); for (int i = 0; i bank.getNumOfCustomers(); i+) Customer c = bank.getCustomer(i); Console.WriteLine(Customer + (i + 1) + is + c.getFirstName() + , + c.getLastName(); if (i = 0) bank.getCustomer(0).addSavingsAccount(new SavingsAccount(500.

18、0, 0.03); Console.WriteLine(tSavingsAccount:Current balance is 500 Interest rate=0.03); bank.getCustomer(0).addCheckingAccount(new CheckingAccount(500.0, 0.0); Console.WriteLine(tCheckingAccount:Current balance is + c.getCheckingAccount(0).balance + OverdraftProtection= + c.getCheckingAccount(0).ove

19、rdraftProtection); bank.getCustomer(0).addCheckingAccount(new CheckingAccount(500.0, 500.0); Console.WriteLine(tCheckingAccount:Current balance is + c.getCheckingAccount(1).balance + OverdraftProtection= + c.getCheckingAccount(1).overdraftProtection); if (i = 1) bank.getCustomer(1).addCheckingAccoun

20、t(new CheckingAccount(200.0, 0.0); Console.WriteLine(tCheckingAccount:Current balance is + c.getCheckingAccount(0).balance + OverdraftProtection= + c.getCheckingAccount(0).overdraftProtection); if (i = 2) bank.getCustomer(2).addSavingsAccount(new SavingsAccount(1500.0, 0.03); Console.WriteLine(tSavi

21、ngsAccount:Current balance is 1500 Interest rate=0.03 ); bank.getCustomer(2).addCheckingAccount(new CheckingAccount(200.0, 0.0); Console.WriteLine(tCheckingAccount:Current balance is + c.getCheckingAccount(0).balance + OverdraftProtection= + c.getCheckingAccount(0).overdraftProtection); if (i = 3) b

22、ank.getCustomer(3).addCheckingAccount(new CheckingAccount(200.0, 0.0); Console.WriteLine(tCheckingAccount:Current balance is + c.getCheckingAccount(0).balance + OverdraftProtection= + c.getCheckingAccount(0).overdraftProtection); bank.getCustomer(3).addSavingsAccount(new SavingsAccount(150.0, 0.03); Console.WriteLine(tSavingsAccount:Current balance is 200.0 Interest rat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论