寫完了,對老師講的面向對象的三個特性確實有了更深的理解,貼上代碼,請大神指點
package prj6;
public class InputInvalidException extends Exception {
?? ?
?? ??? ?private int max;
?? ??? ?public InputInvalidException(int max){
?? ??? ??? ?this.max = max;
?? ??? ?}
?? ??? ?public String toString(){
?? ??? ??? ?return "Invalid input, please try again(1-"+ max+")";
?? ??? ?}
?? ?
}
package prj6;
import java.util.Scanner;
public class Test {
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?int isRent; //value for using the system or not
?? ??? ?RenVehi renv=new RenVehi();
?? ??? ?renv.welMsg(); //print welcome message
?? ??? ?
?? ??? ?Scanner input=new Scanner(System.in); ?? ??? ?
?? ??? ?System.out.println("Are you going to use the XXX rental system? ");
?? ??? ?isRent=input.nextInt();
?? ??? ?
?? ??? ?if (isRent==1){?? ?
?? ??? ??? ?int i=1;
?? ??? ??? ?renv.disHead(); //print title
?? ??? ??? ?Vehicle[] vehi = {new PasVehi("奧迪A4",500,4), new PasVehi("馬自達6",400,4), new PasCargoVehi("皮卡雪6",450,4,2), new PasVehi("金龍",800,20),new CargoVehi("松花江",400,4),new CargoVehi("依維柯",1000,20)};//initial assignment
?? ??? ??? ?for (Vehicle v:vehi){ //traversal all the elements in the object and then print
?? ??? ??? ??? ?System.out.print(i+"\t");
?? ??? ??? ??? ?v.displayCarList();
?? ??? ??? ??? ?i++;
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?int vID[]= renv.getVehicleID(vehi.length); //get all the choices number of customer
?? ??? ??? ?int days=renv.getDays(); // get the days when customer want to rent
?? ??? ??? ?renv.displayBill(vehi, vID, days); //print bill
?? ??? ??? ?
?? ??? ?}else{
?? ??? ??? ?renv.disQuit(); //quit the system
?? ??? ?}
?? ??? ?
?? ?}
}
package prj6;
public class Vehicle { //the class has 4 attributes
?? ?//private int type;
?? ?private String name;?? ?
?? ?private Double rental;
?? ?private int passenger;
?? ?private int cargo;?? ?
?? ?
?? ?
?? ?public String getName(){
?? ??? ?return(name);
?? ?}
?? ?
?? ?public Double getRental(){
?? ??? ?return(rental);
?? ?}
?? ?
?? ?public int getPassenger(){
?? ??? ?return(passenger);
?? ?}
?? ?
?? ?public int getCargo(){
?? ??? ?return(cargo);
?? ?}
??? public Vehicle(String name, Double rental,int passenger,int cargo){ //constructor, get all 4 attribes?? ??? ?
?? ??? ?this.name=name;
?? ??? ?this.rental=rental;
?? ??? ?this.passenger = passenger;
?? ??? ?this.cargo = cargo;
?? ?}
?? ?
?? ?
?? ?public void displayCarList(){ //print all available choice for customers
?? ??? ?if (cargo==0){ //if coach, print name, rent, and capacity of passenger
?? ??? ??? ?System.out.println(name+"\t$"+rental+" /day\t"+"load passenger\t"+passenger);
?? ??? ?}else if(passenger==0){ //if truck, print name, rent, and capacity of cargo
?? ??? ??? ?System.out.println(name+"\t$"+rental+" /day\t"+"load cargo\t"+cargo);
?? ??? ?}else{ //if pickup, print name, rent, and capacity both of passenger and cargo
?? ??? ??? ?System.out.println(name+"\t$"+rental+" /day\tload passenger? "+passenger+"? load cargo "+cargo+"T");
?? ??? ?}
?? ?}?? ?
}
package prj6;
public class PasVehi extends Vehicle { //coach class
?? ?
?? ?public PasVehi(String name, double rental, int passenger ){
?? ??? ?super(name, rental, passenger,0); //call father consturctor with 0 cargo
?? ?}
}
package prj6;
public class CargoVehi extends Vehicle { //truck class
?? ?
?? ?public CargoVehi(String name,double rental,int cargo){
?? ??? ?super(name,rental,0,cargo); //call the father constructor with 0 passenger
?? ?}
}
package prj6;
public class PasCargoVehi extends Vehicle { //pickup class
?? ?public PasCargoVehi(String name,double rental,int passenger,int cargo){
?? ??? ?super(name, rental,passenger,cargo); //call father constructor
?? ?}?? ?
}
package prj6;
import java.util.Scanner;
public class RenVehi{
?? ?//boolean renFlag;
?? ?public void welMsg(){? //print welcome message
?? ??? ?System.out.println("Welcome to XXX rental system!");
?? ??? ?System.out.println();
?? ?}?? ?
?? ?
?? ?public void disHead(){ //print the titles for both system and vehicles
?? ??? ?System.out.println("Price list of XXX rental system");
?? ??? ?System.out.println("Number\t"+"Model\t"+"Price\t\t"+"Capacity");
?? ?}
?? ?
?? ?public void disQuit(){ //print quit message
?? ??? ?System.out.println("You already quit the XXX rental system");
?? ?}
?? ?
?? public int[] getVehicleID(int totalNum){? //get the array of ID for those selected vehicles
?? ??? ?Scanner input = new Scanner(System.in);
?? ??? ?//int numVehi;
?? ??? ?System.out.println("How many vehicles are you going to rent?(1-"+totalNum+")");
?? ??? ?
?? ??? ?int numVehi=input.nextInt();//the number should between 1 and the value of all availble vehicle?? ??? ?
?? ??? ?boolean inputFlag = false;
?? ??? ?do {
?? ??? ??? ?try{?? ??? ??? ??? ?
?? ??? ??? ??? ?checkInput(numVehi,totalNum);
?? ??? ??? ??? ?inputFlag = true;
?? ??? ??? ?}catch (InputInvalidException e){
?? ??? ??? ??? ?System.out.println(e);?? ??? ??? ??? ?
?? ??? ??? ??? ?numVehi=input.nextInt();
?? ??? ??? ?}
?? ??? ?}while (inputFlag==false);
?? ??? ?
?? ??? ?int ID[] = new int[numVehi];?? ??? ?
?? ??? ?inputFlag = false;
?? ??? ?for (int j=0;j<numVehi;j++){?? ??? ??? ?
?? ??? ???? System.out.println("Please input the vehicle's ID(1-"+totalNum+")");
?? ??? ???? ID[j]=input.nextInt();
?? ??? ??? ?
?? ??? ???? do{
?? ??? ??? ??? ?try{
?? ??? ??? ??? ??? ?checkInput(ID[j],totalNum);
?? ??? ??? ??? ??? ?inputFlag = true;
?? ??? ??? ??? ?}catch (InputInvalidException e){
?? ??? ??? ??? ??? ?System.out.println(e);
?? ??? ??? ??? ??? ?ID[j]=input.nextInt();
?? ??? ??? ??? ?}
?? ??? ???? }while (inputFlag==false);
?? ??? ??? ??? ??? ??? ?
?? ??? ?}
?? ??? ?return ID;
?? ?}
?? ?
?? ?public void checkInput(int id, int max) throws InputInvalidException{
?? ??? ?if ((id>max) || (id<1)){?? ??? ??? ?
?? ??? ??? ?throw new InputInvalidException(max);
?? ??? ?}
?? ??? ??? ?
}?? ?
?? ?public int getDays(){ //get the days that customer want to rent
?? ??? ?Scanner input = new Scanner(System.in);
?? ??? ?int days;
?? ??? ?
?? ??? ?System.out.println("Please input the days(>0):");
?? ??? ?days = input.nextInt();
?? ??? ?while (days<1){ //days should be positive number
?? ??? ??? ?System.out.println("Invalid days, please try again");
?? ??? ??? ?System.out.println("Please input the days(>0):");
?? ??? ??? ?days=input.nextInt();
?? ??? ?}
?? ??? ?return(days);
?? ?}
?? ?
?? ?public void displayBill(Vehicle[] v, int[] id, int days){ //print all the names and capacity of selected vehicles, and calculated the total rent
?? ??? ?double totalAmount=0; //total rent
?? ??? ?int totalPassenger=0, totalCargo=0; //total capacity for passenger and cargo
?? ??? ?String vPasName[]=new String[id.length]; //store all the names of selected coach
?? ??? ?String vCargoName[]=new String[id.length]; //store all the names of selected truck
?? ??? ?int j;
?? ??? ?int passNum=0,cargoNum=0; //the number of selected coach, and truck respectively
?? ??? ?
?? ??? ?
?? ??? ?for (int i=0;i<id.length;i++){
?? ??? ??? ?j = id[i]-1; //the subscript of vehicle array should be the printed number showing on the menu minus 1
?? ??? ??? ?totalAmount = totalAmount+v[j].getRental()*days; //get the total rent
?? ??? ??? ?
?? ??? ??? ?if (v[j] instanceof PasVehi){ //for coach, dealing with the coach way: put name in the coach name array, total number of passengers, counter++
?? ??? ??? ??? ?//String h = v[j].getName();
?? ??? ??? ??? ?vPasName[passNum]=v[j].getName();
?? ??? ??? ??? ?totalPassenger+=v[j].getPassenger();?? ??? ??? ??? ?
?? ??? ??? ??? ?passNum++;
?? ??? ??? ??? ?//flag[i]=1;
?? ??? ??? ?}else if (v[j] instanceof CargoVehi){ //for truck, dealing with the truck way: put name in the truck name array, total capacity, counter++
?? ??? ??? ??? ?vCargoName[cargoNum]=v[j].getName();
?? ??? ??? ??? ?totalCargo+=v[j].getCargo();
?? ??? ??? ??? ?cargoNum++;
?? ??? ??? ?}else if (v[j] instanceof PasCargoVehi) { //for pickup, dealing with both ways: put name both in truck name array and coach name array,
?? ??? ??? ??? ?vPasName[passNum]=v[j].getName();
?? ??? ??? ??? ?vCargoName[cargoNum]=v[j].getName();
?? ??? ??? ??? ?totalPassenger+=v[j].getPassenger();
?? ??? ??? ??? ?totalCargo+=v[j].getCargo();
?? ??? ??? ??? ?passNum++;
?? ??? ??? ??? ?cargoNum++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?if (passNum>=1){ //if there's more than one coach in costomer's choice, print all the vehicles and total capacity
?? ??? ??? ?this.displayVehicle(vPasName, 1, totalPassenger, passNum);?? ??? ??? ?
?? ??? ?}else{
?? ??? ??? ?System.out.println("You didn't choose any vehicle for passenger");//if no coach, print result
?? ??? ?}
?? ??? ?
?? ??? ?if (cargoNum>=1){ //if there's more than one truck in costomer's choice, print all the vehicles and total capacity
?? ??? ??? ?this.displayVehicle(vCargoName, 0, totalCargo, cargoNum);?? ??? ??? ?
?? ??? ?}else{
?? ??? ??? ?System.out.println("You didn't choose any vehicle for cargo"); //if no truck, print the result
?? ??? ?}
?? ??? ?System.out.println("You have to pay $"+totalAmount);//print the total rent
?? ??? ?
?? ?}
?? ?
?? ?public void displayVehicle(String[] vName, int flag, int total, int num){//print all the names of selected vehicle and the total capacity
?? ??? ?if (flag==1){
?? ??? ??? ?System.out.println("the Vehicles for loading passengers are: ");
?? ??? ?}else {
?? ??? ??? ?System.out.println("the Vehicles for loading cargo are: ");
?? ??? ?}
?? ??? ?
?? ??? ?for (int i=0;i<num;i++){
?? ??? ??? ?System.out.print(vName[i]+"\t");
?? ??? ?}
?? ??? ?if (flag==1){
?? ??? ??? ?System.out.println("the total number of loading passenger is "+total);
?? ??? ?}else {
?? ??? ??? ?System.out.println("the total number of loading cargo is "+total+"T");
?? ??? ?}
?? ??? ?
?? ??? ?
?? ?}
}
2016-06-11
剛學了第三季的內容,在代碼里加了異常處理,控制車輛數目和序號的輸入
2016-06-10
你想指教什么?