用JavaSE阶段接口之前的知识完成一个简易的汽车租赁系统
汽车租赁系统信息表
运行结果
优化设计
- 将汽车类设计为抽象类
- 将计算租金的方法,设计为抽象方法
下面是代码:
汽车类
package CarRent;
/**
* 汽车类
*/
public abstract class Vehicle {
private String id;//车牌号
private String brand;//品牌
private int dayRent;//日租金
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 int getDayRent() {
return dayRent;
}
public void setDayRent(int dayRent) {
this.dayRent = dayRent;
}
public Vehicle(String id, String brand, int dayRent) {
this.id = id;
this.brand = brand;
this.dayRent = dayRent;
}
//计算租金的方法
public abstract double carRent(int days);
}
客车类
package CarRent;
/**
* 客车类
*/
public class Bus extends Vehicle {
private int seatCount;//座位数
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public Bus(String id, String brand, int seatCount, int dayRent) {
super(id, brand, dayRent);
this.seatCount = seatCount;
}
@Override
public double carRent(int days) {//重写计算租金的方法
if (days >= 1 && days <3) {
System.out.println("不打折");
return getDayRent() * days;
}else if (days >= 3 && days <7){
System.out.println("打9折");
return getDayRent() * days * 0.9;
}
else if (days >= 7 && days <30){
System.out.println("打8折");
return getDayRent() * days * 0.8;
}
else if (days >= 30 && days <150){
System.out.println("打7折");
return getDayRent() * days * 0.7;
}
else if (days >= 150){
System.out.println("打6折");
return getDayRent() * days * 0.6;
}
return 0;
}
}
轿车类
package CarRent;
/**
* 轿车类
*/
public class Car extends Vehicle {
private String type;//型号
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car(String id, String brand, String type, int dayRent) {
super(id, brand, dayRent);
this.type = type;
}
@Override
public double carRent(int days) {//重写计算租金的方法
if (days >= 1 && days <= 7) {
System.out.println("不打折");
return getDayRent() * days;
}else if (days > 7 && days <= 30){
System.out.println("打9折");
return getDayRent() * days * 0.9;
}
else if (days >=30 && days <= 150){
System.out.println("打8折");
return getDayRent() * days * 0.8;
}
else if (days > 150){
System.out.println("打7折");
return getDayRent() * days * 0.7;
}
return 0;
}
}
汽车业务类
package CarRent;
/**
* 汽车业务类
*/
public class VehicleOperation {
Vehicle[] vehicles = new Vehicle[8];//存储所有的车辆信息
//车辆信息初始化
public void initial(){
vehicles[0] = new Car("京NY28558","宝马","X6",800);
vehicles[1] = new Car("京CNY3284","宝马","550i",600);
vehicles[2] = new Car("京NT37465","别克","林荫大道",300);
vehicles[3] = new Car("京NT96968","别克","GL8",600);
vehicles[4] = new Bus("京6566754","金杯",16,800);
vehicles[5] = new Bus("京8696997","金龙",16,800);
vehicles[6] = new Bus("京9696996","金杯",34,1500);
vehicles[7] = new Bus("京8696998","金龙",34,1500);
}
//租赁汽车的办法
public Vehicle getVehicle(String brand, String type,int seatCount) {
//for循环遍历数组
for(int i = 0; i< vehicles.length; i++){
if (vehicles[i] instanceof Car) {
// 强转成小汽车car
Car car = (Car) vehicles[i];
if (car.getBrand().equals(brand) && car.getType().equals(type)) {
return car;
}
}
if(vehicles[i] instanceof Bus){
// 强转成大客车Bus
Bus bus = (Bus) vehicles[i];
if (bus.getBrand().equals(brand) && bus.getSeatCount()==seatCount) {
return bus;
}
}
}
//如果没有就返回空
return null;
}
}
汽车租赁管理类
也是程序的入口
package CarRent;
/**
* 汽车租赁管理类
* 程序入口
*/
import java.util.Scanner;
public class TestRent {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
VehicleOperation vo = new VehicleOperation();//初始化
vo.initial();
//定义一个为空的容器
Vehicle vehicle = null;
System.out.println("--------欢迎光临秋名山守望者汽车租赁公司--------");
System.out.println("1、轿车 2、客车");
System.out.println("请输入你要租赁的汽车类型:");
int num = sc.nextInt();
if (num == 1) {
System.out.println("请选择你要租赁的汽车品牌:1.别克 2.宝马");
int brand = sc.nextInt();
if (brand == 1) {
System.out.println("请选择你要租赁的汽车类型:1、林荫大道 2、GL8");
int type = sc.nextInt();
if (type == 1) {
vehicle = vo.getVehicle("别克","林荫大道",4);
}else if (type == 2) {
vehicle = vo.getVehicle("别克", "GL8", 4);
}
}else if (brand == 2) {
System.out.println("请选择你要租赁的汽车类型:1、X6 2、550i");
int type = sc.nextInt();
if (type == 1) {
vehicle = vo.getVehicle("宝马","X6",4);
}else if (type == 2) {
vehicle = vo.getVehicle("宝马", "550i", 4);
}
}
}else if (num == 2) {
System.out.println("请选择你要租赁的汽车品牌:1.金龙 2.金杯");
int brand = sc.nextInt();
if (brand == 1) {
System.out.println("请选择你要租赁的汽车类型:1、16座 2、34座");
int type = sc.nextInt();
if (type == 1) {
vehicle = vo.getVehicle("金龙","",16);
}else if (type == 2) {
vehicle = vo.getVehicle("金龙", "", 34);
}
}else if (brand == 2) {
System.out.println("请选择你要租赁的汽车类型:1、16座 2、34座");
int type = sc.nextInt();
if (type == 1) {
vehicle = vo.getVehicle("金杯","",16);
}else if (type == 2) {
vehicle = vo.getVehicle("金杯", "", 34);
}
}
}
if (vehicle != null) {
System.out.println("请输入您的租车天数");
int days =sc.nextInt();
double money = vehicle.carRent(days);
System.out.println("您租得的汽车牌号是" + vehicle.getId());
System.out.println("您需要支付的租赁费用是" + money + "元");
}else{
System.out.println("抱歉,暂无您所需要的汽车类型,请重新选择");
}
}
}
程序运行结果:
写的不好,欢迎指点!
还请各路大神不吝赐教