为了账号安全,请及时绑定邮箱和手机立即绑定

java第二季6.1综合练习

标签:
Java

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;
    }
}
点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消