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

慕课作业:菜鸟代码还希望大牛指正不足之处

标签:
Java

package com.java1996;
import java.util.InputMismatchException;
import java.util.Scanner;
/**

  • 答答租车系统
  • @author HBX
  • */
    public class CarSystem {
    public static void main(String[] args) {
    beginning();
    }

    public static void beginning(){
    Car c1=new Auto(1,"奥迪A4",500,4);
    Car c2=new Auto(2,"马自达6",400,4);
    Car c3=new Pcar(3,"皮卡雪6",450,4,2);
    Car c4=new Auto(4,"金龙",800,20);
    Car c5=new Truck(5,"松花江",400,4);
    Car c6=new Truck(6,"依维柯",1000,20);
    //存放车辆名字的数组
    String[] carName={c1.getName(),c2.getName(),c3.getName(),c4.getName(),
    c5.getName(),c6.getName()};

    //空载客车辆数组
    String[] auto=new String[6];
    //空载货车辆数组
    String[] truck=new String[6];
    int sumCarrying=0; //载客总数
    double sumCargoes=0;//载货总数
    double sumPrice=0;//租车价格总和
    int j=0;//用户选择汽车的序号
    int input=0;//用户选择租车的车辆数
    int days=0;//用户租车天数
    Scanner sc=new Scanner(System.in);
    while(true){
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1是 0否");
        sc=new Scanner(System.in);
        String str=sc.next();// 输入Int 会抛异常 为了方便输入字符串吧
        //判断用户输入的是否要租车
        if(str.equals("0")){
            System.out.println("呵呵");
            break;
        }else if(!str.equals("1")){
            System.out.println("别开玩笑了,请按提示输入");
            continue;
        }else{
            //开始
            System.out.println("您可租车的类型及价目表:");
            System.out.println("序号   汽车名称   租金         容量");
            System.out.println(c1.getCarNum()+".   "+c1.getName()+" "+c1.getPrice()+"元/天     载客"+c1.getCarrying()+"人");
            System.out.println(c2.getCarNum()+".  "+c2.getName()+" "+c2.getPrice()+"元/天     载客"+c2.getCarrying()+"人");
            System.out.println(c3.getCarNum()+".  "+c3.getName()+" "+c3.getPrice()+"元/天     载客"+c3.getCarrying()+"人 载货"+c3.getCargoes()+"吨");
            System.out.println(c4.getCarNum()+".  "+c4.getName()+"     "+c4.getPrice()+"元/天       载客"+c4.getCarrying()+"人");
            System.out.println(c5.getCarNum()+".  "+c5.getName()+" "+c5.getPrice()+"元/天       载货"+c5.getCargoes()+"吨");
            System.out.println(c6.getCarNum()+".  "+c6.getName()+" "+c6.getPrice()+"元/天     载货"+c6.getCargoes()+"吨");
            System.out.println("请输入您要租汽车的数量:");
            while(true){
                try{
                    input=sc.nextInt();
                    break;
                }catch(InputMismatchException i){
                    System.out.println("别开玩笑了,请输入数字");
                    sc.next();
                }
            }
            for(int i=0;i<input;i++){
                System.out.println("请输入第"+(i+1)+"辆车的序号:");
                while(true){
                    try{
                        j=sc.nextInt();
                        break;//循环输入直到用户输入对为止
                    }catch(InputMismatchException e){
                        System.out.println("别开玩笑了,请输入数字");
                        sc.next();//此方法可以返回键盘输入的下一个
                        }
                }
    
                switch(j){
                case 1:
                    auto[j-1]=carName[j-1];//把存放车辆名字的数组对应下标赋给载客车数组
                    sumCarrying+=c1.getCarrying();//选一次1车就加一次
                    sumPrice+=c1.getPrice();//同理价格也加一次
                    break;
                case 2:
                    auto[j-1]=carName[j-1];
                    sumCarrying+=c2.getCarrying();
                    sumPrice+=c2.getPrice();
                    break;
                case 3:
                    auto[j-1]=carName[j-1];
                    truck[j-1]=carName[j-1];
                    sumCarrying+=c3.getCarrying();
                    sumCargoes+=c3.getCargoes();
                    sumPrice+=c3.getPrice();
                    break;
                case 4:
                    auto[j-1]=carName[j-1];
                    sumCarrying+=c4.getCarrying();
                    sumPrice+=c4.getPrice();
                    break;
                case 5:
                    truck[j-1]=carName[j-1];
                    sumCargoes+=c5.getCargoes();
                    sumPrice+=c5.getPrice();
                    break;
                case 6:
                    truck[j-1]=carName[j-1];
                    sumCargoes+=c6.getCargoes();
                    sumPrice+=c6.getPrice();
                    break;
                default:
                    System.out.println("输入错误,没有这辆车!请重新输入");
                    i--;
                }
    
            }
    
            System.out.println("请输入租车天数:");
            while(true) {
                try{
                    days=sc.nextInt();
                    break;
                }catch(InputMismatchException e){
                    System.out.println("别开玩笑了,请输入数字");
                    sc.next();
                }
            }
            sumPrice=sumPrice*days;
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            for(int i=0;i<auto.length;i++){
                if(auto[i]==null){
                    continue;
                }
                System.out.print(auto[i]+" ");
            }
            System.out.print("共载人:"+sumCarrying+"人\n");
            System.out.println("***可载货的车有:");
            for(int i=0;i<truck.length;i++){
                //判断数组内为空的值不输出
                if(truck[i]==null){
                    continue;
                }
                System.out.print(truck[i]+" ");
            }
            System.out.print("共载货:"+sumCargoes+"吨\n");
            System.out.println("***租车总价格:"+sumPrice+"元");
    
            System.out.println("本次服务结束,请到前台办理手续,谢谢!");
            sc.close();
            break;
        }
    
    }

    }

}
/**

  • @author HBX
    所有车的父类
    /
    class Car{

    private String name;//车的名字
    private int carNum;//汽车编号
    private int price;//车的价格
    private int carrying;//载货量
    private int cargoes;//载客量

// public Car(int carNum,String name,int price,int carrying,int cargoes){
// this.name=name;
// this.price=price;
// this.carNum=carNum;
// this.carrying=carrying;
// this.cargoes=cargoes;
// }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCarNum() {
return carNum;
}
public void setCarNum(int carNum) {
this.carNum = carNum;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCarrying() {
return carrying;
}
public void setCarrying(int carrying) {
this.carrying = carrying;
}
public int getCargoes() {
return cargoes;
}
public void setCargoes(int cargoes) {
this.cargoes = cargoes;
}

}

class Auto extends Car{

public Auto(int carNum, String name, int price,int carrying) {
    super.setName(name);
    super.setCarNum(carNum);
    super.setPrice(price);
    super.setCarrying(carrying);
    //super(carNum, name, price);

}

}

class Truck extends Car{

public Truck(int carNum, String name, int price,int cargoes) {
    super.setName(name);
    super.setCarNum(carNum);
    super.setPrice(price);
    super.setCargoes(cargoes);
    //super(carNum, name, price);
}

}

class Pcar extends Car{

public Pcar(int carNum, String name, int price,int carrying,int cargoes) {
    super.setName(name);
    super.setCarNum(carNum);
    super.setPrice(price);
    super.setCarrying(carrying);
    super.setCargoes(cargoes);

}

}

点击查看更多内容
16人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消