编写测试类
import java.util.Scanner;
public class TestMotoVehicle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到汽车租赁公司");
System.out.println("请输入需要租赁的天数");
int days = sc.nextInt();
System.out.println("请选择汽车类型(1,轿车 2,客车)");
int choose = sc.nextInt();
switch (choose) {
case 1:
System.out.println("请输入汽车品牌(1,宝马2,别克)");
choose = sc.nextInt();
if (choose == 1) {
Car car = new Car("5501", 8895, "宝马");
System.out.println("租金是" + car.calRent(days));
car.show();
} else if (choose == 2) {
System.out.println("请选择型号(1,商务舱GL8 2,林荫大道)");
choose = sc.nextInt();
Car car = new Car("商务舱GL8", 8895, "别克");
System.out.println("租金是" + car.calRent(days));
car.show();
} else {
Car car = new Car("林荫大道", 8879, "别克");
System.out.println("租金是" + car.calRent(days));
car.show();
}
break;
case 2:
System.out.println("请选择座位数");
choose = sc.nextInt();
if (choose == 1) {
Bus b = new Bus(54865, "金杯", 15);
System.out.println("租金是" + b.calRent(days));
b.show();
} else if (choose == 2) {
Bus b = new Bus(54865, "金龙", 25);
System.out.println("租金是" + b.calRent(days));
b.show();
}
break;
default:
break;
}
}
}
两种汽车类型的父类
public abstract class MotoVehicle {
private int no; // 车牌号
private String brand; // 品牌
/*
* private String color; //颜色 private int Mileage; //里程 private String
* models;//表示型号 private int rental;//表示租金
*/
/*
* public void print(){
* System.out.println("你选择的是"+models+"日租金是"+rental+"元/天"); }
*/
public MotoVehicle(int no, String brand) {
this.no = no;
this.brand = brand;
}
public MotoVehicle() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
no = no;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
brand = brand;
}
public abstract int calRent(int days);
}
创建子类car
public class Car extends MotoVehicle {
private String type;
public Car() {
}
public Car(String type, int no, String brand) {
super(no, brand);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int calRent(int days) {
int sum=0;
if ("5501".equals(type)) {
sum= days * 500;
} else if ("商务舱GL8".equals(type)) {
sum= days * 600;
} else if("林荫大道".equals(type)){
sum= days * 300;
}
return sum;
}
public void show() {
System.out.println("车牌号是" + super.getNo());
}
创建子类Bus
public class Bus extends MotoVehicle {
private int SeatCount;
public Bus() {
super();
// TODO Auto-generated constructor stub
}
public Bus(int no, String brand, int seatCount) {
super(no, brand);
SeatCount = seatCount;
}
public int getSeatCount() {
return SeatCount;
}
public void setSeatCount(int seatCount) {
SeatCount = seatCount;
}
public int calRent(int days) {
int sum=0;
if (SeatCount <= 16) {
sum = days * 800;
} else if(SeatCount>16){
sum= days * 1500;
}
return sum;
}
public void show() {
System.out.println("车牌号是" + super.getNo());
}