租车系统

1、父类

package Rent;
//交通工具类
public class Vehicle {
    private int id;
    private String name;
    private int rentPrice;
    public Vehicle(int id,String name,int rentPrice) {
        this.id=id;
        this.name=name;
        this.rentPrice=rentPrice;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRentPrice() {
        return rentPrice;
    }
    public void setRentPrice(int rentPrice) {
        this.rentPrice = rentPrice;
    }
}

2、载客车类

package Rent;
//载客车
public class Bus extends Vehicle{
    private int peopleNum;
    public Bus(int id,String name,int rentPrice,int peopleNum){
        super(id,name,rentPrice);        
        this.peopleNum=peopleNum;        
    }
    public void setPeopleNum(int peopleNum) {
        this.peopleNum = peopleNum;
    }
    public int getPeopleNum() {
        return peopleNum;
    }
    public String toString() {//返回一个子符串,实例化后打印该类的对象时会出现这个指定返回的字符串 System.out.println(bus);
        return super.getId()+"\t"+super.getName()+"\t"+super.getRentPrice()+"元/天\t"+peopleNum+"人";
            }
}

3、载货车类

package Rent;
//载货车
public class Trunk extends Vehicle{
    private int cargoNum;
    public Trunk(int id,String name,int rentPrice,int cargoNum){
        super(id,name,rentPrice);
        this.cargoNum=cargoNum;        
    }
    public int getCargoNum() {
        return cargoNum;
    }
    public void setCargoNum(int cargoNum) {
        this.cargoNum = cargoNum;
    }
    public String toString() {
        return super.getId()+"\t"+super.getName()+"\t"+super.getRentPrice()+"元/天\t"+cargoNum+"吨";
            }
}

4、载人载货车类

package Rent;
//载人载货车
public class Bustrunk extends Vehicle{
    private int peopleNum;
    private int cargoNum;
    public Bustrunk(int id,String name,int rentPrice,int peopleNum,int cargoNum){
        super(id,name,rentPrice);
        this.peopleNum=peopleNum;
        this.cargoNum=cargoNum;       
    }
    public void setPeopleNum(int peopleNum) {
        this.peopleNum = peopleNum;
    }
    public int getPeopleNum() {
        return peopleNum;
    }
    public void setCargoNum(int cargoNum) {
        this.cargoNum = cargoNum;
    }
    public int getCargoNum() {
        return cargoNum;
    }
    public String toString() {
        return super.getId()+"\t"+super.getName()+"\t"+super.getRentPrice()+"元/天\t"+peopleNum+"人"+cargoNum+"吨";
    }
}

5、测试类

package Rent;
import java.util.*;public class Test {
    public static void main(String[] args) {
        Bus bus=new Bus(1,"奥迪",500,4);
        Trunk trunk=new Trunk(2,"东风",300,2);
        Bustrunk btrunk=new Bustrunk(3,"路虎",600,5,1);
        Vehicle vehicle[]={bus,trunk,btrunk};
        Scanner sc=new Scanner(System.in);
        System.out.println("欢迎光临租车系统");
        System.out.println("1租车  2不租车");
        int n=sc.nextInt();
        switch (n) {
        case 1:{
            System.out.println("序号\t"+"车名\t"+"价格\t"+"容量");
            for (int i = 0; i < vehicle.length; i++) {    
                System.out.println(vehicle[i]);
            }
            System.out.println("请输入要租用车的数量:");
            int total=sc.nextInt();
            int totalprice=0;
            int totalpeople=0;
            int totalcargo=0;
            int p=0;
            int c=0;
            Vehicle people[]=new Vehicle[total];
            Vehicle cargo[]=new Vehicle[total];
            for (int i = 1; i <=total; i++) 
            {
                int busPeople=0;
                int trunkCargo=0;
                int btrunkPeople=0;
                int btrunkCargo=0;
                int price=0;                
                System.out.println("请输入第"+i+"辆车的编号");
                int id=sc.nextInt();
                System.out.println("请输入租用的天数:");
                int day=sc.nextInt();
                price=vehicle[id-1].getRentPrice()*day;
                totalprice+=price;                
                if(vehicle[id-1] instanceof Bus){
                    busPeople=bus.getPeopleNum();
                    people[p++]=vehicle[id-1];                    
                }
                else if(vehicle[id-1] instanceof Trunk){
                     trunkCargo=trunk.getCargoNum();
                     cargo[c++]=vehicle[id-1];                     
                }
                else{
                    btrunkCargo=btrunk.getCargoNum();
                    btrunkPeople=btrunk.getPeopleNum();    
                    people[p++]=vehicle[id-1];
                    cargo[c++]=vehicle[id-1];
                }
                totalpeople+=busPeople+btrunkPeople;
                totalcargo+=trunkCargo+btrunkCargo;
            }
            System.out.println("");
            System.out.println("***************您的账单*******************");
            System.out.print("载人的车:");    for (int i = 0; i < p; i++) {               
                System.out.print(people[i].getName()+" ");        
            }
            System.out.println("");
            System.out.print("载货的车:");            
            for (int i = 0; i <c; i++) {
                System.out.print(cargo[i].getName()+" ");           
            }
            System.out.println("");
            System.out.println("此次能载总人数为:"+totalpeople+"人\t"+"总货物为:"+totalcargo+"吨");    
            System.out.println("总金额为:"+totalprice+"元");
        break;
        }
        case 2:
            System.out.println("感谢使用该系统");
            System.exit(0);        
            break;
        default:
            System.out.println("输入有误");
            System.exit(0);        
            break;
        }                
    }
}

6、运行结果

 

转载于:https://www.cnblogs.com/qfdy123/p/10960031.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值