练习5 练习目标:继承、多态、方法的重写。

本文介绍了一个简单的银行账户管理系统的设计与实现,系统包括基本的Account类及两种子类SavingAccount和CheckingAccount。其中,SavingAccount类实现了存款账户的利率功能,而CheckingAccount类实现了透支保护的功能。

练习目标:继承、多态、方法的重写。 在本练习中,将在银行项目中创建Account的两个子类:SavingAccount 和 CheckingAccount。
1009605-20160926212556485-1858124597.png

1009605-20160926212611281-801010759.png
创建 Account类的两个子类:SavingAccount(存款账户) 和 CheckingAccount(透支账户)子类

1.修改Account类;将balance属性的访问方式改为protected
2.创建 SavingAccount 类,该类继承Account类
3.该类必须包含一个类型为double的interestRate(利率)属性
4.该类必须包括带有两个参数(balance和interest_rate)的共有构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。

实现CheckingAccount类
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).如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。

6.在主exercise1目录中,编译并执行TestBanking程序。输出应为:
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 prot
ection.
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 p
rotection.
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

///SavingAccount
package banking;

public class SavingAccount extends Account {
private double interestRate;

public SavingAccount(double b) {
    super(b);
}

public double getInterestRate() {
    return interestRate;
}

public SavingAccount(double b, double i) {
    super(b);
    this.interestRate = i;
}

}

///CheckingAccount

package banking;

public class CheckingAccount extends Account {
public double getOverdraftProtection() {
return overdraftProtection;
}

double overdraftProtection;

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

public CheckingAccount(double b, double protect) {
    super(b);
    this.overdraftProtection = protect;
}

public boolean withdraw(double i)
{
    if(balance>=i)
    {
    balance-=i;
    System.out.print("Withdraw "+i);
    return true;
    }
    else if(balance+overdraftProtection>=i)
    {
        balance=0;
        System.out.print("Withdraw "+i);
        return true;
    }
    else
    {
        System.out.print("Withdraw "+i);
        return false;
    }
}

}

///Testbanking 类

public class TestBanking {
private static void d(Bank bk1)
{
System.out.println("create the customer "+bk1.getCustomer(bk1.getNumOfCustomers()).getFirstName()+" "+bk1.getCustomer(bk1.getNumOfCustomers()).getLastName()+".");
}

public static void main(String[] args) {



    Bank bk1=new Bank();
    bk1.addCustomer( "Jane" , "Smith" );
    d(bk1);
    SavingAccount a1=new SavingAccount(500,3);
    System.out.println("Creating her Savings Account with a "+a1.getBalance()+" balance "+"and"+a1.getInterestRate()+"% "+"interest.");
    bk1.addCustomer("Owen","Bryant");
    d(bk1);
    CheckingAccount a2=new CheckingAccount (500,0);
    System.out.println("Creating her Checking Account with a "+a2.getBalance()+" balance "+"and "+a2.getOverdraftProtection()+" overdraftprotection.");
    System.out.println();       
    bk1.addCustomer("Tim","Soley");
    d(bk1);
    CheckingAccount a3=new CheckingAccount (500,500);
    System.out.println("Creating his Checking Account with a "+a3.getBalance()+" balance "+"and "+a3.getOverdraftProtection()+" overdraftprotection.");
    bk1.addCustomer("Marry","Soley");
    d(bk1);
    System.out.println("Maria shares her Checking Account with her husband Tim.");
    System.out.println();
    System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
    System.out.println(":"+a1.withdraw(150));
    System.out.println(":"+a1.deposit(22.50));
    System.out.println(":"+a1.withdraw(47.62));
    System.out.println(":"+a1.withdraw(400));
    System.out.println("Customer ["+bk1.getCustomer(1).getFirstName()+","+bk1.getCustomer(1).getLastName()+"] has a balance of "+a1.getBalance());
    System.out.println();
    
    System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
    System.out.println(":"+a2.withdraw(150));
    System.out.println(":"+a2.deposit(22.50));
    System.out.println(":"+a2.withdraw(47.62));
    System.out.println(":"+a1.withdraw(400));
    System.out.println("Customer ["+bk1.getCustomer(2).getFirstName()+","+bk1.getCustomer(2).getLastName()+"] has a balance of "+a2.getBalance());
    System.out.println();
    
    System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
    System.out.println(":"+a3.withdraw(150));
    System.out.println(":"+a3.deposit(22.50));
    System.out.println(":"+a3.withdraw(47.62));
    System.out.println(":"+a3.withdraw(400));
    System.out.println("Customer ["+bk1.getCustomer(3).getFirstName()+","+bk1.getCustomer(3).getLastName()+"] has a balance of "+a3.getBalance());
    System.out.println();
    
    System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
    System.out.println(":"+a3.deposit(150));
    System.out.println(":"+a3.withdraw(750));
    System.out.println("Customer ["+bk1.getCustomer(4).getFirstName()+","+bk1.getCustomer(4).getLastName()+"] has a balance of "+a3.getBalance());

}

}
///运行结果
create the customer Jane Smith.
Creating her Savings Account with a 500.0 balance and3.0% interest.
create the customer Owen Bryant.
Creating her Checking Account with a 500.0 balance and 0.0 overdraftprotection.

create the customer Tim Soley.
Creating his Checking Account with a 500.0 balance and 500.0 overdraftprotection.
create the customer Marry Soley.
Maria shares her Checking Account with her husband Tim.

Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
Withdraw 150.0:true
Deposit 22.5:true
Withdraw 47.62:true
Withdraw 400.0:false
Customer [Jane,Smith] has a balance of 324.88

Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
Withdraw 150.0:true
Deposit 22.5:true
Withdraw 47.62:true
Withdraw 400.0:false
Customer [Owen,Bryant] has a balance of 324.88

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

Retrieving the customer Maria Soley with her joint checking account with husband Tim.
Deposit 150.0:true
Withdraw 750.0:false
Customer [Marry,Soley] has a balance of 150.0

转载于:https://www.cnblogs.com/nicebaby/p/5910776.html

项目名称:Bank Account Management System 银行账户管理系统 简称BAM 项目描述:这是一个基于C/S结构的银行账户在线管理系统,用户可以通过ATM终端界面来操作自己的银行账户. 项目实施方式:这是一个同步练习,随着达内CoreJava课程的深入,这个项目将趋于完整,学员的任务是随着知识点的深入,完成每一个进阶的项目要求. 项目一 练习1:(面向对象基础语法) 写一个账户(Account),属性: id:账户号码 长整数 password:账户密码 name:真实姓名 personId:身份证号码 字符串型 email:客户的电子邮箱 balance:账户余额 方法: deposit: 存款方法,参数是double型的金额 withdraw:取款方法,参数是double型的金额 构造方法: 有参和无参,有参构造方法用于设置必要的属性 练习2:(封装) 将Account作成完全封装,注意:要辨别每个属性的set/get方法是否需要公开 练习3:(继承,多态) 银行的客户分为两,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度. 注意:CreditAccount需要多一个属性 ceiling 透支额度 为这两种用户编写相关的 同时要求编写Bank,属性: 1.当前所有的账户对象的集合,存放在数组中 2.当前账户数量 方法: 1.用户开户,需要的参数:id,密码,密码确认,姓名,身份证号码,邮箱,账户型(int),返回新创建的Account对象 2.用户登录,参数:id,密码 返回Account对象,提示 用s1.equals(s2)判断s1和s2两个字符串内容是否相等 3.用户存款,参数:id,存款数额,返回修改过的Account对象 4.用户取款,参数:id,取款数额,返回修改过的Account对象 5.设置透支额度 参数:id,新的额度 ,返回修改过的Account对象.这个方法需要验证账户是否是信用账户 用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数 另外,请为Bank添加几个统计方法 1.统计银行所有账户余额总数 2.统计所有信用账户透支额度总数 写个主方法测试你写的 项目二 练习4:(语言高级特性,三个修饰符) 1.修改Account,银行用户的账号(id)是自动生成的,初始值为100000,第一个开户的用户id为100001,第二个为100002,依此推. 提示:构造对象的时候采用static属性为id赋值 2.对于Account,有两个方法,存款方法和取款方法,请修改这两个方法. 存款方法改为不允许子修改 取款方法根据不同的子而不同,因此,改为抽象方法,在两个子中分别实现 3.将Bank作成单例 项目三 练习5:(接口) 为SavingAccount和CreditAccount各自添加一个子 LoanSavingAccount:用户可以贷款,不可以透支 LoanCreditAccount:用户可以贷款,可以透支 说明:贷款和透支是不一样的,透支指的是账户余额小于0,而贷款用户需要一个贷款额的属性. 在ATM机上,用户可以选择贷款,也可以选择还贷款,而还贷款就是要把账户余额上的资金转到贷款额上 例如:用户余额10000元,贷款额100000元,用户可以选择还款5000元,则用户余额变为5000,贷款额变为95000元. 利用接口来抽象出LoanSavingAccount和LoanCreditAccount的共性 接口中的方法: requestLoan:贷款 payLoan:还贷 getLoan:获取用户贷款总额 为Bank添加三个方法, 贷款:参数 id,贷款额,返回修改过的Account对象 还贷款:参数 id,还款额,返回修改过的Account对象 统计所有账户贷款的总数 练习6:(Object) 为Account及其子添加toString方法和equals方法 项目四 练习7:(Exception) 为BAM添加几个异常 BalanceNotEnoughException :用于取钱的时候余额不足的情况(包括账户余额超过透支额的情况) RegisterException:用于开户异常的情况,例如密码两次输入不一致等情况 LoginException:用户登录异常的情况,例如id错误,密码错误 LoanException:贷款额不能为负数,如果用户试图将贷款额置为负数,则会抛出这个异常 以上四个异常有一个共同的父 BusinessException 并妥善的处理这些异常 项目五 练习8:(集合) 改写Bank,采用集合的方式来管理多个Account对象 为Bank添加一个方法 打印所有用户的总资产排名 说明:一个用户可能会有多个账号,以身份证号为准.总资产指多个账户余额的总和,不需要考虑贷款账户的贷款额 项目六 练习9:(GUI) 为BAM添加用户界面 需要以下几个: BAMClient 其中会包含一个Frame,这是用户主界面 MainPanel:主界面,用户可以选择开户或者登录 RegisterPanel:用户开户具体用到的界面 LoginPanel:用户登录需要的界面 BusinessPanel:界面上会显示账户的功能 至少包括存款和取款,对于可透支的用户,还允许用户修改透支额度,对于贷款用户,还允许用户贷款和还贷款 注:练习的界面布局不做要求,请阅读现有代码,添加事件处理代码 提示:在开户或者登录之后都会跳到BusinessPanel,而用户点击了交易之后,界面停留在BusinessPanel 要随时注意在BusinessPanel上根据数据的变化更新显示信息 项目七 在该加资源保护的地方加上,没有标准 项目八 练习10:(I/O) 修改Bank,账户信息会采用对象序列化的方式存放在文件中.当Bank对象生成的时候会读取文件,设置账户集合.当账户信息改变的时候,会随时更新文件 设计一个FileDAO(文件数据访问对象),负责对文件的访问,包括存放账户,提取账户等方法,在Bank中,会通过FileDAO对象来访问文件 注意:如果已有的账户对象会存在文件中,那么为新的账户对象分配id的做法也应相应的改变,过去的用static属性的做法不再合适,应该改为,把下一个可用的id存放在一个文件中,每创建一个新对象的时候都会读取这个文件,获得新对象的id,并且修改文件中的id,使其加1.这个工作可以放在Account的构造方法中 项目九 练习11:(网络) 在现有的BAM中,用户是通过界面直接访问Bank对象的,将其改为C/S结构,由界面充当客户端,通过TCP协议访问服务器端的Bank对象. 提示:客户端和服务器端需要通过对象来传递信息,这里会使用对象序列化技术.
要求编写程序模拟银行账户的存、取款操作。按要求完成以下步骤: 步骤1:编写程序Account.java,其中定义银行账户Account。该中有账号、姓名、 存款余额等数据域,余额默认是0;有存款、取款、获取当前余额等方法。其中账号为长度 为12位数字的字符串,姓名为字符串,存款余额为double。 步骤2:编写名为CreditAccount的信用卡账户。该继承Account,增加一 个透支限额(double)数据域,透支限额默认为1000。同时该账户取款时允许透支,但不 能超过透支限额。 步骤3:编写名为SavingAccount的储蓄账户SavingAccount。该继承Account 。该账户取款时不允许透支。 步骤4:编写名为Bank的模拟银行,其中可以存储多个型可能是信用卡账户或储 蓄账户的对象(可以用数组或ArrayList实现)。该包含以下方法: 开户:即增加一个新的账户,注意:不允许两个账户的账号相同 销户:即删除一个已有的账户 查询账户:根据一个账号,查询有无该账号的账户 统计目前银行的存款总余额的方法。 统计目前银行的信用卡账户总透支金额的方法。 统计目前总账户数。 统计目前信用卡账户数 统计目前储蓄卡账户数 步骤5:编写客户端Client.java完成以下功能: 编写一个静态方法,创建一个银行对象,并随机生成10个账号从1000 0000 0000 0000到1000 0000 0000 0009,型不同的账户。 main方法中模拟几次开户操作。 main方法中模拟几次销户操作。 模拟几个对指定账号的存款和取款操作。 输出银行的总账户数、总余额、总透支数、各具体账户数。
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索局部开发之间实现平衡。文章详细解析了算法的初始化、勘探开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOAMOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值