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

答答租车系统,代码还是写的有点丑和不良,希望自己能再接再厉

标签:
Java

抽象类 Car类

package com.dada.carrental.carclass;

public abstract class Car {
    public int num;
    public String name;
    public float dayRent;

    abstract public void print();

    public Car(int num, String name, float dayRent) {
        super();
        this.num = num;
        this.name = name;
        this.dayRent = dayRent;
    }
}

载货接口

package com.dada.carrental.carclass;

public interface ICarryCargo {
    public abstract float carryCargo();
}

载客接口

package com.dada.carrental.carclass;

public interface ICarryPeople {
    public abstract int carryPeople();
}

客车类

package com.dada.carrental.carclass;

public class PassengerCar extends Car implements ICarryPeople {

    public int peopleLoad = 0;
    public PassengerCar(int num, String name, float dayRent,int people) {
        super(num, name, dayRent);
        this.peopleLoad = people;
    }

    @Override
    public int carryPeople() {
        return this.peopleLoad;
    }

    @Override
    public void print() {
        System.out.println(num + ".\t" + name + "\t" + (int)dayRent +"元/天\t载人:" + peopleLoad + "人");
    }

}

货车类

package com.dada.carrental.carclass;

public class Truck extends Car implements ICarryCargo {

    public float cargoLoad = 0.0f;
    public Truck(int num, String name, float dayRent,float cargo) {
        super(num, name, dayRent);
        this.cargoLoad = cargo;
    }

    @Override
    public float carryCargo() {
        return this.cargoLoad;
    }

    @Override
    public void print() {
        System.out.println(num + ".\t" + name + "\t" + (int)dayRent +"元/天\t载货:" + (int)cargoLoad + "吨");
    }

}

皮卡类

package com.dada.carrental.carclass;

public class Pickup extends Car implements ICarryCargo, ICarryPeople {

    public int peopleLoad = 0;
    public float cargoLoad = 0.0f;
    public Pickup(int num, String name, float dayRent,int people,float cargo) {
        super(num, name, dayRent);
        this.peopleLoad = people;
        this.cargoLoad = cargo;
    }

    @Override
    public int carryPeople() {
        return this.peopleLoad;
    }

    @Override
    public float carryCargo() {
        return this.cargoLoad;
    }

    @Override
    public void print() {
        System.out.println(num + ".\t" + name + "\t" + (int)dayRent +"元/天\t载人:" + peopleLoad + "人\t载货:" + (int)cargoLoad + "吨");
    }

}

在一个新的类里创建所有车的实例

package com.dada.carrental.cardata;

import com.dada.carrental.carclass.*;

public class CarData {
    public Car car1 = new PassengerCar(1, "奥迪A4", 500, 4);
    public Car car2 = new PassengerCar(2, "马自达6", 400, 4);
    public Car car3 = new Pickup(3, "皮卡雪6", 450, 4, 2.0f);
    public Car car4 = new PassengerCar(4, "金龙", 800, 20);
    public Car car5 = new Truck(5, "松花江", 400, 4.0f);
    public Car car6 = new Truck(6, "依维柯", 1000, 20);

}

系统类

package com.dada.carrental;

import java.util.Scanner;

import com.dada.carrental.carclass.*;
import com.dada.carrental.cardata.*;

class MainSystem {

    static Scanner scanner = new Scanner(System.in);

    static CarData data = new CarData();
    static Car[] allCars = {data.car1,data.car2,data.car3,data.car4,data.car5,data.car6};

    public static void main(String[] args) {
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车:1是  0否");
        start();
    }

    //入口方法
    static void start() {
        int userPrint = scanner.nextInt();
        //检测输入的是否是0或1,若不是,报错并重新输入
        while (!(userPrint == 1||userPrint ==0)) {
            System.out.println("输入错误,请重新输入");
            userPrint = scanner.nextInt();
        }
        //检测用户输入的是1还是0,分支进行
        switch (userPrint) {
        case 1:
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号\t汽车名称\t租金\t容量");
            printData(allCars);
            createNotice();
            break;
        case 0:
            System.out.println("退出程序");
        default:
            break;
        }
    }

    static void printData(Car[] cars) {
        for (Car car : cars) {
            car.print();
        }
    }

    static void createNotice() {
        int selectDays;
        System.out.println("请输入您要租车的数量:");
        int num = scanner.nextInt();
        Car[] selectedCars = new Car[num];
        for (int i = 0; i < num; i++) {
            System.out.println("请输入第" + (i+1) + "辆车的序号:");
            selectedCars[i] = allCars[scanner.nextInt()-1];
        }
        System.out.println("请输入租车天数:");
        selectDays = scanner.nextInt();
        Notice end = new Notice(selectedCars);
        end.printNotice(selectDays, selectedCars);
    }
}

详单类

package com.dada.carrental;

import com.dada.carrental.carclass.*;

class Notice {
    int totalPeopleLoad;
    float totalCargoLoad;
    float dayTotalRent;

    Notice(Car[] cars) {
        for (Car car : cars) {
            if (car instanceof ICarryPeople) {
                totalPeopleLoad += ((ICarryPeople) car).carryPeople();
            }
            if (car instanceof ICarryCargo) {
                totalCargoLoad += ((ICarryCargo) car).carryCargo();
            }
            dayTotalRent += car.dayRent;
        }
    }

    public void printNotice(int days,Car[] cars) {
        System.out.println("您的账单:");
        System.out.println("***可载人的车有:");
        for (Car car : cars) {
            if (car instanceof ICarryPeople) {
                System.out.print(car.name + "\t");
            }
        }
        System.out.println("共载人:" + totalPeopleLoad + "人");
        System.out.println("***载货的车有:");
        for (Car car : cars) {
            if (car instanceof ICarryCargo) {
                System.out.print(car.name + "\t");
            }
        }
        System.out.println("共载货:" + totalCargoLoad + "吨");
        float totalRent = dayTotalRent * days;
        System.out.println("***租车总价格" + totalRent +"元");
    }
}
点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消