题目内容
实现租车的挑选,租金的计算等功能
Vehicle.java
package cn.kgc.tangcco;
/**
*
* @author 10570
*2019-12-31
*/
public abstract class Vehicle {
private String vehicleID; //车牌号
private String vehicleBrand; //品牌
private int perRent;//日租金
public abstract double calcRent(int days);//租车需要的租金(便于重写的一个抽象方法)
//创建vehicle的无参构造函数,一般情况下无参构造都要写明
public Vehicle() {}
//创建get。set方法
public String getVehicleID() {
return vehicleID;
}
public void setVehicleID(String vehicleID) {
this.vehicleID = vehicleID;
}
public String getVehicleBrand() {
return vehicleBrand;
}
public void setVehicleBrand(String vehicleBrand) {
this.vehicleBrand = vehicleBrand;
}
public int getPerRent() {
return perRent;
}
public void setPerRent(int perRent) {
this.perRent = perRent;
}
//代参构造函数
public Vehicle(String vehicleID, String vehicleBrand, int perRent) {
this.vehicleID = vehicleID;
this.vehicleBrand = vehicleBrand;
this.perRent = perRent;
}
}
Bus.java
package cn.kgc.tangcco;
public class Bus extends Vehicle {
int seatCount ; //座位
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public Bus(String vehicleID ,String vehicleBrand, int perRent, int seatCount) {
super(vehicleID, vehicleBrand, perRent);
this.seatCount = seatCount;
}
//租金方式,重写方法
public double calcRent(int days) {
double price = this.getPerRent()*days;
if(days > 3 && days <=7) {
price *= 0.9;
}else if(days > 7 && days <=7) {
price *= 0.8;
}else if(days > 30 && days <=150) {
price *= 0.7;
}else if(days > 150){
price *= 0.6;
}
return price;
}
}
Car.java
package cn.kgc.tangcco;
public class Car extends Vehicle{
private String types; //车的类型
public String getTypes() {
return types;
}
public void setTypes(String types) {
this.types = types;
}
public Car(String vehicleID,String vehicleBrand ,int perRent, String types) {
super(vehicleID, vehicleBrand, perRent);
this.types = types;
}
//租金方式,重写方法
public double calcRent(int days) {
int price = this.getPerRent()*days;
if(days >= 7 && days < 30) {
price *= 0.9;
}else if(days > 30 && days <=150) {
price *= 0.8;
}else if(days > 150) {
price *= 0.7;
}
return price;
}
}
ManageOperation,java
package cn.kgc.tangcco;
public class ManageOperation {
public Vehicle[] magOper = new Vehicle[8];
//车辆的初始化
public void init() {
magOper[0]= new Car("京NY","宝马",800,"X6"); //Vehicle m = new Car();父类引用指向子类对象
magOper[1]= new Car("京BP","宝马",600,"550i");
magOper[2]= new Car("京TY","别克",300,"林荫大道");
magOper[3]= new Car("京ER","别克",600,"GL8");
magOper[4]= new Bus("京WE","金龙",800,16);
magOper[5]= new Bus("京UI","金杯",800,16);
magOper[6]= new Bus("京PJ","金龙",1600,34);
magOper[7]= new Bus("京GD","金杯",1600,34);
}
//租赁汽车
public Vehicle vehicleLeastOut(String vehicleBrand, String types, int seatCount) {
Vehicle moto = null; //一个临时存储的地址
for(Vehicle mymoto : magOper) {
//instanceof 查看所查找的内容中是否包含 某个值
if(mymoto instanceof Car) { //遍历出的对象是什么类型 Car Or Bus
Car car= (Car)mymoto; //向下转型必须强转
if(car.getVehicleBrand().equals(vehicleBrand)&&car.getTypes().equals(types)) {
moto = car;
break;
}
}else {
Bus bus = (Bus)mymoto;
if(bus.getVehicleBrand().equals(vehicleBrand) && bus.getSeatCount()==seatCount) {
moto = bus;
break;
}
}
}
return moto;
}
}
RentManage.java
package cn.kgc.tangcco;
import java.util.Scanner;
public class RentManage {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//汽车业务类需要调用,才有车
ManageOperation mango = new ManageOperation();
//汽车的初始化,即先确定一下有的车
mango.init();
Vehicle moto = null;
// Random r = new Random();
// int ran = r.nextInt();
System.out.println("---------欢迎来到汽车城----------");
System.out.println("你的租车类型:1、轿车\t2、客车");
System.out.print("你的选择:");
int choose = input.nextInt();
//租车的三个条件
String vehicleBrand = "";//品牌
String types = "";//车的类型,属于轿车
int seatCount = 0;//座位数 ,属于客车
if(choose == 1) {
//租轿车
System.out.println("请选择你要的品牌:1、宝马\t2、别克");
System.out.print("你的选择:");
choose = input.nextInt();
if(choose == 1) {
vehicleBrand = "宝马";
System.out.println("请选择你要的型号:1、X6\t 2、550i");
System.out.print("你的选择:");
choose = input.nextInt();
types = (choose == 1)?"X6":"550i";
}else {
vehicleBrand = "别克";
System.out.println("请你选择你要的型号:1、林荫大道\t2、GL8");
System.out.print("你的选择:");
choose = input.nextInt();
types = (choose == 1)?"林荫大道":"GL8";
}
}else {
//租客车
System.out.println("请选择你要的品牌:1、金龙\t2、金杯");
System.out.print("你的选择:");
choose = input.nextInt();
if(choose == 1 ) {
vehicleBrand = "金龙";
System.out.println("你要的座位数:1、16座\t 2、34座");
choose = input.nextInt();
seatCount = (choose == 1)?16:34;
}else {
vehicleBrand = "金杯";
System.out.println("你要的座位数:1、16座\t 2、34座");
choose = input.nextInt();
seatCount = (choose == 1)?16:34;
}
}
moto = mango.vehicleLeastOut(vehicleBrand, types, seatCount);
System.out.println("请输入要租的天数:");
int days = input.nextInt();
double money = moto.calcRent(days);
System.out.println(vehicleBrand+types+seatCount);
System.out.println("你租的车牌:"+moto.getVehicleID());
System.out.println("你的租金是:"+money+"元");
}
}
这是之前的一道小的作业题,自己做完之后,对功能进行了实现。之后又参考了老师给打案例,进行了完善以及注释上的批注。今天发一下,借于大家参考。欢迎提出问题。
谢谢