java第二季6.1综合练习


import java.util.Scanner;
public class Text1 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        Car[] carforrent={new CarryPeople("奥迪A4",500,4),
                          new CarryPeople("马自达", 400, 4),
                          new CarryC_P("皮卡雪6", 450, 4, 2),
                          new CarryPeople("金龍", 800, 20),
                          new CarryCargo("松花江", 400, 4),
                          new CarryCargo("依維柯", 1000, 20)};
        String question="T";
        Car car=new Car();
        do{
        System.out.println("欢迎使用達達租車系統!");
        System.out.println("您是否要租车?(T/F)");
        String choice=input.next();
        if(choice.equalsIgnoreCase("T")){
            System.out.println("序号\t"+"汽车名称\t"+"租金\t\t"+"容量");
            int i=1;
            for(Car c:carforrent){
                System.out.print(i+".\t");
                if(c instanceof CarryCargo){
                ((CarryCargo)c).information();}
                if(c instanceof CarryPeople){
                ((CarryPeople)c).information();}
                if(c instanceof CarryC_P){
                ((CarryC_P)c).information();}
            //不能用c.information()因为c是属于Car数组的一个变量而已
                i++;
            }
            System.out.println("請輸入要租用車輛的數量:");
            int carnumber=input.nextInt();
            int N[]=new int[carnumber];
            for(int j=0;j<carnumber;j++){
                System.out.println("請輸入第"+(j+1)+"輛租用車輛:");
                N[j] = input.nextInt();
            }
            System.out.println("請輸入組訂的天數:");
            int days=input.nextInt();
            System.out.println("您的訂單為:\n"+"***************可載人車輛***************");
            String message=""; 
            int peototal=0;
            for(int a=0;a<N.length;a++){
                if(carforrent[N[a]-1] instanceof CarryCargo){
                }
                if(carforrent[N[a]-1] instanceof CarryPeople){
                message+=((CarryPeople) carforrent[N[a]-1]).getName()+" ";
                peototal+=((CarryPeople) carforrent[N[a]-1]).numofpeo;
                } 
                if(carforrent[N[a]-1] instanceof CarryC_P){
                message+=((CarryC_P) carforrent[N[a]-1]).getName()+" ";
                peototal+=((CarryC_P) carforrent[N[a]-1]).numofpeo;
                }
            }
            System.out.println(message+" 共載人:"+peototal);
            System.out.println("***************可載貨車輛***************");
            message="";
            int cargototal=0;
            for(int a=0;a<N.length;a++){
                if(carforrent[N[a]-1] instanceof CarryCargo){
                message=message+((CarryCargo) carforrent[N[a]-1]).getName()+" ";
                cargototal+=((CarryCargo) carforrent[N[a]-1]).cargo;
                }
                if(carforrent[N[a]-1] instanceof CarryPeople){

                } 
                if(carforrent[N[a]-1] instanceof CarryC_P){
                message=message+((CarryC_P) carforrent[N[a]-1]).getName()+" ";
                cargototal+=((CarryC_P) carforrent[N[a]-1]).cargo;
                }
            }
            System.out.println(message+" 共載貨:"+cargototal);
            System.out.println("總租金為:");
            double renttotal=0;
            for(int a=0;a<N.length;a++){
                if(carforrent[N[a]-1] instanceof CarryCargo){
                    renttotal+=((CarryCargo) carforrent[N[a]-1]).getRentMoney()*days;
                }
                if(carforrent[N[a]-1] instanceof CarryPeople){
                    renttotal+=((CarryPeople) carforrent[N[a]-1]).getRentMoney()*days;
                } 
                if(carforrent[N[a]-1] instanceof CarryC_P){
                    renttotal+=((CarryC_P) carforrent[N[a]-1]).getRentMoney()*days;
                }
            }
            System.out.println(renttotal);
            System.out.println("您還想再次使用達達租車系統嗎?(T/F)");
            question=input.next();
        }
        else {
            question="F";
            System.out.print("再见!");
        }

        }while(question.equalsIgnoreCase("T"));
        System.out.println("達達租車:謝謝您的光臨,祝您生活愉快!");
    }
   }

public class Car {
  protected  String name;
  protected double money;
  protected int cargo,numofpeo;
  Car(){
      }
 void setName(String name) {
     this.name=name;
}
 String getName() {
    return name;
}
 void setRentMoney(double money) {
     this.money=money;
}
double getRentMoney() {
    return money;
}
}   

public class CarryCargo extends Car {
    public CarryCargo(String name,double money,int cargo) {
     this.name=name;
     this.money=money;
     this.cargo=cargo;
    }
    void information(){
        System.out.println(name + "\t" + money + "元一天\t" + "载货:" + cargo + "吨");
    }

    void setCargo(int cargo){
        this.cargo=cargo;
    }
    int getCargo(){
    return cargo;   
    }
}

public class CarryPeople extends Car {
    public CarryPeople(String name, double money, int numofpeo) {
        this.name = name;
        this.money = money;
        this.numofpeo = numofpeo;
    }

    void information() {
        System.out.println(name + "\t" + money + "元一天\t" + "载人:" + numofpeo
                + "人");
    }

    void setNumofpeo(int numofpeo) {
        this.numofpeo = numofpeo;
    }

    int getNumofpeo() {
        return numofpeo;
    }

}

public class CarryC_P extends Car {
    public CarryC_P(String name, double money, int numofpeo, int cargo) {
        this.name = name;
        this.money = money;
        this.numofpeo = numofpeo;
        this.cargo = cargo;
    }

    void information() {
        System.out.println(name + "\t" + money + "元一天\t" + "载人:" + numofpeo
                + "人 载货:" + cargo + "吨");
    }

    void setNumofpeo(int numofpeo) {
        this.numofpeo = numofpeo;
    }

    int getNumofpeo() {
        return numofpeo;
    }

    void setCargo(int cargo) {
        this.cargo = cargo;
    }

    int getCargo() {
        return cargo;
    }
}
点赞