ITPub博客

首页 > IT职业 > IT生活 > Java入门第二季——答答租车系统

Java入门第二季——答答租车系统

IT生活 作者:jhere 时间:2021-09-09 08:58:00 0 删除 编辑

我写的这个系统注释很详细,应该很适合小白参考,经运行没有任何问题,并且运用了面向对象的三大特性封装、继承和多态(在代码里也都有注释),欢迎参考,也希望大家能提出宝贵的意见,一起学习,共同进步!

图片描述

创建父类Car,子类PassengerCar,Pickup,Truck,详见代码。
父类Car:

package com.imooc;

public class Car {
    //描述汽车可能有特征
    public String name;//车的名称
    public double cargoCapacity;//车的载货量
    public int busLoad;//车的载客量
    public int dailyRent;//车的日租金

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
    public int getBusLoad() {
        return busLoad;
    }
    public void setBusLoad(int busLoad) {
        this.busLoad = busLoad;
    }
    public int getDailyRent() {
        return dailyRent;
    }
    public void setDailyRent(int dailyRent) {
        this.dailyRent = dailyRent;
    }   
}

子类:小汽车PassengerCar

package com.imooc;

public class PassengerCar extends Car {//使用面向对象的继承特性
    //使用面向对象的封装特性
    private String name;
    private int busLoad;
    private int dailyRent;
    //客车的构造方法
    public PassengerCar(String name,int busLoad,int dailyRent){
        this.name=name;
        this.busLoad=busLoad;
        this.dailyRent=dailyRent;
    }
    //自动生成getter和setter函数
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getBusLoad() {
        return busLoad;
    }
    public void setBusLoad(int busLoad) {
        this.busLoad = busLoad;
    }
    public int getDailyRent() {
        return dailyRent;
    }
    public void setDailyRent(int dailyRent) {
        this.dailyRent = dailyRent;
    }
}

子类:皮卡Pickup

package com.imooc;

public class Pickup extends Car {
    private String name;
    private double cargoCapacity;
    private int busLoad;
    private int dailyRent;

    public Pickup(String name,double cargoCapacity,int busLoad,int dailyRent){
        this.name=name;
        this.cargoCapacity=cargoCapacity;
        this.busLoad=busLoad;
        this.dailyRent=dailyRent;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
    public int getBusLoad() {
        return busLoad;
    }
    public void setBusLoad(int busLoad) {
        this.busLoad = busLoad;
    }
    public int getDailyRent() {
        return dailyRent;
    }
    public void setDailyRent(int dailyRent) {
        this.dailyRent = dailyRent;
    }

}

子类:卡车Truck

package com.imooc;

public class Truck extends Car {
    private String name;
    private double cargoCapacity;
    private int dailyRent;

    public Truck(String name,double cargoCapacity,int dailyRent){
        this.name=name;
        this.cargoCapacity=cargoCapacity;
        this.dailyRent=dailyRent;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getCargoCapacity() {
        return cargoCapacity;
    }
    public void setCargoCapacity(double cargoCapacity) {
        this.cargoCapacity = cargoCapacity;
    }
    public int getDailyRent() {
        return dailyRent;
    }
    public void setDailyRent(int dailyRent) {
        this.dailyRent = dailyRent;
    }
}

主程序:Test类

package com.imooc;
import java.util.Scanner;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎使用答答租车系统!");
        System.out.println("您是否需要租车:1是  0否");
        Scanner input1=new Scanner(System.in);
        int is=input1.nextInt();

        //创建一个Car类数组来保存各种车,利用引用多态来创建不同的子类对象
        Car[] cars={new PassengerCar("奥迪A4",5,500),
                    new Truck("小货车",5,150),
                    new PassengerCar("奔驰E400",5,700),
                    new Pickup("皮卡",1.5,4,200),
                    new Truck("大型货车",10,600),
                    new PassengerCar("大客车",25,650)          
        };

        //输入1,需要租车
        if(is==1){
            int totalMoney=0;
            double totalcargoCapacity=0;
            int totalbusLoad=0;
            int rentCarDays=0;

            System.out.println("您可租车的类型极其价目表:");
            System.out.println("序号t"+"汽车名称t"+"日租金t"+"容量t");
            int num=1;//定义初始序号
            for(Car currentCar:cars){//对所有车进行一次循环遍历,即foreach(),来创建一个汽车一览表
                if(currentCar instanceof PassengerCar){//判断正在遍历的car是否是Passenger类的一个实例
                    System.out.println(
                            "No."+num+'t'+currentCar.getName()+'t'//封装后只能通过get()方法来获取值
                            +currentCar.getDailyRent()+"元/天t"
                            +"载人:"+currentCar.getBusLoad()+"人");
                    num++;
                }
                if(currentCar instanceof Truck){//判断正在遍历的car是否是Truck类的一个实例
                    System.out.println(
                            "No."+num+'t'+currentCar.getName()+'t'
                            +currentCar.getDailyRent()+"元/天t"
                            +"载货:"+currentCar.getCargoCapacity()+"吨");
                    num++;                      
                } 
                if(currentCar instanceof Pickup){//判断正在遍历的car是否是Pickup类的一个实例
                    System.out.println(
                            "No."+num+'t'+currentCar.getName()+'t'
                            +currentCar.getDailyRent()+"元/天t"
                            +"载人:"+currentCar.getBusLoad()+"人"
                            +",载货:"+currentCar.getCargoCapacity()+"吨");
                    num++;                      
                }
            }
            int totalDailyMoney = 0;//每日租金的总和
            System.out.println("请输入您的租车数量(最大租车数为6):");
            Scanner input2=new Scanner(System.in);
            int carRentAmount=input2.nextInt();//租出去的车数
            if(carRentAmount>0&&carRentAmount

运行结果:

欢迎使用答答租车系统!
您是否需要租车:1是  0否
1
您可租车的类型极其价目表:
序号  汽车名称    日租金 容量  
No.1    奥迪A4    500元/天  载人:5人
No.2    小货车 150元/天  载货:5.0吨
No.3    奔驰E400  700元/天  载人:5人
No.4    皮卡  200元/天  载人:4人,载货:1.5吨
No.5    大型货车    600元/天  载货:10.0吨
No.6    大客车 650元/天  载人:25人
请输入您的租车数量(最大租车数为6):
5
请输入第1辆车的序号:
6
请输入第2辆车的序号:
4
请输入第3辆车的序号:
2
请输入第4辆车的序号:
1
请输入第5辆车的序号:
3
请输入需要租车的天数:
3
总载货量:6.5    总载客量:39 总价格:6600
您的账单:
***可载人的车有:
大客车
皮卡
奥迪A4
奔驰E400
——————共载人:39人
***可载货的车有:
皮卡
小货车
——————共载货:6.5吨

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4022/viewspace-2807507/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-17

  • 博文量
    81
  • 访问量
    655