面向对象(上)-综合练习1:Account和Customer
package top.qaqaq.java.P236;

public class Account {

	private int id;//账号
	private double balance;//余额
	private double annuallnterestRate;//年利率
	
	public Account(int id, double balance, double annuallnterestRate) {
		
		this.id = id;
		this.balance = balance;
		this.annuallnterestRate = annuallnterestRate;
	}
	
	public int getId() {
		
		return id;
	}
	
	public double getBalance() {
		
		return balance;
	}
	
	public double getAnnuallnterestRate() {
		
		return annuallnterestRate;
	}
	
	public void setId(int id) {
		
		this.id = id;
	}
	
	public void setBalance(double balance) {
		
		this.balance = balance;
	}
	
	public void setAnnuallnterestRate(double annuallnterestRate) {
		this.annuallnterestRate = annuallnterestRate;
	}
	
	//在提款方法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);
			
		}
	}
}
package top.qaqaq.java.P236;

public class Customer {
	
	private String firstName;
	private String lastName;
	private Account account;
	
	public Customer(String f,String l) {
		
		this.firstName = f;
		this.lastName = l;
		
	}
	
	public String getFirstName() {
		
		return firstName;
	}
	
	public String getLastName() {
		
		return lastName;
	}
	
	public Account getAccount() {
		
		return account;
	}
	
	public void setAccount(Account account) {
		
		this.account = account;
	}

}
package top.qaqaq.java.P236;
/*
 * 	写一个测试程序。
	(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
 * 
 * 
 */
public class CustomerTest {
	
	public static void main(String[] args) {
		
		Customer cust = new Customer("Jane", "Smith");
		
		Account acct = new Account(1000, 2000, 0.0123);
		
		cust.setAccount(acct);
		
		cust.getAccount().deposit(100);
		cust.getAccount().withdraw(960);
		cust.getAccount().withdraw(2000);
		
		System.out.println("Customer[" + cust.getLastName() + "," + cust.getFirstName() + 
				"] has a account: id is " + cust.getAccount().getId() + "," + "annualInterestRate is " + 
				cust.getAccount().getAnnuallnterestRate() * 100 + "%,balance is " + cust.getAccount().getBalance());
	}

}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇