手记

租车系统项目~~~~~~~~~

package com.Carrental;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        /*
         * 建立三个车的类别,其他车辆分别继承这三种车类,并且用数组保存车辆信息。
         * 
         */

        Car [] carsForRent = {new PassengerCar("奥迪A4", 500, 4),
                new PassengerCar("马自达6", 400, 4),
                new Pickup("皮卡雪6", 450, 4, 2),
                new Trunk("金龙", 800, 20),
                new PassengerCar("松花江", 400, 4),
                new Trunk("依威柯", 1000, 20)};
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎使用滴答租车系统");
        System.out.println("您是否需要租车: 1  是 0 否");
        String input = scanner.next();
        /*
         * 判断顾客是否需要租车,需要则进入循环选车,不需要则退出
         */
        if(input.equals("1")){
            System.out.println("您可租车的类型及其价目表");
            System.out.println("序号\t汽车名称\t租金\t容量");
            /*
             * 
             * for循环遍历carsForRent[]打印信息
             */

            for(int i=0;i<=carsForRent.length-1;i++){
                System.out.println(i+".\t"+carsForRent[i].name+"\t"+carsForRent[i].rent+
                        "元/天"+"\t"+"载货:"+carsForRent[i].carryCargo+"吨  "+"载人:"+carsForRent[i].manned+"个");
            }
            System.out.println("请输入您要租车的数量");
            int m = scanner.nextInt();//设置变量保存租车数量
            /*
             * 利用循环保存租车的重金额,载人量,载货量
             */

            int load = 0;//保存总载货量
            int money = 0;//保存总价钱
            int person = 0;//保存总载人量
            String N1 = "";//保存载人汽车名字
            String N2 = "";//保存载货汽车名字
            for(int i=0,j=1;i<m;i++,j++){
                System.out.println("请输出要租用的第"+j+"辆车的序号");
                int b = scanner.nextInt();
                money+=carsForRent[b].rent;
                load+=carsForRent[b].carryCargo;
                person+=carsForRent[b].manned;
                switch(b){
                case 0:
                case 1:
                case 4:
                    N1 = N1+"  "+carsForRent[b].name;
                    break;
                case 2:
                    N1 = N1+"  "+carsForRent[b].name;
                    N2 = N2+"  "+carsForRent[b].name;
                    break;
                case 3:
                case 5:
                    N2 = N2+"  "+carsForRent[b].name;
                    break;
                    }
            }
            System.out.println("请输入需要租用的天数");
            int day = scanner.nextInt();
            System.out.println("您的账单:");
            System.out.println("****可载人的车有");
            System.out.println(N1+"\t共载人"+person);
            System.out.println("****可载货的车有");
            System.out.println(N2+"共载货"+load);
            System.out.println("租车总金额"+money*day);
            System.out.println("感谢您的使用!!!!!");
        }else{
            System.out.println("感谢您的访问");
        }
    }

}
  • 所有车的父类
package com.Carrental;

public class Car {
    public String name;
    public int rent;
    public int manned;
    public int carryCargo;
    public Car() {
        super();
    }
    public Car(String name, int rent, int manned, int carryCargo) {
        super();
        this.name = name;
        this.rent = rent;
        this.manned = manned;
        this.carryCargo = carryCargo;
    }

}
  • 载人车辆类,继承Car父类
package com.Carrental;

public class PassengerCar extends Car{
    public PassengerCar(String name,int rent,int manned){
        this.name = name;
        this.rent = rent;
        this.manned = manned;
    }
}
  • 皮卡(既能载人又能载货)车辆类,继承Car类
package com.Carrental;

public class Pickup extends Car{
    public Pickup(String a,int b,int c,int d) {
        this.name = a;
        this.rent = b;
        this.manned = c;
        this.carryCargo = d;
    }

}
  • 载货车辆类,继承Car类
package com.Carrental;

public class Trunk extends Car{
    public Trunk(String name,int rent,int carryCargo){
        this.name = name;
        this.rent = rent;
        this.carryCargo = carryCargo;
    }
}
1人推荐
随时随地看视频
慕课网APP