oopDay03Homework

2、抽象类MotoVehicle

  • 有参构造器:带有两个参数(String类型的汽车牌照和品牌)
  • 方法getNo():返回String 类型的汽车牌照
  • 方法getBrand():返回String 类型的品牌(宝马或者别克)
  • 抽象方法calRent():需要一个int类型的参数表示被租赁的天数,用于计算租金

3、子类 Car:表示轿车

  • 有参构造器:带有三个参数(String类型的汽车牌照、品牌、车型)
  • 方法getType():返回String 类型的车型(如果是宝马,则为 550i)
  • 方法setType():带有Stirng类型的参数,设置车型
  • 重写方法calRent():根据品牌及车型,找到对应的日租金,乘以租用天数,计算租金

4、子类 Bus:表示客车

  • 有参构造器:带有三个参数(String类型的汽车牌照和品牌、int类型的车座位数)
  • 方法getSeatCount ():返回int 类型的车座位数
  • 方法setSeatCount ():带有int 类型的参数,设置车座位数
  • 重写方法calRent():根据座位数,找到对应的日租金,乘以租用天数,计算租金

5、定义测试类TestRent,并实现车辆的租赁计算。

package day03.homework;

public abstract class MotoVehicle {
private String no;
private String brand;

public MotoVehicle(String no, String brand) {
this.no = no;
this.brand = brand;
}

public String getNo() {
return no;
}

public String getBrand() {
return brand;
}
public abstract int calRent(int days);
}

package day03.homework;

public class Bus extends MotoVehicle{
private int seatCount;

public Bus(String no, String brand, int seatCount) {
super(no, brand);
this.seatCount = seatCount;
}

public int getSeatCount() {
return seatCount;
}

public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}

@Override
public int calRent(int days) {
if (seatCount <= 16){
return days * 800;
}else
return days * 1500;
}
}

package day03.homework;

public class Car extends MotoVehicle {
private String type;

public Car(String no, String brand, String type) {
super(no, brand);
this.type = type;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

@Override
public int calRent(int days) {
if ("1".equals(type)){
return days* 500;
}else if ("2".equals(type)){
return days * 600;
}else {
return days * 300;
}
}
}

package day03.homework;

import java.sql.SQLOutput;
import java.util.Scanner;

public class TestRent {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎您来到汽车租赁公司!");
System.out.print("请输入要租赁的天数:");
int days = scanner.nextInt();
System.out.print("请输入要租赁的汽车类型(1、轿车 2、客车)");
String mtype = scanner.next();
if (mtype.equals("1")){
System.out.print("请输入要租赁的汽车品牌(1、宝马 2、别克)");
String brand = scanner.next();
if (brand.equals("1"))
System.out.print("(1、550i)");
else
System.out.print("(2、商务舱GL8 3、林荫大道)");
String type = scanner.next();
String no = "京BK5543";
System.out.println("分配给您的汽车牌号是: "+no);
Car car = new Car(no,brand,type);
int rent = car.calRent(days);
System.out.println("顾客您好!您需要支付的租赁费用是"+rent+"。");
}else {
System.out.print("请输入要租赁的客车品牌(1、金杯 2、金龙):");
String brand = scanner.next();
System.out.print("请输入客车的座位数");
int seatCount = scanner.nextInt();
String no = "京AU8769";
System.out.println("分配给您的汽车牌号是: "+no);
Bus bus = new Bus(no,brand,seatCount);
int rent = bus.calRent(days);
System.out.println("顾客您好!您需要支付的租赁费用是"+rent+"。");

}
}
}