Java_银行开户存取款

1.按照如下的UML类图,创建相应的类,提供必要的结构
Java_银行开户存取款_第1张图片
在提款方法withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。deposit()方法表示存款。
2. 按照如下的UML类图,创建相应的类,提供必要的结构

Java_银行开户存取款_第2张图片
3. 按照如下的UML类图,创建相应的类,提供必要的结构
Java_银行开户存取款_第3张图片
 addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer对象,然后把它放到 customer 数组中。还必须把 numberofCustomer 属性的值加 1。
 getNumOfCustomers 方法返回 numberofCustomers 属性值。
 getCustomer方法返回与给出的index参数相关的客户。
// 创建Account类

public class Account {
	// 余额
	private double balance;
	// 无参构造
	public Account() {
	}
	// 有参构造
	public Account(double balance) {
		this.balance = balance;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	// 取钱
	public void withdraw(double amount) {
		if (amount > balance) {
			System.out.println("余额不足!");
		} else {
			balance -= amount;
			System.out.println("成功取出:" + amount);
		}
	}

	// 存钱
	public void deposit(double amount) {
		if (amount >= 0) {
			balance += amount;
			System.out.println("成功存入:" + amount);
		} else {
			System.out.println("不能为负数");
		}

	}

}

// 创建Customer类

public class Customer {

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

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	
	
	
}

// 创建Bank类

public class Bank {
	// 银行存款上限
	private int total = 10;
	// customers 是数组名字
	private Customer[] customers = new Customer[total];
	// 实际存款个数
	private int numberOfCustomer;
	
	public Bank(){}
	
	public void addCustomer(String f,String l){
		if(numberOfCustomer < total){
		// 创建客户
		Customer customer = new Customer(f,l);
		// 将客户存到数组
		customers[numberOfCustomer] = customer;
		 numberOfCustomer++;
		}else System.out.println("银行账户满了");
	}
	
	public int getNumberOfCustomer(){
		return numberOfCustomer;
	}
	
	public Customer getCustomer(int index){
		return customers[index];
	}
	
	public void printInfo(){
		// 遍历数组
		for(int i = 0;i<getNumberOfCustomer();i++){
			// 将客户与数组一一对应
			Customer customer = getCustomer(i);
			
			String f = customer.getFirstName();
			String l = customer.getLastName();
			// Customer中定义的账户,是否开户
			Account acct = customer.getAccount();
			// 防止空指针异常
			if(acct != null){
				System.out.println(f+""+l+",账户余额是:"+acct.getBalance());
			}else System.out.println(f+""+l+"还没开户");
		}
	}
}

// 测试

public class Test3_2 {

	public static void main(String[] args) {
		Bank bank = new Bank();
		// 创建账户
		bank.addCustomer("老","顽童");//[0]
		bank.addCustomer("郭", "靖");//[1]
		bank.addCustomer("黄", "蓉");
		bank.addCustomer("欧阳","峰");
		bank.addCustomer("欧阳", "客");
		bank.addCustomer("黄", "药师");
		bank.addCustomer("王", "重阳");
		bank.addCustomer("周","博通");
		bank.addCustomer("穆", "念慈");
		bank.addCustomer("杨", "康");//[9]
		bank.addCustomer("梅", "超风");
		// 开户
		bank.getCustomer(0).setAccount(new Account(1000));
		bank.getCustomer(1).setAccount(new Account(2000));
		bank.getCustomer(2).setAccount(new Account(3000));
		bank.getCustomer(3).setAccount(new Account(4000));
		bank.getCustomer(4).setAccount(new Account(5000));
		// 打印的东西
		bank.printInfo();
	}

}

你可能感兴趣的:(实例)