写一个名为Account的类模拟账户。 该类包括的属性:账户id, 余额balance,年利率annualInterestRate:

问题描述:

写一个名为Account的类模拟账户。
该类包括的属性:账户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 [Jane Smith] has a account: id is 1000annualInterestRate is 1.23% balance is 1140.0

代码:

需要注意创建两个类实现目标。

public class homework2{
    
    
	public static void main(String[] args){
    
    
		//创建账户对象
		Account act = new Account("1000",2000,1.23);
		//创建客户对象
		Customer cus = new Customer("Jane Smith",act);
		cus.getAct().deposit(100);
		cus.getAct().withdraw(960);
		cus.getAct().withdraw(2000);
		System.out.println("Customer [" + cus.getName() + "] has a account: id is " + cus.getAct().getId() + "annualInterestRate is " + cus.getAct().getAnnualInterestRate() + "%" + " balance is " + cus.getAct().getBalance());
	}
}
//客户类
class Customer{
    
    
	private String name;
	private Account act;

	public Customer(){
    
    
		
	}

	public Customer(String name,Account act){
    
    
		this.name = name;
		this.act = act;
	}

	public String getName(){
    
    
		return this.name;
	}
	public void setName(String name){
    
    
		this.name = name;
	}
	public Account getAct(){
    
    
		return this.act;
	}
	public void setAct(Account act){
    
    
		this.act = act;
	}
}
//账户类
 class Account{
    
    
	private String id;     
	private double balance;    //私有化,以后所有操作都通过get set来完成
	private double annualInterestRate;
	
	public Account(){
    
    
	
	}
	public Account(String id,double balance,double annualInterestRate){
    
    
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}

	public void withdraw(double quKuan){
    
    
		if(this.balance < quKuan){
    
    
			System.out.println("余额不足,取钱失败");
		}
		else{
    
    
			this.setBalance(this.getBalance() - quKuan);
			System.out.println("成功取出:" + quKuan);
			}
	}
	public void deposit(double cunKuan){
    
    
		this.setBalance(this.getBalance() + cunKuan);
		System.out.println("成功存入:" + cunKuan);
	}

	public String getId(){
    
    
		return id;
	}
	public void setId(String id){
    
       //初始化之后想修改id使用set
		this.id = id;
	}
	public double getBalance(){
    
    
		return balance;
	}
	public void setBalance(double balance){
    
    
		this.balance = balance;
	}
	public double getAnnualInterestRate(){
    
    
		return annualInterestRate;
	}
	public void setId(double annualInterestRate){
    
    
		this.annualInterestRate = annualInterestRate;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40141955/article/details/109105688
今日推荐