• 租车系统


    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()+"	"+super.getName()+"	"+super.getRentPrice()+"元/天	"+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()+"	"+super.getName()+"	"+super.getRentPrice()+"元/天	"+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()+"	"+super.getName()+"	"+super.getRentPrice()+"元/天	"+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("序号	"+"车名	"+"价格	"+"容量");
                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+"人	"+"总货物为:"+totalcargo+"吨");    
                System.out.println("总金额为:"+totalprice+"元");
            break;
            }
            case 2:
                System.out.println("感谢使用该系统");
                System.exit(0);        
                break;
            default:
                System.out.println("输入有误");
                System.exit(0);        
                break;
            }                
        }
    }

    6、运行结果

  • 相关阅读:
    JavaScript事件模拟元素拖动
    瀑布流思路总结
    JavaScript模拟QQ签名(HTML5 contenteditable属性)
    烧录时发生:permission denied:'/dev/ttyUSB0'问题的解决
    学习笔记——make项目中克隆GitHub目录失败的解决
    学习笔记——ESP8266项目的例子编译时发生cannot find -lstdc++问题的解决
    ohmyzsh的安装过程中失败拒绝连接问题的解决
    对ESP8266的例子进行编译时报错check_python_dependencies的问题的解决
    ESP8266交叉编译环境变量设置
    ubuntu下安装ESP8266开发环境步骤中可能出现的问题及解决办法
  • 原文地址:https://www.cnblogs.com/qfdy123/p/10960031.html
Copyright © 2020-2023  润新知