手记

答答租车系统---借鉴后写出的

Car.java

package com.imooc;

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

Bus.java

package com.imooc;

public class Bus extends Car{
    public Bus(int id,String name,float price,int capPerson){
        this.id=id;
        this.name=name;
        this.price=price;
        this.capPerson=capPerson;
    }
}

Truck.java

package com.imooc;

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

Pika.java

package com.imooc;

public class Pika extends Car {
    public Pika(int id,String name,float price,float capCargo,int capPerson){
        this.id=id;
        this.name=name;
        this.price=price;
        this.capCargo=capCargo;
        this.capPerson=capPerson;
    }
}

Test.java

package com.imooc;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car cars[]={new Truck(1, "小货车", 200f, 3.5f),new Truck(2, "大货车", 500f, 7f),
                new Bus(3, "小客车", 300f, 12),new Bus(4, "大客车", 600f, 30),
                new Pika(5, "小皮卡车", 400f, 2.5f,8),new Pika(6, "大皮卡车", 800f, 5f, 16)
        };
        System.out.println("----------答答租车系统----------");
        System.out.println("是否进行租车:1.是\t2.否");
        Scanner input = new Scanner(System.in);
        int in=input.nextInt();
        if(in==1){
            System.out.println("序号\t车名\t租金\t容量");
            for(Car car:cars){
                System.out.println(car.toString());
            }
            float priceSum=0;
            float capCargoSum=0;
            int capPersonSum=0;
            String capCargoName = "";
            String capPersonName = "";
            for(int i=1;;i++){
                System.out.print("请输入您要租的第"+i+"辆车的序号:");
                int id=input.nextInt();
                System.out.print("请输入您要租用的数量:");
                int num = input.nextInt();
                System.out.print("请输入您要租用的天数:");
                int day = input.nextInt();
                priceSum+=cars[id-1].price*num*day;
                if(id==1||id==2||id==5||id==6){
                    capCargoName += cars[id-1].name+"*"+num+"\t";
                    capCargoSum+=cars[id-1].capCargo*num;
                }
                if(id==3||id==4||id==5||id==6){
                    capPersonName += cars[id-1].name+"*"+num+"\t";
                    capPersonSum+=cars[id-1].capPerson*num;
                }
                System.out.println("是否继续租车:1.继续     2.退出");
                int choose = input.nextInt();
                if(choose==2) break;
            }
            System.out.println("-------------------");
            System.out.println("租车成功,您的账单:");
            System.out.println("可载货的车:");
            System.out.print(capCargoName);
            System.out.println("共载货:"+capCargoSum+"吨");
            System.out.println("可载人的车:");
            System.out.print(capPersonName);
            System.out.println("共载人:"+capPersonSum+"人");
            System.out.println("租车共需付款:"+priceSum+"元"); 
        }else{
            System.out.println("已退出答答租车系统");
        }
        input.close();
    }
}
1人推荐
随时随地看视频
慕课网APP