模拟账户Account和Customer

模拟账户Account和Customer

1、写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。
该类包括的属性:账号 id,余额 balance,年利率 annualInterestRate;
包含的方法:访问器方法(getter 和 setter方法),取款方法 withdraw(),存款方法 deposit()。

模拟账户Account和Customer_第1张图片

提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。

Account.java

public class Account {
     

	private int id;//账号
	private double balance;//余额
	private double annualInterestRate;//年利率
	
	
	public Account(int id, double balance, double annualInterestRate) {
     
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}

	//get、set方法
	public int getId() {
     
		return id;
	}
	public void setId(int 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;
	}
	
	
	//在提款方法withdraw中,需要判断用户余额是否能满足提款数额的要求,如果不能,应给出提示
	public void withdraw(double amount) {
     //取钱
		if(balance < amount) {
     
			System.out.println("余额不足,取款失败");
			return;
		}
		balance-=amount;
		System.out.println("成功取出:" + amount);
	}
	public void deposit(double amount) {
     //存钱
		if(amount > 0) {
     
			balance+=amount;
			System.out.println("成功存入:" + amount);
		}
	}
	
	
}

2. 创建 Customer 类。
模拟账户Account和Customer_第2张图片

a. 声明三个私有对象属性:firstName、lastName 和 account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)
c. 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName 返回相应的属性。
d. 声明 setAccount 方法来对 account 属性赋值。
e. 声明 getAccount 方法以获取 account 属性。

Customer.java


public class Customer {
     

	private String firstName;
	private String lastName;
	private Account account;
	
	
	public Customer(String firstName, String lastName) {
     
		this.firstName = firstName;
		this.lastName = lastName;
	}


	public Account getAccount() {
     
		return account;
	}
	public void setAccount(Account account) {
     
		this.account = account;
	}
	public String getFirstName() {
     
		return firstName;
	}
	public String getLastName() {
     
		return lastName;
	}
	
	
	
}
3.写一个测试程序。
(1) 创建一个 Customer ,名字叫 Jane Smith, 他有一个账号为 1000,余额为 2000 元,
	年利率为 1.23% 的账户。
(2) 对 Jane Smith 操作。
	存入 100 元,再取出 960 元。再取出 2000 元。
	打印出 Jane Smith 的基本信息
	
成功存入 :100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%,
balance is 1140.0

CustomerTest.java

public class CustomerTest {
     

	public static void main(String[] args) {
     
		Customer customer = new Customer("Jane","Smith");
		
		Account account = new Account(1000,2000,0.0123);
		customer.setAccount(account);
		
		customer.getAccount().deposit(100.0);
		customer.getAccount().withdraw(960.0);
		customer.getAccount().withdraw(10000);
		
		System.out.println("Customer [" + customer.getLastName() + ", " + customer.getFirstName()
		+"] has a account: id is " + customer.getAccount().getId()
		+", annualInterestRate is " + customer.getAccount().getAnnualInterestRate() * 100
		+"%, balance is " + customer.getAccount().getBalance());
		
	}
}

你可能感兴趣的:(Java学习练习题,java)