编写Java程序
该类的属性和方法如下所示。
该类包括的属性:账户id,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法。取款方法withdraw(),存款方法deposit()
写一个测试程序
(1)创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.23%的账户
(2)对Jane Smith操作:
存入100元,再取出960元,再取出2000。
打印Jane Smith的基本信息
信息如下显示:
成功存入:100
成功取出:960
余额不足,取钱失败
Customer [Smith,Jane] has a account :id is 1000 annualInterestRate is 1.23% balance is 1140.0
//账户类
class Account{
private String id;
private double balance;
private double annualInterestRate;
//无参构造
public Account(){
}
//有参构造
public Account(String id,double balance,double annualInterestRate){
this.id=id;
this.balance=balance;
this.annualInterestRate=annualInterestRate;
}
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setBalance(double balance){
this.balance=balance;
}
public double getBalance(){
return balance;
}
public void setAnnualInterestRat(double annualInterestRate){
this.annualInterestRate=annualInterestRate;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
//取款
public void withdraw(double money){
if(money > getBalance()){
System.out.println("余额不足,取钱失败");
return;
}
this.setBalance(this.getBalance() - money);
System.out.println("成功存入:"+money);
}
//存款
public void deposit(double money){
this.setBalance(this.getBalance()+ money);
System.out.println("成功存入:"+money);
}
}
//客户
class Customer{
//名字
private String name;
//客户有一个账户才能存取钱
Account act;
public Customer(){
}
public Customer(String name,Account act){
this.name=name;
this.act=act;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAct(Account act){
this.act=act;
}
public Account getAct(){
return act;
}
}
public class Text{
public static void main(String[] args){
Account a=new Account("1000",2000,1.23);
Customer c1=new Customer("Jane Smith",a);
c1.getAct().deposit(100); //人有一个账户存取钱
c1.getAct().withdraw(960);
c1.getAct().withdraw(2000);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/144947.html