软件体系结构作业:继承与多态相关作业(JAVA)

照抄的话改一下变量名什么的的的的的的的!!!然后去创建项目运行一下
还有关注!!!

类图:
软件体系结构作业:继承与多态相关作业(JAVA)_第1张图片

Customer.java:

public class Customer {
	protected String name = null;
	protected String password = null;
	protected double money = 0;
	protected String type = null;
	
	Customer(){
		init();
	}
	
	public void init(){
		this.name = "aaaaaaa";
		this.password = "aaaaaaa";
		this.type = "ADULT";
	}
	
	public String getType() {
		return this.type;
	}
	
	public String getName() {
		return this.name;
	}
	
	public String getPassword() {
		return this.password;
	}
	
	public void setMoney(double money) {
		this.money = money;
	}
}

Customer_Adult.java:

public class Customer_Adult extends  Customer{

	
	Customer_Adult(){
		init();
	}
	
	public void init(){
		this.name = "aaaaaaa";
		this.password = "aaaaaaa";
		this.type = "ADULT";
	}
}

Customer_Child.java:

public class Customer_Child extends Customer {
	Customer_Child(){
		init();
	}
	
	public void init(){
		this.name = "123456";
		this.password = "123456";
		this.type = "CHILD";
	}
	
}

Bank.java:

public class Bank {
	private Map<String, String> user_name =  new HashMap<String,String>();
	private Map<String,Double> user_money =  new HashMap<String,Double>();
	
	Bank(){initBank();}
	
	public void initBank() {
		this.user_name.put("123456","123456");
		this.user_name.put("112233","112233");
		this.user_name.put("aaaaaaa","aaaaaaa");
		
		this.user_money.put(user_name.get("123456") + "_123456",(double) 20);
		this.user_money.put(user_name.get("112233") + "_112233",(double) 200);
		this.user_money.put(user_name.get("aaaaaaa") + "_aaaaaaa",(double) 1000000);
	}
	
	public void withdrawMoney(Customer customer) {
		if(user_name.get(customer.getName()).toString().equals(customer.getPassword())) {
			System.out.println("==============\n您是" + customer.getType() + "类型客户。");
			Scanner scanner = new Scanner(System.in);
			double money = Double.valueOf(user_money.get(customer.getName() + "_" + customer.password));
			double widthdraw = 0;
			System.out.print("您的余额为:" +  money + "(元)\n请输入要取款金额(元):");
			widthdraw = scanner.nextDouble();
			if(widthdraw > money) {
				System.out.println("您的余额为不足!");
			}else {
				user_money.put(customer.getName() + "_" + customer.password, money - widthdraw);
				customer.setMoney(widthdraw);
				money = Double.valueOf(user_money.get(customer.getName() + "_" + customer.password));
				System.out.println("取款成功!\n您的账户余额为:" +  money + "(元)");
			}
		}
	}
}

调用运行:

Bank bank = new Bank();
Customer customer1 = new Customer_Adult();
Customer customer2 = new Customer_Child();
bank.withdrawMoney(customer1);
bank.withdrawMoney(customer2);

运行结果:
软件体系结构作业:继承与多态相关作业(JAVA)_第2张图片

你可能感兴趣的:(作业,java)