美文网首页
租车系统

租车系统

作者: 清波ss | 来源:发表于2018-07-25 10:47 被阅读0次

java练习2:
java实现模拟借书系统:
可实现:
(基本功能)
1.展示可租车辆
2.选择车型、租车量
3.展示租车清单,包含:总金额、总载货量及其车型、总载人量及其车型
(异常处理)
1.输入类型错误,输出“are you kidding me?”,提示重新输入
2.超过车辆库的范围,输出“序号超出范围”,提示重新输入
3.对于不符合常识的数量和日期,输出“请输入大于或等于1的整数!”,提示重新输入

界面:


图片1.png
public class Car {
    private int seq;
    private String name;
    private int money;
    public int getSeq() {
        return seq;
    }
    public void setSeq(int seq) {
        this.seq = seq;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
}
public class RenCar extends Car {
    private int people;
    public RenCar(int seq,String name,int money,int people) {
        this.setSeq(seq);
        this.setName(name);
        this.setMoney(money);
        this.people=people;
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }
}
public class picar extends Car {
    private int people;
    private int thing;
    public picar(int seq,String name,int money,int people,int thing) {
        this.setSeq(seq);
        this.setName(name);
        this.setMoney(money);
        this.people=people;
        this.thing=thing;
    }
    public int getPeople() {
        return people;
    }
    public void setPeople(int people) {
        this.people = people;
    }
    public int getThing() {
        return thing;
    }
    public void setThing(int thing) {
        this.thing = thing;
    }
}
public class Trunk extends Car {
    private int thing;
    public Trunk(int seq,String name,int money,int thing) {
        this.setSeq(seq);
        this.setName(name);
        this.setMoney(money);
        this.thing=thing;
    }
    public int getThing() {
        return thing;
    }
    public void setThing(int thing) {
        this.thing = thing;
    }
}
import java.util.Scanner;
public class Initail {
    private static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //车辆信息
        Car[] cars= {new RenCar(1,"奥迪A4",500,4),new RenCar(2,"马自达6",400,4),new picar(3,"皮卡雪6",450,4,2),new RenCar(4,"金龙",800,20),new Trunk(5,"松花江",400,4),new Trunk(6,"依维柯",1000,20)};
        System.out.println("******欢迎使用哒哒租车系统:");
        System.out.println("*****您是否要租车:1是  0否;");
        while(true) {
            int a1=selectNum();
            switch(a1) {
                case 1://租车 
                 // 租车表显示
                    System.out.println("****您可租车的类型及其价目表:");
                    System.out.println("序号"+"\t汽车名称"+"\t租金(元)"+"\t载人(人)"+"\t载货(吨)");
                    for(Car car:cars) {
                        if(car instanceof RenCar) {
                            System.out.println(car.getSeq()+"\t"+car.getName()+"\t"
                        +car.getMoney()+"\t"+((RenCar) car).getPeople());
                        }else if(car instanceof picar) {
                            System.out.println(car.getSeq()+"\t"+car.getName()+"\t"
                        +car.getMoney()+"\t"+((picar) car).getPeople()+"\t"+((picar) car).getThing());
                        }else {
                            System.out.println(car.getSeq()+"\t"+car.getName()+"\t"
                        +car.getMoney()+"\t"+"\t"+((Trunk) car).getThing());
                        }
                    }
                        //开始租车
                    System.out.println("****请输入您要租汽车的数量:");
                    int a2=selectDateOrNumber();
                    int[] seq=new int[a2];
                    for(int i=0;i<a2;i++) {
                        System.out.println("****请输入第"+(i+1)+"辆车的序号:");
                        seq[i]=selectSeq(cars)-1; 
                    }
                    System.out.println("****请输入租车天数:");
                    int date=selectDateOrNumber();
                    System.out.println("****您的账单:");
                    System.out.println("*****可载人的车有:");
                    int price=0;
                    int zairen=0;
                    int zaihuo=0;
                    for(int j=0;j<a2;j++) {
                        if(cars[seq[j]] instanceof RenCar) {
                            System.out.print(cars[seq[j]].getName()+"  ");
                            zairen+=((RenCar) cars[seq[j]]).getPeople();
                        }
                        if(cars[seq[j]] instanceof picar) {
                            System.out.print(cars[j].getName()+"  ");
                            zairen+=((picar) cars[seq[j]]).getPeople();
                        }
                            price+=cars[seq[j]].getMoney()*date;
                        }
                    System.out.println("共载人:"+zairen+"人");
                    System.out.println("*****可载货的车有:");
                    for(int k=0;k<a2;k++) {
                        if(cars[seq[k]] instanceof Trunk) {
                            System.out.print(cars[seq[k]].getName()+"  ");
                            zaihuo+=((Trunk) cars[seq[k]]).getThing();
                        }
                        if(cars[seq[k]] instanceof picar) {
                            System.out.print(cars[seq[k]].getName()+"  ");
                            zaihuo+=((picar) cars[seq[k]]).getThing();
                        }
                    }
                    System.out.println("共载货:"+zaihuo+"吨");
                    System.out.println("*****租车总金额:"+price+"元");
                    System.out.println("期待您下次使用!");
                    break;
                case 0:
                    //不租车
                    System.out.println("祝您生活愉快!");
                    break;
                case -1:
                    //输入了字符串或小数
                    System.out.println("Are you kidding me???!");
                    continue;
                default:
                    //输入其他数字均为错误
                    System.out.println("??请输入正确的选择!");
                    continue;
            }
            break;
        }
    }

    //数字型输入
    public static int selectNum() {
        int number;
        try {
            number=sc.nextInt();
        } catch (Exception e) {
            // 输入字符型的异常
            sc=new Scanner(System.in);
            number=-1;
        }
        return number;
    }
    
    // 日期和租车数量的处理
    public static int selectDateOrNumber() {
        while(true) {
            int date=selectNum();
            if(date<1) {
                System.out.print("请输入大于或等于1的整数!");
                continue;
            }
            return date;
        }
    }
    
    //租车序号处理
    public static int selectSeq(Car[] cars){
        while(true) {
                int seq=selectDateOrNumber();
                if(seq>cars.length) {
                    System.out.print("序号超出范围!");
                    continue;
                }
                int number=cars[seq-1].getSeq();
                return number;
        }
    }
}

相关文章

网友评论

      本文标题:租车系统

      本文链接:https://www.haomeiwen.com/subject/nisemftx.html