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

dada租车——添加异常处理

ChainCat
关注TA
已关注
手记 3
粉丝 0
获赞 4
package abc;
public class Car {
    private String carName;//车名
    private int price;//日租金
    private int passenger;//载客量
    private int burden;//载货量
    public String getCarName(){//getter访问private修饰的变量
        return carName;
    }
    public void setCarName(String carName){//set访问private修饰的变量
        this.carName=carName;
    }
    public int getPassenger(){
        return passenger;
    }
    public void setPassenger(int passenger){
        this.passenger=passenger;
    }
    public int getPrice(){
        return price;
    }
    public void setPrice(int price){
        this.price=price;
    }
    public int getBurden(){
        return burden;
    }
    public void setBurden(int burden){
        this.burden=burden;
    }

}
package abc;

public class SmallCar extends Car{//客车类
    public SmallCar(String carName,int passenger,int price){//定义有参的构造方法
        this.setCarName(carName);//参数与父类的参数相同
        this.setPassenger(passenger);
        this.setPrice(price);
    }
    public String toString(){
        return getCarName()+"\t"+getPassenger()+"\t\t"+getPrice();
    }

}
package abc;

public class Turck extends Car{货车类
    public Turck(String carName,int burden,int price){//定义有参的构造方法
        this.setCarName(carName);//参数与父类的参数相同
        this.setPrice(price);
        this.setBurden(burden);
    }
    public String toString(){
        return getCarName()+"\t\t"+getBurden()+"\t"+getPrice();
    }

}
package abc;

public class Pickup extends Car{//皮卡
    public Pickup(String carName,int passenger,int burden,int price){//定义有参的构造方法
        this.setCarName(carName);//参数与父类的参数相同
        this.setPrice(price);
        this.setPassenger(passenger);
        this.setBurden(burden);
    }
    public String toString(){
        return getCarName()+"\t"+getPassenger()+"\t"+getBurden()+"\t"+getPrice();
    }

}
package abc;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestRent {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        TestException te=new TestException();
        Car[] cars={new SmallCar("奥迪A4",4,500),//调用有参的构造方法
                new SmallCar("马自达6",4,400),
                new SmallCar("金龙",20,800),
                new Pickup("皮卡雪6",4,2,450),
                new Turck("松花江",4,400),
                new Turck("依维柯",20,1000)};
        System.out.println("*******欢迎来到答答租车*******");
        System.out.print("您是否要租车,输入1确定,输入0退出:");
        try{
            int select=input.nextInt();//检查输入是否有异常
            if(select==0)       System.out.println("****谢谢!****");//输入不为1,结束程序
            else if(select==1){
                System.out.println("****您可以租车的类型及其价目表:");
                System.out.println("序号\t车名\t载客量(人)\t载货量(吨)\t日租金(元/天)");
                for(int i=0;i<cars.length;i++)              System.out.println((i+1)+"\t"+cars[i]);//遍历数组打印租车的类型及其价目表
                System.out.print("****您需要租车的数量为:");
                try{
                    int number=input.nextInt();
                    Car[] choose=new Car[number];//定义新数组储存选中的车
                    boolean b=false;
                    for(int i=0;i<choose.length;i++){
                        System.out.print("请输入第"+(i+1)+"辆车的序号:");
                        try{
                            int sequence=input.nextInt();//检查输入是否有异常
                            if(sequence>0&&sequence<=6)     choose[i]=cars[sequence-1];
                            else                            throw new MistakeException();
                        }catch(MistakeException e){
                            System.out.println("您输入的数应在1~6内!~~~");
                            b=true;
                            break;
                        }catch(InputMismatchException e){
                            System.out.println("您应该输入整数!~~~");
                            b=true;
                            break;
                        }
                    }if(b)                                  System.out.println("出现错误,程序结束!~~~");
                    else{
                        System.out.print("您要租车的天数:");
                        try{
                            int days=input.nextInt();
                            int totalPrice=0,totalPassenger=0,totalBurden=0;//定义变量存放总金额,总载人数,总载货数
                            for(int i=0;i<choose.length;i++){
                                totalPassenger+=choose[i].getPassenger();//数组遍历计算最后的金额,载人数,载货数
                                totalBurden+=choose[i].getBurden();
                                totalPrice+=choose[i].getPrice()*days;
                            }
                            System.out.println("****您的账单如下****");
                            System.out.println("****已选客车:");
                            for(int i=0;i<choose.length;i++){
                                if(choose[i].getPassenger()!=0)     System.out.println(choose[i].getCarName());//载人数不为0为客车
                            }
                            System.out.println("****已选货车:");
                            for(int i=0;i<choose.length;i++){
                                if(choose[i].getBurden()!=0)        System.out.println(choose[i].getCarName());//载货数不为0为货车
                            }
                            System.out.println("可载:"+totalPassenger+"人");
                            System.out.println("可载货:"+totalBurden+"吨");
                            System.out.println("总金额为:"+totalPrice+"元");
                            System.out.println("*******感谢您的使用!*******");
                        }catch(InputMismatchException e){
                            System.out.println("您应该输入整数!~~~");
                        }
                    }
                }catch(InputMismatchException e){
                    System.out.println("您应该输入整数!~~~");
                }
            }else                                       te.mistake();
        }catch(MistakeException e){
            System.out.println("您输入的指令错误!应该输入1或0~~~");
        }catch(InputMismatchException e){
            System.out.println("您应该输入整数!~~~");
        }   
    }
}
package abc;
public class MistakeException extends Exception{//自定义输入指令错误异常
    public MistakeException(){}
    public MistakeException(String message){
        super(message);
    }
}
package abc;
public class TestException {//定义类提供抛出异常方法
    public void mistake()throws MistakeException{
        throw new MistakeException();
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP