某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示
车型 | 具体信息 | 日租金 | 折扣 |
轿车 | 宝马X6(京NY28588) | 800 | days>7天9折 days>30天8折 days>150天7折 |
宝马550i(京CNY3284) | 600 | ||
别克林荫大道(京NT37465) | 300 | ||
别克GL8(京NT96968) | 600 | ||
客车 | 金杯,16座(京6566754) | 800 | days>=3天9折 days>=7天8折 days>=30天7折 days>=150天6折 |
金龙,16座(京8696997) | |||
金杯,34座(京9696996) | 1500 | ||
金龙,34座(京8696998) |
提取有效信息:
抽象出类
汽车:品牌 车牌号 租金
轿车:车牌号 品牌 租金 型号
客车:车牌号 品牌 租金 座位数
测试:
需要有哪些方法:
汽车类:计算租金
客车:计算租金
轿车:计算租金
汽车业务类:如何进行租赁
汽车租赁管理类:程序入口
代码:
Car
package Pojo;
public abstract class Car{
private String licenseplate;
private String brand;
private double Price;
public Car(String licenseplate, String brand, double price) {
this.licenseplate = licenseplate;
this.brand = brand;
Price = price;
}
public String getLicenseplate() {
return licenseplate;
}
public void setLicenseplate(String licenseplate) {
this.licenseplate = licenseplate;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return Price;
}
public void setPrice(double price) {
Price = price;
}
public abstract double compute(int day);
}
轿车(Sedan)
package Pojo;
public class Sedan extends Car{
private String model;
public Sedan(String licenseplate, String brand, double price,String model) {
super(licenseplate, brand, price);
this.model = model;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Override
public double compute(int day) {
double money;
if(day>7 && day<=30){
money = day*getPrice()*0.9;
} else if (day>30 && day<=150) {
money = day*getPrice()*0.8;
}else{
money = day*getPrice()*0.7;
}
return money;
}
}
客车(PassengerCar)
package Pojo;
public class PassengerCar extends Car{
private int pernum;
public PassengerCar(String licenseplate, String brand, double price,int pernum) {
super(licenseplate, brand, price);
this.pernum = pernum;
}
public int getPernum() {
return pernum;
}
public void setPernum(int pernum) {
this.pernum = pernum;
}
@Override
public double compute(int day) {
double money;
if(day>=3 && day<7){
money = day*getPrice()*0.9;
} else if (day>=7 && day<30) {
money = day*getPrice()*0.8;
} else if (day>=30 && day<150){
money = day*getPrice()*0.7;
}else{
money = day*getPrice()*0.6;
}
return money;
}
}
服务类(CarService)
package Service;
import Pojo.Car;
import Pojo.PassengerCar;
import Pojo.Sedan;
import java.util.List;
public class CarService {
PassengerCar[] bus = new PassengerCar[4];
Sedan[] sedans = new Sedan[4];
public void info(){
//添加信息
bus[0] = new PassengerCar("京6566754","金杯",800,16);
bus[1] = new PassengerCar("京6566753","金龙",800,16);
bus[2] = new PassengerCar("京6566752","金杯",1500,34);
bus[3] = new PassengerCar("京6566751","金龙",1500,34);
sedans[0] = new Sedan("京6566714","宝马",800,"x6");
sedans[1] = new Sedan("京6566724","宝马",600,"550i");
sedans[2] = new Sedan("京6566734","别克",300,"林荫大道");
sedans[3] = new Sedan("京6566744","别克",600,"GL8");
}
//确定车辆
// 组轿车 确定轿车
public Sedan rentS(String brand,String model){
Sedan s = null;
for(Sedan sedan:sedans){
if (sedan.getBrand().equals(brand) && sedan.getModel().equals(model)){
s = sedan;
System.out.println(s.getLicenseplate());
break;
}
// System.out.println(brand + model +sedan.getBrand() + sedan.getModel());
// System.out.println(brand.equals(sedan.getBrand()));
// System.out.println(model.equals(sedan.getModel()));
}
return s;
}
// 组客车
public PassengerCar rentP(String brand,int setnum){
PassengerCar p = null;
for(PassengerCar bus:bus){
if (bus.getPernum() == setnum && bus.getBrand().equals(brand)){
p = bus;
break;
}
}
return p;
}
}
程序入口(Main)
import Pojo.Car;
import Pojo.PassengerCar;
import Pojo.Sedan;
import Service.CarService;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//创建业务
CarService c = new CarService();
PassengerCar p;
c.info();
String brand = null;
String model = null;
int setnum = 0;
int flag = 0;
Scanner sc =new Scanner(System.in);
// 用户界面
System.out.println("----------欢迎来到汽车租赁系统----------");
while(true){
System.out.println("请选择您要租赁的车型 1、轿车 2、客车");
flag = sc.nextInt();
if (flag == 1){
System.out.println("请选择您要租赁的品牌 1、宝马 2、别克");
if (sc.nextInt() == 1){
brand = "宝马";
System.out.println("请选择车的系列 1、x6 2、550i");
if (sc.nextInt() == 1){
model = "x6";
}else {
model = "5501";
}
}else {
brand = "别克";
System.out.println("请选择车的系列 1、林荫大道 2、GL8");
if (sc.nextInt() == 1){
model = "林荫大道";
}else {
model = "GL8";
}
}
} else if (flag == 2){
System.out.println("请选择您要租赁的品牌 1、金龙 2、金杯");
if (sc.nextInt() == 1){
brand = "金龙";
System.out.println("请选择车座数 1、16 2、34");
if (sc.nextInt() == 1){
setnum = 16;
}else {
setnum = 34;
}
}else {
brand = "金杯";
System.out.println("请选择车座数 1、16 2、34");
if (sc.nextInt() == 1){
setnum = 16;
}else {
setnum = 34;
}
}
flag = 2;
}
if(flag == 1){
Sedan s = c.rentS(brand,model);
System.out.println("您租赁的车为:" + s.getBrand() + " " + s.getModel() + ",车牌号为:" + s.getLicenseplate() + ",日租金为:(时间越长优惠越多)" + s.getPrice());
System.out.println("请输入您要租赁的天数");
int day = sc.nextInt();
double sum = s.compute(day);
System.out.println("您需要支付的费用为:" + sum);
break;
}else if(flag == 2){
p = c.rentP(brand,setnum);
System.out.println("您租赁的车为:" + p.getBrand() + " " + p.getPernum() + ",车牌号为:" + p.getLicenseplate() + ",日租金为:(时间越长优惠越多)" + p.getPrice());
System.out.println("请输入您要租赁的天数");
double sum = p.compute(sc.nextInt());
System.out.println("您需要支付的费用为:" + sum);
break;
}else{
break;
}
}
}
}
运行结果