学习了面向对象的核心概念——类的封装、继承、多态之后,答答租车系统开始营运了。请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。
父类Car
public class Car {
protected int num;
protected String carName;
protected int carMoney;
protected int peopleNumbers;
protected int weight;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarMoney() {
return carMoney;
}
public void setCarMoney(int carMoney) {
this.carMoney = carMoney;
}
public int getPeopleNumbers() {
return peopleNumbers;
}
public void setPeopleNumbers(int peopleNumbers) {
this.peopleNumbers = peopleNumbers;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
客车类
//客车
public class MannedVehicle extends Car {
public MannedVehicle(int num,String carName,int carMoney,int peopleNumbers){
this.num = num;
this.carName = carName;
this.carMoney = carMoney;
this.peopleNumbers = peopleNumbers;
}
}
皮卡车类
//皮卡车
public class DualPurposeVehicle extends Car {
public DualPurposeVehicle(int num,String carName,int carMoney,int peopleNumbers,int weight){
this.num = num;
this.carName = carName;
this.carMoney = carMoney;
this.peopleNumbers = peopleNumbers;
this.weight = weight;
}
}
货车类
//创建一个货车类,继承car类
public class Truck extends Car {
//货车名字,货车租借价格,货车载重
public Truck(int num,String carName,int carMoney,int weight){
this.num = num;
this.carName = carName;
this.carMoney = carMoney;
this.weight = weight;
}
}
主函数
package com.ldu.day0413;
//答答租车系统相关数据变量如下
/*
* MannedVehicle 客车
* DualPurposeVehicle 皮卡车
* Truck 货车
* input Scanner定义的输入
* num 是否租车,1或者0
* rentCar 数组来存储车辆信息
* rentNum 需要租借的车的数量
* numbers[] 存储需要租借的汽车的序号
* count = 0; 可载人,计算人数
* count1 = 0; 计算可载重吨数
* sum 计算单天的总费用
* allSum 计算总的费用
*
*/
import java.util.Scanner;
public class AnswerCarRentalSystem {
public static void main(String[] args) {
//定义一个数组,来存储可租车的类型
Car [] rentCar = {new MannedVehicle(1,"奥迪A4",500,4),
new MannedVehicle(2,"马自达6",400,4),
new DualPurposeVehicle(3,"皮卡雪",450,4,2),
new Truck(4,"松花江",400,4),
new Truck(5,"铁龙",1000,20),
};
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否需要租车:1是,0否");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if(num == 1){
//通过for循环来实现车辆的输出
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t" + "汽车名称\t\t" + "租金\t\t" + "所载人数\t\t" + "所载重量");
for(int i = 0;i < rentCar.length;i++){
System.out.print(rentCar[i].num + "\t" + rentCar[i].carName + "\t\t"
+ rentCar[i].carMoney + "元/天\t\t" + rentCar[i].peopleNumbers + "人\t\t"
+ rentCar[i].weight + "吨"
);
System.out.println();
}
//实现想要租借的车辆的数量,以及想要租借的车辆序号
System.out.println("请输入您要租汽车的数量:");
int rentNum = input.nextInt();
//定义一个数组,来存储想要租借的汽车的序号
int [] numbers = new int [6];
if(rentNum < 0){
System.out.println("输入的数量不符合,请重新输入");
rentNum = input.nextInt();
}else{
for(int j = 0;j < rentNum;j++){
System.out.println("请输入第" + (j + 1) + "辆车的序号");
numbers[j] =input.nextInt();
}
}
//输入想要租借车辆的天数,通过下面来实现
System.out.println("请输入租车天数");
int rentNumCar = input.nextInt();
//定义三个计数器,分别计算三种类型车辆的数量
int count = 0; //可载人,计算人数
int count1 = 0; //计算可载重吨数
//计算总钱数500 400 450 400 1000
int sum = 0;
//分类输出,车辆类型,然后计算车辆所总共载人数。
for(int i = 0;i < numbers.length;i++){
int temp = numbers[i];
switch(temp){
case 1:
sum += 500;
count += 4;
continue;
case 2:
sum += 400;
count += 4;
continue;
case 3:
sum += 450;
count += 4;
continue;
case 4:
sum += 400;
count1 += 2;
continue;
case 5:
sum += 1000;
count1 += 20;
continue;
}
}
//下面实现租借车辆所需要花的钱,sum和是租借一天的钱,总钱数需要*租借天数
int allSum = sum * rentNumCar;
//最后输出总账单
System.out.println("您的账单是:");
System.out.println("****可载人的车有:");
for(int j = 0;j < rentNum;j++){
System.out.print(rentCar[j].carName + "\t");
}
System.out.println("共载人:" + count + "人");
System.out.println("***载货的车有:");
for(int j = 0;j < rentNum;j++){
if(numbers[j] == 3 || numbers[j] == 4 || numbers[j] == 5)
System.out.print(rentCar[j].carName + "\t");
}
System.out.println("共载货:" + count1 + "吨");
System.out.println("***租车总价格:" + allSum + "元");
}else{
System.out.println("感谢使用再见!");
return ;
}
}
}