CE1004-計算機實習 Ⅱ Introduction to Computer Science ⅡLab-A-1062 Assignment 3

要求:

code:

原來附上的code:

Main.java

package bank;

import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner input = new Scanner(System.in);
		
		Customer customer = null ;
		
		System.out.println("what mode customer?\n"
				+ "1-general\n"
				+ "2-can transfer\n"
				+ "3-can borrow");
		int customermode = input.nextInt();
		
		input = new Scanner(System.in);
		System.out.println("what's your name?");
		String a = input.nextLine();
		
		switch(customermode) {
			case 1 :
				customer = new Customer(a); 
				break;
			case 2:
				customer= new CustomerTransfer(a);
				break;
			case 3:
				customer = new CustomerBorrow(a);
				break;
		}
		
		customer.sayhi();
		
		int choose = 0 ;

		while(choose!=-1) {	
			customer.showlist();
			choose = input.nextInt();
			customer.choose(choose,customermode);	
		}
		
		System.out.println("Thank's for using!");		
	}
}

Customer.java

package bank;

import java.util.Scanner;

public class Customer {
	String customer_name ;
	int money= 0  ;
	Scanner input = new Scanner(System.in);
	
	public Customer() {
		
	}
	
	public Customer(String customer_name) {
		this.customer_name = customer_name;
	}
	
	public void savemoney(int money) {
		this.money = this.money +  money;	
		show();
	}
	
	public void takemoney(int money) {
		this.money = this.money -  money;	
		show();
	}
	
	public void show() {
		System.out.println(this.customer_name + " have " + this.money +" in the bank ");
	}
	
	public void showlist() {
		System.out.println(" what you want to do?\n"
				+ "1-show the money\n"
				+ "2-save the money\n"
				+ "3-take the money\n"
				+ "4-transfer the money\n"
				+ "5-borrow the money\n"
				+ "-1 to break");	
	}
	
	public void choose(int number, int mode) {
		if(number==1)
			show();
		else if(number==2) {
			int dollors = input.nextInt();
			savemoney(dollors);
		}
		else if(number==3) {
			int dollors = input.nextInt();
			takemoney(dollors);
		}
		else if (number>3 && mode == 1) {
			error();
		}
		
	}
	
	public void error() {
		System.out.println("you don't have this method");
	}

	public void sayhi() {
		System.out.print("Hi " + customer_name);
	}
}

答案:

CustomerBorrow.java

package bank;

public class CustomerBorrow extends Customer {
	private int borrowMoney;
	public CustomerBorrow(String name) {
		this.customer_name = name;
	}
	public void borrow(int borrowMoney) {
		if(borrowMoney <= money) {
			System.out.println("You don't need to borrow money!!!");
		}else {
			this.borrowMoney = borrowMoney;
			money += borrowMoney;
			show();
		}
	}
	@Override
	public void show() {
		System.out.println(this.customer_name + " have " + this.money +" in the bank ");
		System.out.println(this.customer_name + " borrow " + this.borrowMoney + "\n");
	}
	@Override
	public void choose(int number, int mode) {
		if(number==1)
			show();
		else if(number==2) {
			int dollors = input.nextInt();
			savemoney(dollors);
		}
		else if(number==3) {
			int dollors = input.nextInt();
			takemoney(dollors);
		}
		else if (number==5) {
			int borrowMoney = input.nextInt();
			borrow(borrowMoney);
		}else {
			error();
		}
	}
}

CustomerTransfer.java

package bank;

public class CustomerTransfer extends Customer {
	private int transferMoney;
	public CustomerTransfer(String name) {
		this.customer_name = name; //store name
	}
	public void transfer(int transferMoney) {
		if (transferMoney >= money) {
			System.out.println("You don't have enough money!!!");
		}else {
			this.transferMoney = transferMoney;
			money -= transferMoney;
			show();
		}
	}
	@Override
	public void show() {
		System.out.println(this.customer_name + " have " + this.money +" in the bank ");
		System.out.println(this.customer_name + " transfer " + this.transferMoney + "\n");
	}
	@Override
	public void choose(int number, int mode) {
		if(number==1)
			show();
		else if(number==2) {
			int dollors = input.nextInt();
			savemoney(dollors);
		}
		else if(number==3) {
			int dollors = input.nextInt();
			takemoney(dollors);
		}
		else if (number==4) {
			int transferMoney = input.nextInt();
			transfer(transferMoney);
		}else {
			error();
		}
		
	}
	
}

解題思路

題目要求 >Please write two sub classes to fit the give java file.

暗示了要利用inheritance的寫法,故有

... extends ...

的寫法,換言之整份作業就是extendoverride去的作品