Java账户取款作业

写一个名为Account的类模拟账户。该类的属性和方法如下所示。

该类包括的属性:账户id,余额balance,年利率annualInterestRate为1.23%;包含的方法:各属性的set和get方法。取款方法withdraw(),存款方法deposit()
写一个测试程序
(1)创建一个Customer,名字叫JaneSmith,他有一-个账号为1000,余额为2000,年利率为
(2)对Jane Smith操作:存入100元,再取出960元,再取出2000。
打印Jane Smith 的基本信息
信息如下显示:成功存入:100

成功取出:960

余额不足,取钱失败

public class HomeWork02{
  public static void main(String[] args){

	Account a=new Account("1000",2000,1.23);
	Customer c=new Customer("Jane Smith",a);
	c.getAct().deposit(100);
	c.getAct().withdraw(960);
	c.getAct().withdraw(2000);
	System.out.println("你的账户剩余"+c.getAct().getBalance()+"元");

	}
}
//客户类
class Customer
{  //客户名字
	private String name;
	//客户账户
	private Account act;
	//无参数构造方法
	public Customer(){}
	//有参数构造方法
	public Customer(String name,Account act){
	this.name=name;
	this.act=act;

	}

	//setter and getter
	public String getName(){
	return name;
	}
	public void setName(String name){
	this.name=name;

	}
	public Account getAct(){
	return act;
	}
	public void setAct(Account act){
	this.act=act;
	}
}
//账户类
class Account{
	//账户id
	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;

	}
	// setter and getter
	public String getId(){
	return id;
	}
	public void setId(String id){
	this.id=id;
	}	
	public double getBalance(){
	return balance;
	}
	public void setBalance(double balance){
	this.balance=balance;
	}	
	public double getAnnualInterestRate(){
	return annualInterestRate;
	}	
	public void setAnnualInterestRate(double annualInterestRate){
	this.annualInterestRate=annualInterestRate;
	}	

	// 存款方法
	public void deposit(double money){
	
	//this.balance+=money;
	this.setBalance(this.getBalance()+money);
	System.out.println("成功存入:"+money);
}
	//取款方法
	public void withdraw(double money){
	if(money>this.getBalance()){
	System.out.println("余额不足,取钱失败");
	return;
	}
	this.setBalance(this.getBalance()-money);
	
	System.out.println("成功取出:"+money);
}

}

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页