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

我的作业提交

//基类
package car_rent;

public abstract class Car {
    String name;
    int person;
    double price;
    double weight;
    //客车
    public Car(String name,int person,double price){
        this.name = name;
        this.person = person;
        this.price = price;
    }
    //货车
    public Car(String name,double weight,double price){
        this.name = name;
        this.weight = weight;
        this.price = price;
    }
    //皮卡
    public Car(String name,int person, double weight,double price){
        this.name = name;
        this.weight = weight;
        this.price = price;
        this.person = person;
    }
    public void showInfo(int num,int days){
        double totalAmount;
        totalAmount = num * days * price;
        System.out.println("您选择的车是:"+name+"价格是:"+price+"/天,数量为"+num+"辆,天数为"+days+"天,总计"+totalAmount+"元");
    }
    public abstract void show();
}

//客车类
public class PassengerCar extends Car {
    public PassengerCar(String name,int person,double price){
        super(name,person,price);
    }
    public void show(){
        System.out.println(name+" "+price+"/天,可以载人数:"+person);
    }
}
//卡车类
public class Truk extends Car {
    Truk(String name,double weight,double price){
        super(name,weight,price);
    }

    public void show(){
        System.out.println(name+" "+price+"/天,载重量为:"+weight+"吨");
    }
}
//皮卡类
public class Pickup extends Car {
    Pickup(String name,int person, double weight,double price){

        super(name,person,weight,price);
    }

    public void show(){
        System.out.println(name+" "+price+"/天,可载人数"+person+" 载重量为:"+weight+"吨");
    }
}
//入口
package car_rent;
import java.util.Scanner;

public class Index {
    public static void main(String[] args){
        Car[] cars = {
          new PassengerCar("马自达",6,150),
          new Pickup("皮卡雪",4,3,300),
          new Truk("东风重卡",20,350)
        };
        System.out.println("欢迎使用XXX租车系统");
        System.out.println("是否租车:1-是  0否");
        Scanner input1 = new Scanner(System.in);
        int xz = input1.nextInt();
        if(xz == 1){
            System.out.println("请选择租车类型:");
            for (int i=1;i<= cars.length;i++){
                System.out.print("编号:"+i+" ");
                cars[i-1].show();
            }
            System.out.println("请选择编号:");
            Scanner input2 = new Scanner(System.in);
            int carIndex = input2.nextInt();
            if(carIndex <= 0 || carIndex > cars.length){
                System.out.println("输入错误,退出系统");
                System.exit(0);
            }
            System.out.println("请选择数量:");
            Scanner input4 = new Scanner(System.in);
            int num = input4.nextInt();
            if(num <= 0 ){
                System.out.println("输入错误,退出系统");
                System.exit(0);
            }
            System.out.println("请选择租车天数:");
            Scanner input3 = new Scanner(System.in);
            int days = input3.nextInt();
            if(days <= 0){
                System.out.println("输入错误,退出系统");
                System.exit(0);
            }
            for (int i=1;i<= cars.length;i++){
                if(carIndex == i){
                    cars[i-1].showInfo(num,days);
                    break;
                }
            }



        }else {
            System.out.println("退出系统");
            System.exit(0);
        }
    }
}


正在回答

4 回答

没看懂那个exit(0)方法是哪来的

0 回复 有任何疑惑可以回复我~

学习了

0 回复 有任何疑惑可以回复我~

大佬写的好。学习了。

0 回复 有任何疑惑可以回复我~

继承还可以优化,父类的重载有些繁琐了,可以简化。还有就是,如果记录人数,最好用personNum之类的命名方式,person更偏向于类,而不是一个变量。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       529961    人
  • 解答问题       6086    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

我的作业提交

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信