为了账号安全,请及时绑定邮箱和手机立即绑定

java入门第二季答答租车,有不对的地方还请各位前辈指正,谢谢大家!

标签:
Java
package myCar;
/**
 * 父类
 * */
public abstract class Car  {
    //车型
    private String models;
    //品牌
    private String brand;
    //租金
    private float  baserent;
    //载货
    private float deadweight;
    //载人
    private int manned;
    //天数
    private int days;

    public String getModels() {
        return models;
    }

    public void setModels(String models) {
        this.models = models;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public float getBaserent() {
        return baserent;
    }

    public void setBaserent(float baserent) {
        this.baserent = baserent;
    }

    public float getDeadweight() {
        return deadweight;
    }

    public void setDeadweight(float deadweight) {
        this.deadweight = deadweight;
    }

    public int getManned() {
        return manned;
    }

    public void setManned(int manned) {
        this.manned = manned;
    }

    public int getDays() {
        return days;
    }

    public void setDays(int days) {
        this.days = days;
    }

    public abstract void  print();

    public abstract void print(int xuhao,int days);

}
package myCar;
/**
 * 用户租车信息
 * */
public class CarInformation {
    //租车序号
    private int number;
    //租车天数
    private int  days;
    public CarInformation(int number,int days) {
        // TODO Auto-generated constructor stub
        this.number =number;
        this.days = days;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public int getDays() {
        return days;
    }
    public void setDays(int days) {
        this.days = days;
    }
}
package myCar;
/**
 * 数据异常类
 * */  

public class Abnormaldata extends Exception  
{  
    public Abnormaldata(String msg)  
    {  
        super(msg);  
    }  
}   
package myCar;
/**
 * 客车类
 * */

public class PassengerCar extends Car  {
    public PassengerCar(String models,String brand,float baserent,int manned) {
        // TODO Auto-generated constructor stub
        this.setModels(models);
        this.setBaserent(baserent);
        this.setManned(manned);
        this.setBrand(brand); 
        //判断车辆数据是否异常
        if (manned==0||baserent==0||models==null||brand==null){
            try {
                throw new Abnormaldata("抱歉,客车类数据异常,请联系管理员!");
            } catch (Abnormaldata e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }
    }

    @Override
    //打印车辆信息
    public void print() {
        // TODO Auto-generated method stub
        System.out.print(this.getModels()+"\t"+this.getBrand()+"\t"+this.getBaserent()+"\t"+this.getManned());
        System.out.println();
    }

    //打印租车后的车辆
    public void print(int number, int days) {
        // TODO Auto-generated method stub
        System.out.println(number+"\t"+this.getModels()+"\t"+this.getBrand()+"\t"+this.getBaserent()+"\t"+null+"\t"+this.getManned()+"\t"+days);        
    }

}
package myCar;
/**
 * 皮卡类
 * */
public class PickupCar extends Car{
    public PickupCar(String models,String brand,float baserent,float deadweight,int manned)  {
        // TODO Auto-generated constructor stub
        this.setModels(models);
        this.setBaserent(baserent);
        this.setManned(manned);
        this.setBrand(brand); 
        this.setDeadweight(deadweight);
        if (manned==0||deadweight==0||baserent==0||models==null||brand==null){
            try {
                throw new Abnormaldata("抱歉,皮卡类数据异常,请联系管理员!");
            } catch (Abnormaldata e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }
    }

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.print(this.getModels()+"\t"+this.getBrand()+"\t"+this.getBaserent()+"\t"+this.getDeadweight()+"\t"+this.getManned());
        System.out.println();
    }

    @Override
    public void print(int number, int days) {
        // TODO Auto-generated method stub
        System.out.println(number+"\t"+this.getModels()+"\t"+this.getBrand()+"\t"+this.getBaserent()+"\t"+this.getDeadweight()+"\t"+this.getManned()+"\t"+days);        
    }

}
package myCar;
/**
 * 卡车类
 * */
public class TruckCar extends Car{
    public TruckCar(String models,String brand,float baserent,float deadweight)  {
        // TODO Auto-generated constructor stub
        this.setModels(models);
        this.setBaserent(baserent);
        this.setDeadweight(deadweight);
        this.setBrand(brand); 
        if (deadweight==0||baserent==0||models==null||brand==null){
            try {
                throw new Abnormaldata("抱歉,卡车类载货数据异常,请联系管理员!");
            } catch (Abnormaldata e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }
    }

    @Override
    public void print() {
        // TODO Auto-generated method stub
        System.out.print(this.getModels()+"\t"+this.getBrand()+"\t"+this.getBaserent()+"\t"+this.getDeadweight());
        System.out.println();
    }

    @Override
    public void print(int number, int days) {
        // TODO Auto-generated method stub
        System.out.println(number+"\t"+this.getModels()+"\t"+this.getBrand()+"\t"+this.getBaserent()+"\t"+this.getDeadweight()+"\t"+null+"\t"+days);        
    }

}
package myCar;

import java.util.Scanner;

public class Test {
    private  Scanner sr = new Scanner(System.in);

    private  void print(){
        System.out.println("序号"+"\t"+"车型"+"\t"+"品牌"+"\t"+"租金"+"\t"+"载货"+"\t"+"载人");
    }
    //租车信息
    private  Car[] Cardata ={new PassengerCar("客车","奥迪",150.5f,5),
            new PassengerCar("客车","金龙",1000,50),
            new TruckCar("货车","陕汽重卡",2500.5f,25),
            new TruckCar("货车","轻卡",250.2f,5),
            new PickupCar("皮卡","铃木皮卡",200.5f,1.5f,3),
            new PickupCar("皮卡","长城皮卡",195.5f,1.2f,3)
            };  

    //选车类别
    private  int[] filtering(int type){
        int [] number = new int[Cardata.length];
        switch (type) {
        case 1:
            System.out.println("序号"+"\t"+"车型"+"\t"+"品牌"+"\t"+"租金"+"\t"+"载人");
            for(int i=0;i<Cardata.length;i++){
                if(Cardata[i].getModels()=="客车"){
                System.out.print((i+1)+"\t");
                Cardata[i].print();
                number[i]=i;
                }
            }
            return number;
        case 2:
            System.out.println("序号"+"\t"+"车型"+"\t"+"品牌"+"\t"+"租金"+"\t"+"载货");
            for(int i=0;i<Cardata.length;i++){
                if(Cardata[i].getModels()=="货车"){
                System.out.print((i+1)+"\t");
                Cardata[i].print();
                number[i]=i;
                }
            }
            return number;
        case 3:
            System.out.println("序号"+"\t"+"车型"+"\t"+"品牌"+"\t"+"租金"+"\t"+"载货"+"\t"+"载人");
            for(int i=0;i<Cardata.length;i++){
                if(Cardata[i].getModels()=="皮卡"){
                System.out.print((i+1)+"\t");
                Cardata[i].print();
                number[i]=i;
                }
            }
            return number;
        default:
            print();
            for(int i=0;i<Cardata.length;i++){
                System.out.print((i+1)+"\t");
                System.out.println(Cardata[i].getModels()+"\t"+Cardata[i].getBrand()+"\t"+Cardata[i].getBaserent()+"\t"+Cardata[i].getDeadweight()+"\t"+Cardata[i].getManned());
                number[i]=i;
            }
        }
        return number;
    }
    //判断是否存在该租车序号
    public boolean contains( int[] array, int v ) {
        for ( int  e: array){
            if ( e == v)
                return true;
        }
        return false;
    }

    //打印租车清单和租车金额
    private void RentalListing(CarInformation[] Information){
        System.out.println("序号"+"\t"+"车型"+"\t"+"品牌"+"\t"+"租金"+"\t"+"载货"+"\t"+"载人"+"\t"+"租车天数");
        float totalrent = 0;
        for (int k=0;k<Information.length;k++){
            Cardata[Information[k].getNumber()-1].print((k+1),Information[k].getDays());
            totalrent = totalrent+Information[k].getDays()*Cardata[Information[k].getNumber()-1].getBaserent();
        }
        System.out.println("**租车总金额:"+totalrent);
    }

    public static void main(String[] args) {
        Test test =new Test();
        int input;
        while (true) {
            System.out.println("欢迎来到租车系统,您是否要租车?(1/0)");

            input = test.sr.nextInt();
            if (input==1) {
            System.out.println("请选择车类别:1-客车、2-货车、3-皮卡、4-全部");
            input = test.sr.nextInt();
            //获取当前类别的租车序号范围
            int [] Carnumber=test.filtering(input);
            while(true){
                System.out.println("请输入租车数量:");
                input = test.sr.nextInt();
                if (input ==0){
                    System.out.println("租车数量不能为零!");
                    continue;
                }
                for (int i = 0; i < input; i++) {
                    System.out.println("请输入第" + (i + 1) + "辆车的序号:");
                    int number = test.sr.nextInt();
                    if (!test.contains(Carnumber,number-1)){
                        System.out.println("租车序号不存在!");
                        break;
                    }
                    System.out.println("请输入租车天数:");
                    int days = test.sr.nextInt();
                    if (days==0){
                        System.out.println("租车天数不能为零!");
                        break;
                    }
                    //保存用户租车信息
                    CarInformation[] Information = {new CarInformation(number, days)};
                    if((i+1) ==input){
                        System.out.println("租车清单如下:");
                        test.RentalListing(Information);
                    }
                }
                break;
            }
            }else if (input==0){
                System.out.println("感谢使用本软件,再见!!");
                break;
            }else{
                System.out.println("命令输入错误,请重新输入!");
                continue;
            }
            break;
        }

    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消