某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示
package com;
/**
* 汽车类
* @author Lenovo
*
*/
public abstract class Vehicle {
protected String brand; //品牌
protected String vehicleNum; //车牌号
protected double dayrent; //日租金
//汽车的构造方法
public Vehicle(String brand, String vehicleNum, double dayrent) {
this.brand = brand;
this.vehicleNum = vehicleNum;
this.dayrent = dayrent;
}
//租金的抽象方法:
public abstract double rent(int days);
}
package com;
/**
* 客车类
* @author Lenovo
*
*/
public class Bus extends Vehicle {
public String seating; //座位数
public Bus(String brand, String seating,String vehicleNum, double dayrent) {
super(brand,vehicleNum,dayrent);
this.seating = seating;
}
//租客车折扣规则
@Override
public double rent(int days) {
double rentmoney = dayrent*days;
if(days > 150){
rentmoney = rentmoney*0.6;
}else if(days > 30){
rentmoney = rentmoney*0.7;
}else if(days > 7){
rentmoney = rentmoney*0.8;
}else if(days > 3){
rentmoney = rentmoney*0.8;
}
return rentmoney;
}
}
package com;
/**
* 小轿车类
* @author Lenovo
*
*/
public class Car extends Vehicle {
public String cartype; //型号
public Car(String brand, String cartype,String vehicleNum, double dayrent) {
super(brand,vehicleNum,dayrent);
this.cartype = cartype;
}
//租轿车折扣规则
@Override
public double rent(int days) {
double rentmoney = dayrent*days;
if(days > 150){
rentmoney = rentmoney*0.7;
}else if(days > 30){
rentmoney = rentmoney*0.8;
}else if(days > 7){
rentmoney = rentmoney*0.9;
}
return rentmoney;
}
}
package com;
import java.util.Scanner;
/**
* 汽车租赁管理类
* @author Lenovo
*
*/
public class RentalManage {
public static final String[][] vehicleInfos = {
{"宝马","X6","","京NY28588"},
{"宝马","550i","","京CNY3284"},
{"别克","林荫大道","","京NT37465"},
{"别克","GL8","","京NT96968"},
{"金杯","","16座","京6566754"},
{"金龙","","16座","京8696997"},
{"金杯","","34座","京9696996"},
{"金龙","","34座","京8696998"}
};
public static void main(String[] args) {
//租车入口:
VehicleService vehicleService =new VehicleService();
Scanner sc= new Scanner(System.in);
String vehicleType=""; //客车或者轿车
System.out.println("**********************欢迎光临XX汽车租赁公司*********************");
System.out.println("车型"+"\t"+"具体信息"+"\t\t\t"+"日租金"+"\t"+"折扣");
System.out.println("轿车"+"\t"+"宝马X6(京NY28588)"+"\t"+"800"+"\t"+"days>7天9折");
System.out.println(" "+"\t"+"宝马550i(京CNY3284)"+"\t"+"600"+"\t"+"days>30天8折");
System.out.println(" "+"\t"+"别克林荫大道(京NT37465)"+"\t"+"300"+"\t"+"days>150天7折");
System.out.println(" "+"\t"+"别克GL8(京NT96968)"+"\t"+"600"+"\t"+" ");
System.out.println();
System.out.println("客车"+"\t"+"金杯,16座(京6566754)"+"\t"+"800"+"\t"+"days>3天9折");
System.out.println(" "+"\t"+"金龙,16座(京8696997)"+"\t"+"800"+"\t"+"days>7天8折");
System.out.println(" "+"\t"+"金杯,34座(京9696996)"+"\t"+"1500"+"\t"+"days>30天7折");
System.out.println(" "+"\t"+"金龙,34座(京8696998)"+"\t"+"1500"+"\t"+"days>150天6折");
System.out.print("请选择您需要的车型"+"(1代表轿车"+"2代表客车):");
int num = sc.nextInt();
if (num==1){
vehicleType="轿车";
}else if(num==2){
vehicleType="客车";
}else{
System.out.println("不好意思,输入有误,没有此种类型!!!");
}
//租车
Vehicle vehicle=vehicleService.getVehicle(vehicleType); // vehicleType传参数
System.out.println("请输入您需要租赁的天数:");
int days = sc.nextInt();
double rentmoney = vehicle.rent(days);
//分配给的车牌号
System.out.println("分配给您的汽车牌号:" +vehicle.vehicleNum);
//总的费用
System.out.println("您需要支付的租赁费用:" + rentmoney);
}
}
package com;
import java.util.Scanner;
/**
* 汽车业务类
* @author Lenovo
*
*/
public class VehicleService {
Scanner input =new Scanner(System.in);
public Vehicle getVehicle(String vehicleType){
Vehicle vehicle = null;
String brand = ""; //品牌
String type = ""; //型号
String seating = ""; //座位数
String vehicleNum = ""; //车牌号
int dayrent = 0;//日租金
if(vehicleType.equals("轿车")){
System.out.println("请选择轿车的品牌:"+"1、宝马"+"\t"+"2、别克");
int choose1 = input.nextInt();
if(choose1==1){
brand = "宝马";
System.out.println("请选择"+brand+"的型号:"+"1、X6"+"\t"+"2、550i");
int ch1 = input.nextInt();
if(ch1==1){
type = "X6";
dayrent = 800;
}else if(ch1==2){
type = "550i";
dayrent = 600;
}else{
System.out.println("抱歉"+brand+"没有此型号");
}
}
if(choose1==2){
brand = "别克";
System.out.println("请选择"+brand+"的型号:"+"1、林荫大道"+"\t"+"2、GL8");
int ch1 = input.nextInt();
if(ch1==1){
type = "林荫大道";
dayrent = 300;
}else if(ch1==2){
type = "GL8";
dayrent = 600;
}else{
System.out.println("抱歉"+brand+"没有此型号");
}
}
for(int i = 0; i<RentalManage.vehicleInfos.length; i++){
String[] vv =RentalManage.vehicleInfos[i];
if (vv[0]==brand&&vv[2]==type){
vehicleNum=vv[3];
}
}
//创建客车类对象
vehicle = new Car(brand, type, vehicleNum, dayrent);
}
//选择客车:
else if(vehicleType.equals("客车")){
System.out.println("请选择客车的品牌:"+"1、金杯"+"\t"+"2、金龙");
int choose2 = input.nextInt();
if(choose2==1){
brand = "金杯";
System.out.println("请选择"+brand+"的座位数:"+"1、16座"+"\t"+"2、34座");
int ch = input.nextInt();
if(ch==1){
seating = "16座";
dayrent = 800;
}else if(ch==2){
seating = "34座";
dayrent = 1500;
}else{
System.out.println("抱歉"+brand+"没有此座位数车型");
}
}
if(choose2==2){
brand = "金龙";
System.out.println("请选择"+brand+"的座位数:"+"1、16座"+"\t"+"2、34座");
int ch = input.nextInt();
if(ch==1){
seating = "16座";
dayrent = 800;
}else if(ch==2){
seating = "34座";
dayrent = 1500;
}else{
System.out.println("抱歉"+brand+"没有此座位数车型");
}
}
for(int i = 0; i<RentalManage.vehicleInfos.length; i++){
String[] vv =RentalManage.vehicleInfos[i];
if (vv[0]==brand&&vv[2]==seating){
vehicleNum=vv[3];
}
}
//创建客车类对象
vehicle = new Bus(brand, seating, vehicleNum, dayrent);
} //else if 结束
return vehicle;
}
}