号外!号外!豪车出租啦!
你没看错!你也没进错!出租豪车啦!接下来让我们看看是如何租车的吧!
车辆详情:
对应的类和属性:
实现效果:
根据上面的要求,咱来分析分析·······················
- 首先创建汽车类(Vehicle)、轿车类(Car)、客车类(Bus),当然还有一个测试类(TestVehicle),轿车和客车都属于汽车类,所以具有继承关系。
- 车辆的信息可以放在一个数组中,通过传入的汽车类型、汽车品牌、汽车座位数来判断租赁哪一辆车,然后返回对应的车牌号。
- 根据日租金和租赁天数,以及天数对应的折扣计算所需要的租金。并返回结果。
根据对应的类和属性,让我们来创建相关的类吧!代码如下:
Vehicle.java:
/**
* 汽车类
*/
public abstract class Vehicle {
private String id; //车牌号
private String brand; //品牌
private double dayRent; //日租金
//重写equals()方法,用来比较类型
@Override
public abstract boolean equals(Object obj);
//创建有参构造便于创建对象
public Vehicle(String id, String brand, double dayRent) {
this.id = id;
this.brand = brand;
this.dayRent = dayRent;
}
//当然还得有无参构造
public Vehicle() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getDayRent() {
return dayRent;
}
public void setDayRent(double dayRent) {
this.dayRent = dayRent;
}
//计算租金
public abstract double Money(int num, double dayRent);
}
Car.java
/**
* 轿车类 继承汽车类
*/
public class Car extends Vehicle{
private String type;
@Override
public boolean equals(Object obj) {
//判断传入的参数obj是否属于Car类型
if (obj instanceof Car){
//向下转型
Car c = (Car) obj;
return this.getBrand().equals(c.getBrand())&&this.getType().equals(c.getType());
}
return false;
}
public Car(String id, String brand, double dayRent, String type) {
super(id, brand, dayRent);
this.type = type;
}
public Car() {
}
//重写父类Vehicle中的Money()方法,用来计算轿车的租金
@Override
public double Money(int num, double dayRent) {
if (num > 150){
return num*dayRent*0.7;
}else if (num > 30){
return num*dayRent*0.8;
}else if (num > 7){
return num*dayRent*0.9;
}else
return dayRent*num;
}
public Car(String brand, String type) {
super.setBrand(brand);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Bus.java
/**
* 客车 继承汽车类
*/
public class Bus extends Vehicle{
private String num; //座位数
//计算租金
@Override
public double Money(int num, double dayRent) {
if (num >= 150){
return num*dayRent*0.6;
}else if (num >= 30){
return num*dayRent*0.7;
}else if (num >= 7){
return num*dayRent*0.8;
}else if (num >= 3){
return num*dayRent*0.9;
}else
return dayRent*num;
}
public Bus(String id, String brand, double dayRent, String num) {
super(id, brand, dayRent);
this.num = num;
}
@Override
public boolean equals(Object obj) {
//判断传入的参数obj是否是Bus类型
if (obj instanceof Bus){
//向下转型
Bus b = (Bus) obj;
return this.getBrand().equals(b.getBrand())&&this.getNum().equals(b.getNum());
}
return false;
}
public Bus() {
}
public Bus(String brand, String type) {
super.setBrand(brand);
this.num = type;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
TestVehicle.java
/**
* 汽车业务类
*/
public class TestVehicle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//创建一个数组,将汽车的
Vehicle[] cars = new Vehicle[8];
cars[0] = new Car("京NY28588","宝马",800,"X6");
cars[1] = new Car("京CNY3284","宝马",600,"550i");
cars[2] = new Car("京NT37465","别克",300,"林荫大道");
cars[3] = new Car("京NT96968","别克",600,"GL8");
cars[4] = new Bus("京6566754","金杯",800,"16座");
cars[5] = new Bus("京8696997","金龙",800,"16座");
cars[6] = new Bus("京9696996","金杯",1500,"34座");
cars[7] = new Bus("京8696998","金龙",1500,"34座");
Vehicle v;
System.out.println("************欢迎光临秋名山守望者汽车租赁公司************");
System.out.println("1、轿车 2、客车");
System.out.print("请选择你要租赁的汽车类型:");
int t1 = sc.nextInt();
String brand; //品牌
String type; //汽车类型
int t2,b;
//判断是否为轿车,如果是,则往下执行。如果不是,则执行else部分
if (t1 == 1){
System.out.print("请选择你要租赁的汽车品牌(1、别克 2、宝马):");
b = sc.nextInt();
brand = b == 1 ?"别克":"宝马"; //根据三目运算判断汽车品牌并返回对应的品牌,用brand接收。
if (b == 1){
System.out.print("请选择你要租赁的汽车类型(1、林荫大道 2、GL8):");
t2 = sc.nextInt();
type = t2 == 1 ?"林荫大道":"GL8";
}else{
System.out.print("请选择你要租赁的汽车类型(1、X6 2、550i):");
t2 = sc.nextInt();
type = t2 == 1 ?"X6":"550i";
}
v = new Car(brand, type);
}else {
System.out.print("请选择你要租赁的汽车品牌(1、金龙 2、金杯):");
b = sc.nextInt();
brand = b == 1 ?"金龙":"金杯";
if (b == 1){
System.out.print("请选择你要租赁的汽车类型(1、16座 2、34座):");
t2 = sc.nextInt();
type = t2 == 1 ?"16座":"34座";
}else{
System.out.print("请选择你要租赁的汽车类型(1、16座 2、34座):");
t2 = sc.nextInt();
type = t2 == 1 ?"16座":"34座";
}
v = new Bus(brand, type);
}
//通过foreach增强for来找出对应的车辆信息
for(Vehicle vv : cars){
if (vv.equals(v)) {
System.out.print("请输入您要租赁的天数:");
int num = sc.nextInt();
v.setDayRent(num);
System.out.println("分配给您的车牌号是:"+vv.getId());
System.out.println("您需要支付的租赁费用是:" + v.Money(num, vv.getDayRent())+"元");
}
}
}
}
以上就是所有的代码啦,有需要的就请借鉴吧!!!!!
运行结果:
看完后别忘了留下个赞哟!