利用面向对象思想,写一个名为Account的类模拟账户,模拟存取钱系统。

2022-12-01 11:41:59 浏览数 (2)

编写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

代码语言:javascript复制
//账户类
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);

	
	}

}
代码语言:javascript复制
//客户
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;

	}

}
代码语言:javascript复制
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);
	}
}

0 人点赞