继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门之租车系统程序

慕粉1633222867
关注TA
已关注
手记 2
粉丝 0
获赞 22
创建Car父类
package rentSystem;

public class Car {
    String name;  //车辆名称
    int rent;     //租金
    int capacity; //载客量
    int load;     //载货量
}
分别创建子类
package rentSystem;

public class PassengerCar extends Car {
    public PassengerCar(String name, int rent, int capacity){
        this.name = name;
        this.rent = rent;
        this.capacity = capacity;
    }
}
package rentSystem;

public class GoodsVan extends Car {
    public GoodsVan(String name, int rent,int load){
        this.name = name;
        this.rent = rent;
        this.load = load;
    }
}
package rentSystem;

public class PickUp extends Car {
    public PickUp(String name, int rent, int capacity, int load){
        this.name = name;
        this.rent = rent;
        this.capacity = capacity;
        this.load = load;
    }
}
租车系统主程序
package rentSystem;
import java.util.*;
public class carSystem {
    Car cars[] = {new PassengerCar("奥迪A4",500,4), new PassengerCar("马自达",400,4), new PassengerCar("金龙",800,20), new PickUp("皮卡雪",450,2,4), new GoodsVan("松花江",400,4), new GoodsVan("依维柯",1000,20)};
    int count; //租车数量
    int day;   //租车天数
    int money; //租车消费
    Scanner in = new Scanner(System.in);

    //询问是否要租车
    public void ask(){
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车:1是  0否");
        int select = in.nextInt();
        if(select == 0){
            in.close();
            System.exit(-1);
        }
    }

    //显示可租车辆信息
    public void show(){
        System.out.println("序号\t汽车名称\t租金\t容量");
        for(int i=0; i<cars.length; i++){
            System.out.print((i+1)+"\t"+cars[i].name+"\t"+cars[i].rent+"\t");
            if(cars[i].capacity != 0){
                System.out.print("载人:"+cars[i].capacity+"人 ");
            }
            if (cars[i].load != 0){
                System.out.print("载货:"+cars[i].load+"吨");
            }
            System.out.println();
        }
    }

    //顾客租车车辆信息
    public void rentMessage(){
        int capacitySum = 0;   //记录总载客量
        int loadSum = 0;       //记录总载货量
        String PassengerCar = "";
        String GoodsCar = "";
        System.out.println("请输入您要租车的数量:");
        count = in.nextInt();
        while(count <= 0){
            System.out.println("输入有误,请重新输入");
            count = in.nextInt();
        }
        for(int i=0; i<count; i++){
            System.out.println("您要租的第"+(i+1)+"辆车的序号");
            int label = in.nextInt();
            while(label<1 || label>6){
                System.out.println("输入有误,请重新输入");
                label = in.nextInt();
            }
            money += cars[label-1].rent;
            if(cars[label-1].capacity>0){
                capacitySum += cars[label-1].capacity;
                PassengerCar = PassengerCar + cars[label-1].name + "\t";
            }
            if(cars[label-1].load>0){
                loadSum += cars[label-1].load;
                GoodsCar = GoodsCar + cars[label-1].name + "\t";
            }
        }
        System.out.println("请输入租车的天数:");
        day = in.nextInt();
        money *= day;
        System.out.println("您的账单:");
        if(PassengerCar != ""){
            System.out.println("*可载人的车有:\n"+PassengerCar+"共载人:"+capacitySum+"人");
        }
        if(GoodsCar != ""){
        System.out.println("*可载货的车有:\n"+GoodsCar+"共载货:"+loadSum+"吨");
        }
        System.out.println("*租车总价格:"+money+"元");
    }

    //主函数
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        carSystem run = new carSystem();
        run.ask();
        run.show(); 
        run.rentMessage();
    }

}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP