Java 哒哒租车系统(控制台)

2019-06-15 00:00:00 控制台 租车 哒哒


哒哒租车系统

  1. Cars类为父类
  2. 重写Cars类toString方法
  3. 三种车型3个子类继承父类


Cars.java

package com.xk;

public class Cars {
    public int id;
    public String name;
    public float price;
    public float carryingCapacity;
    public int seatingCapacity;
    @Override
    public String toString() {
        if(seatingCapacity==0) {
            return id+".\t"+name+"\t"+price+"元/天\t载货"+carryingCapacity+"吨";
        }else if(carryingCapacity==0){
            return id+".\t"+name+"\t"+price+"元/天\t载客"+seatingCapacity+"人";
        }else{
            return id+".\t"+name+"\t"+price+"元/天\t载货"+carryingCapacity+"吨\t载客"+seatingCapacity+"人";
        }
    }
}


Truck.java

package com.xk;

public class Truck extends Cars{
    public Truck(int id,String name,float price,float carryingCapacity){
        this.id=id;
        this.name=name;
        this.price=price;
        this.carryingCapacity=carryingCapacity;
    }
}


Coach.java

package com.xk;

public class Coach extends Cars {
    public Coach(int id,String name,float price,int seatingCapacity){
        this.id=id;
        this.name=name;
        this.price=price;
        this.seatingCapacity=seatingCapacity;
    }
}


Pickup.java

package com.xk;

public class Pickup extends Cars {
    public Pickup(int id,String name,float price,float carryingCapacity,int seatingCapacity){
        this.id=id;
        this.name=name;
        this.price=price;
        this.carryingCapacity=carryingCapacity;
        this.seatingCapacity=seatingCapacity;
    }
}


Test.java

package com.xk;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cars Cars[] = {new Truck(1,"小货车",500f,3.5f),new Truck(2,"大货车",800f,7.8f),new Coach(3,"小客车",400f,12),new Coach(4,"大客车",900f,25),new Pickup(5,"小皮卡",600f,1.1f,4),new Pickup(6,"大皮卡",1100f,2.5f,6)};
        System.out.println("----------哒哒租车系统----------\n是否进行租车:1.是\t2.否");
        Scanner input = new Scanner(System.in);
        int in = input.nextInt();
        if(in==1) {
            System.out.println("序号\t车名\t租金\t\t容量\t");
            for(int i=0;i<6;i++) {
                System.out.println(Cars[i]);
            }
            float sum = 0.0f;
            float capacity1=0.0f;
            int capacity2=0;
            String[] car1=new String[6];
            String[] car2=new String[6];
            for(int i=1;;i++){
                System.out.print("请输入你要购买第"+ i +"辆车的序号:");
                int id=input.nextInt();
                System.out.print("请输入你要租用的数量:");
                int qty=input.nextInt();
                System.out.print("请输入你要租用的天数:");
                int day=input.nextInt();
                switch(id){
                case 1: sum+=qty*day*Cars[0].price;car1[i-1]=Cars[0].name;capacity1+=qty*Cars[0].carryingCapacity;break;
                case 2: sum+=qty*day*Cars[1].price;car1[i-1]=Cars[1].name;capacity1+=qty*Cars[1].carryingCapacity;break;
                case 3: sum+=qty*day*Cars[2].price;car2[i-1]=Cars[2].name;capacity2+=qty*Cars[2].seatingCapacity;break;
                case 4: sum+=qty*day*Cars[3].price;car2[i-1]=Cars[3].name;capacity2+=qty*Cars[3].seatingCapacity;break;
                case 5: sum+=qty*day*Cars[4].price;car1[i-1]=Cars[4].name;car2[i-1]=Cars[4].name;capacity1+=qty*Cars[4].carryingCapacity;capacity2+=qty*Cars[4].seatingCapacity;break;
                case 6: sum+=qty*day*Cars[5].price;car1[i-1]=Cars[5].name;car2[i-1]=Cars[5].name;capacity1+=qty*Cars[5].carryingCapacity;capacity2+=qty*Cars[5].seatingCapacity;break;
                default : System.out.print("输入错误,");i--;break;
                }
                System.out.println("是否继续租车:1.继续\t2.退出");
                int choose=input.nextInt();
                if(choose==2){
                    break;
                }
            }
            System.out.println("--------------------------\n租车成功,您的账单:\n可载货的车:");
            for(int i=0;i<car1.length;i++){
                 if(car1[i]==null){
                     continue;
                     }
                System.out.print(car1[i]+'\t');
            }
            System.out.println("共载货:"+capacity1+"吨\n可载人的车:");
            for(int i=0;i<car2.length;i++){
                if(car2[i]==null){
                     continue;
                     }
                System.out.print(car2[i]+'\t');
            }
            System.out.println("共载人:"+capacity2+"人\n租车共需付款:"+sum+"元");
        }else{
            System.out.println("已退出哒哒租车系统");
        }
        input.close();
    }
}


运行效果

《Java 哒哒租车系统(控制台)》

相关文章