面向对象(上)-综合练习2:对象数组
package top.qaqaq.java.P237;

public class Account {

	private double balance;
	
	public Account(double init_balance) {
		
		this.balance = init_balance;
		
	}
	
	public double getBalance() {
		
		return balance;
	}
	
	//存钱操作
	public void deposit(double amt) {
		
		if(amt > 0) {
			balance += amt;
			System.out.println("存钱成功");
		}
	}
	
	//取钱操作
	public void withdraw(double amt) {
		
		if(balance >= amt) {
			balance -= amt;
			System.out.println("取钱成功");
		}else{
			System.out.println("余额不足");
		}
		
	}
}
package top.qaqaq.java.P237;

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 acct) {
		
		this.account = acct;
	}

}
package top.qaqaq.java.P237;

public class Bank {
	
	private Customer[] customers;//存放多个客户的数组
	private int numberOfCustomers;//记录客户的个数
	
	public Bank() {
		
		customers = new Customer[10];
	}
	
	//添加客户
	public void addCustomer(String f,String l) {
		Customer cust = new Customer(f,l);
//		customers[numberOfCustomers] = cust;
//		numberOfCustomers++;
		//或
		customers[numberOfCustomers++] = cust;
	}
	
	//获取客户的个数
	public int getNumOfCustomers() {
		
		return numberOfCustomers;
	}
	
	//获取指定位置上的客户
	public Customer getCustomer(int index) {
		
//		return customers[index];//可能报异常
		if(index >= 0 && index < numberOfCustomers) {
			return customers[index];
		}
		
		return null;
	}

}
package top.qaqaq.java.P237;

public class BankTest {
	
	public static void main(String[] args) {
		
		Bank bank = new Bank();
		
		bank.addCustomer("Jane", "Smith");
		
		//连续操作
		bank.getCustomer(0).setAccount(new Account(2000));
		
		bank.getCustomer(0).getAccount().withdraw(500);
		
		double balance = bank.getCustomer(0).getAccount().getBalance();
		System.out.println("客户:" + bank.getCustomer(0).getFirstName() + "的账户余额为:" + balance);
		
		System.out.println("*************************");
		
		bank.addCustomer("万里", "杨");
		
		System.out.println("银行客户的个数为:" + bank.getNumOfCustomers());
		
		
		
	}

}
暂无评论

发送评论 编辑评论


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