//父类代码
package Rrent;
/*
* 机动车类 父类
*/
public abstract class MotoVehicle {
private String no; //车牌号
private String brand;//品牌
private int perRent;//日租金
* 机动车类 父类
*/
public abstract class MotoVehicle {
private String no; //车牌号
private String brand;//品牌
private int perRent;//日租金
public MotoVehicle(){ //无参构造方法
}
//有参构造方法
public MotoVehicle(String no,String brand,int perRent){
this.no=no;
this.brand=brand;
this.perRent=perRent;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPerRent() {
return perRent;
}
public void setPerRent(int perRent) {
this.perRent = perRent;
}
// 计算租金(抽象方法)
public abstract double caleRent(int days);
}
public abstract double caleRent(int days);
}
//Car类代码
package Rrent;
//轿车类
public class Car extends MotoVehicle {
// 型号
private String type;
public Car(){}//无参构造
public Car(String no,String brand,int perRent,String type){//有参构造
super(no,brand,perRent);
this.type=type;
}
public class Car extends MotoVehicle {
// 型号
private String type;
public Car(){}//无参构造
public Car(String no,String brand,int perRent,String type){//有参构造
super(no,brand,perRent);
this.type=type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double caleRent(int days) {
double price=this.getPerRent()*days; //价格乘以天数
if(days>7&&days<=30){
price*=0.9;
}else if(days>30&&days<=150){
price*=0.8;
}else if(days>150){
price*=0.7;
}
return price ;
}
}
//Bus类代码
package Rrent;
//客车类
public class Bus extends MotoVehicle {
// 座位数
private int seatCount;
public Bus(){}//无参构造
public Bus(String no,String brand,int perRent,int seatCount){//有参构造
super(no,brand,perRent);
this.seatCount=seatCount;
}
public class Bus extends MotoVehicle {
// 座位数
private int seatCount;
public Bus(){}//无参构造
public Bus(String no,String brand,int perRent,int seatCount){//有参构造
super(no,brand,perRent);
this.seatCount=seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
this.seatCount = seatCount;
}
// 根据自己的租金 重写租金方法
@Override
public double caleRent(int days) {
double price=this.getPerRent()*days; //价格乘以天数
if(days>=3&&days<7){
price*=0.9;
}else if(days>=7&&days<30){
price*=0.8;
}else if(days>=30&&days<150){
price*=0.7;
}else if(days>150){
price*=0.6;
}
return price ;
}
}
//管理信息类代码
package Rrent;
//汽车业务类
public class MotoOperation {
//定义汽车类型的数组,将该数组声明为父类类型
MotoVehicle[] motos=new MotoVehicle[8];
//初始化汽车信息
public void init(){
motos[0]=new Car("京A88888","宝马",800,"X6");//MotoVehicle m=new Car();
motos[1]=new Car("京A88887","宝马",600,"550i");
motos[2]=new Car("京A88886","别克",300,"林荫大道");
motos[3]=new Car("京A88885","别克",600,"GL8");
motos[4]=new Bus("京A88884","金杯",800,16);//MotoVehicle m=new Bus();
motos[5]=new Bus("京A88883","金杯",1500,34);
motos[6]=new Bus("京A88882","金龙",800,16);
motos[7]=new Bus("京A88881","金龙",1500,34);
}
// 租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
// 如果租赁的是客车 需要的条件:品牌 座位数 型号null
// 如果租赁的是轿车 需要的条件:品牌 型号 座位0
public MotoVehicle motoLeastOut(String brand,String type,int seat){
MotoVehicle moto=null;
for(MotoVehicle mymoto:motos){
if(mymoto instanceof Car){
Car car=(Car)mymoto;
if(car.getBrand().equals(brand)&&car.getType().equals(type)){
moto=car;
break;
}
}else{
Bus bus=(Bus)mymoto;
if(bus.getBrand().equals(brand)&&bus.getSeatCount()==seat){
moto=bus;
break;
}
}
}
return moto;
}
}
public class MotoOperation {
//定义汽车类型的数组,将该数组声明为父类类型
MotoVehicle[] motos=new MotoVehicle[8];
//初始化汽车信息
public void init(){
motos[0]=new Car("京A88888","宝马",800,"X6");//MotoVehicle m=new Car();
motos[1]=new Car("京A88887","宝马",600,"550i");
motos[2]=new Car("京A88886","别克",300,"林荫大道");
motos[3]=new Car("京A88885","别克",600,"GL8");
motos[4]=new Bus("京A88884","金杯",800,16);//MotoVehicle m=new Bus();
motos[5]=new Bus("京A88883","金杯",1500,34);
motos[6]=new Bus("京A88882","金龙",800,16);
motos[7]=new Bus("京A88881","金龙",1500,34);
}
// 租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
// 如果租赁的是客车 需要的条件:品牌 座位数 型号null
// 如果租赁的是轿车 需要的条件:品牌 型号 座位0
public MotoVehicle motoLeastOut(String brand,String type,int seat){
MotoVehicle moto=null;
for(MotoVehicle mymoto:motos){
if(mymoto instanceof Car){
Car car=(Car)mymoto;
if(car.getBrand().equals(brand)&&car.getType().equals(type)){
moto=car;
break;
}
}else{
Bus bus=(Bus)mymoto;
if(bus.getBrand().equals(brand)&&bus.getSeatCount()==seat){
moto=bus;
break;
}
}
}
return moto;
}
}
//测试类
package Rrent;
import java.util.Scanner;
public class RentMgr {
// 汽车租赁测试类
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
MotoOperation motoMgr=new MotoOperation();
// 租赁
motoMgr.init();
System.out.println("****欢迎来到汽车租赁公司****");
System.out.println("1、轿车\t2、客车:");
System.out.println("请选择您要租赁的汽车类型:");
int motoType=sc.nextInt();
String brand ="";
String type="";
int seat= 0;
if(motoType==1){
// 租赁轿车
System.out.println("请选择您要租赁的轿车品牌:1、别克;2、宝马");
int choose=sc.nextInt();
if(choose==1){
brand="别克";
System.out.println("请选择您要租赁的汽车型号:1、林荫大道;2、GL8");
type=(sc.nextInt()==1?"林荫大道":"GL8");
}else if(choose==2){
brand="宝马";
System.out.println("请选择您要租赁的汽车型号:1、X6;2、550i");
type=(sc.nextInt()==1?"X6":"550i");
}
}else if(motoType==2){
// 租赁汽车
type="";
System.out.println("请选择您要租赁的客车品牌:1、金杯 ;2金龙");
brand =(sc.nextInt()==1)?"金杯":"金龙";
System.out.println("请输入您要租赁的客车座位数:1、16座;2、34座");
seat =(sc.nextInt()==1)?16:34;
}
//租车
MotoVehicle moto=motoMgr.motoLeastOut(brand, type, seat);
System.out.println("请输入您要租赁的天数:");
int days=sc.nextInt();
double money=moto.caleRent(days);
System.out.println("租车成功,请按照如下车牌号去提车:"+moto.getNo());
System.out.println("您要需要支付"+money+"元");
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
MotoOperation motoMgr=new MotoOperation();
// 租赁
motoMgr.init();
System.out.println("****欢迎来到汽车租赁公司****");
System.out.println("1、轿车\t2、客车:");
System.out.println("请选择您要租赁的汽车类型:");
int motoType=sc.nextInt();
String brand ="";
String type="";
int seat= 0;
if(motoType==1){
// 租赁轿车
System.out.println("请选择您要租赁的轿车品牌:1、别克;2、宝马");
int choose=sc.nextInt();
if(choose==1){
brand="别克";
System.out.println("请选择您要租赁的汽车型号:1、林荫大道;2、GL8");
type=(sc.nextInt()==1?"林荫大道":"GL8");
}else if(choose==2){
brand="宝马";
System.out.println("请选择您要租赁的汽车型号:1、X6;2、550i");
type=(sc.nextInt()==1?"X6":"550i");
}
}else if(motoType==2){
// 租赁汽车
type="";
System.out.println("请选择您要租赁的客车品牌:1、金杯 ;2金龙");
brand =(sc.nextInt()==1)?"金杯":"金龙";
System.out.println("请输入您要租赁的客车座位数:1、16座;2、34座");
seat =(sc.nextInt()==1)?16:34;
}
//租车
MotoVehicle moto=motoMgr.motoLeastOut(brand, type, seat);
System.out.println("请输入您要租赁的天数:");
int days=sc.nextInt();
double money=moto.caleRent(days);
System.out.println("租车成功,请按照如下车牌号去提车:"+moto.getNo());
System.out.println("您要需要支付"+money+"元");
}
}