java编写汽车租赁系统

面向对象编程 汽车租赁系统
package com.my.rentalcar;

//汽车类(父类)
public abstract class Automobile {

//车牌号 品牌 日租金
private String aNum;
private String brand;
private int rent;

//构造方法:构建子类对象时通过super调用
public Automobile() {}

public Automobile(String aNum, String brand, int rent) {
	this.aNum = aNum;
	this.brand = brand;
	this.rent = rent;
}


public String getaNum() {
	return aNum;
}
public void setaNum(String aNum) {
	this.aNum = aNum;
}

public String getBrand() {
	return brand;
}
public void setBrand(String brand) {
	this.brand = brand;
}

public int getRent() {
	return rent;
}
public void setRent(int rent) {
	this.rent = rent;
}

//定义抽象方法,计算租金
public abstract float totalPrice(int days);

}

package com.my.rentalcar;

//汽车业务类
public class AutomobileBusiness {

public final static  Automobile[] Automobiles = new Automobile[8];

//初始化汽车
//public void init()
static{
	
	//多态,父类引用指向子类对象
	Automobiles[0] = new Car("京NY28588","宝马",800,"X6");   
	Automobiles[1] = new Car("京CNY3284","宝马",600,"550i");
	Automobiles[2] = new Car("京NT37465","别克",300,"林荫大道");
	Automobiles[3] = new Car("京NT96968","别克",600,"GL8");
	Automobiles[4] = new Bus("京6566754","金杯",800,16);
	Automobiles[5] = new Bus("京9696996","金杯",1500,34);
	Automobiles[6] = new Bus("京8696997","金龙",800,16);
	Automobiles[7] = new Bus("京8696998","金龙",1500,34);
}

//租车方法
public Automobile rentalAutomobile(String brand,int seatnum,String type) {
	Automobile a = null;
	
	//根据用户输入的信息,遍历数组,得到对应汽车返回给用户
	for(Automobile automobile:Automobiles) {
		if(automobile instanceof Car) {
			//向下转型:将automobile强转为Car
			Car car = (Car)automobile;
			//轿车品牌和型号与用户想要的吻合
			if(car.getBrand().equals(brand) && car.getType().equals(type)) {
				a = car;
				break;
			}
		}else {
			//向下转型:将automobile强转为Bus
			Bus bus = (Bus)automobile;
			//客车品牌和型号与用户想要的吻合
			if(bus.getBrand().equals(brand) && bus.getSeatnum()==seatnum) {
				a = bus;
				break;
			}
		}
	}
	return a;
}

}

package com.my.rentalcar;

//轿车类(子类)
public class Bus extends Automobile{

//客车座位数
private int seatnum;

public Bus() {}
public Bus(String aNum, String brand, int rent,int seatnum) {
	super(aNum, brand, rent);   //调用父类带参构造方法
	this.seatnum = seatnum;
}

public int getSeatnum() {
	return seatnum;
}
public void setSeatnum(int seatnum) {
	this.seatnum = seatnum;
}

//重写父类计算租金抽象方法,由客车车型的折扣来计算总价
public float totalPrice(int days) {
	//不在折扣范围内时的价格 = 日租金 * 天数 
	float price = this.getRent() * days;
	//在折扣范围内时
	if(days>=3 && days<7) {
		price = price * 0.9f;
	}else if(days>=7 && days<30) {
		price = price * 0.8f;
	}else if(days>=30 && days<150) {
		price = price * 0.7f;
	}else if(days>=150) {
		price = price * 0.6f;
	}
	return price;
}

}

package com.my.rentalcar;

//轿车类(子类)
public class Car extends Automobile{

//轿车型号
private String type;

public Car() {}
public Car(String aNum, String brand, int rent,String type) {
	super(aNum, brand, rent);   //调用父类带参构造方法
	this.type = type;
}

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

//重写父类计算租金抽象方法,由轿车车型的折扣来计算总价
public float totalPrice(int days) {
	//不在折扣范围内时的价格 = 日租金 * 天数 
	float price = this.getRent() * days;
	//在折扣范围内时
	if(days>7 && days<=30) {
		price = price * 0.9f;
	}else if(days>30 && days<=150) {
		price = price * 0.8f;
	}else if(days>150) {
		price = price * 0.7f;
	}
	return price;
}

}

package com.my.rentalcar;

import java.util.Scanner;

//汽车租赁管理类
public class RentalAutomobile {
public static void main(String[] args) {
//System.out.println(AutomobileBusiness.Automobiles[0].getClass());
Scanner input = new Scanner(System.in);
AutomobileBusiness Auto = new AutomobileBusiness();
System.out.println(“欢迎光临利民汽车租赁公司”);
System.out.print(“请选择您要租赁的车型(1、轿车 2、客车):”);
int AutoType = 0;
do{
if(input.hasNextInt())
{
AutoType = input.nextInt();
break;
}
else{
input.next();
System.out.print(“输入有误,请重新输入:”);
}
}while(true);
//获得用户输入的条件
String brand = “”;
int seatnum = 0;
String type = “”;

	switch(AutoType) {
	case 1:
		//租轿车
		System.out.print("请选择您要租赁汽车的品牌(1、宝马  2、别克):");
		int choose = 0;
		do{
			if(input.hasNextInt())
			{
				choose = input.nextInt();
				break;
			}
			else{
				input.next();
				System.out.print("输入有误,请重新输入:");
				}
		}while(true);
		if(choose == 1) {
			brand = "宝马";
			System.out.print("请选择您要租赁的汽车型号(1、X6  2、550i):");
			int x = 0;
			do{
				if(input.hasNextInt())
				{
					x = input.nextInt();
					break;
				}
				else{
					input.next();
					System.out.print("输入有误,请重新输入:");
					}
			}while(true);
			type = (x == 1)?"X6":"550i";
		}else {
			brand = "别克";
			System.out.print("请选择您要租赁的汽车型号(1、林荫大道  2、GL8):");
			int y = 0;
			do{
				if(input.hasNextInt())
				{
					y = input.nextInt();
					break;
				}
				else{
					input.next();
					System.out.print("输入有误,请重新输入:");
					}
			}while(true);
			type = (y == 1)?"林荫大道":"GL8";
		}
		break;
	case 2:
		//租客车
		System.out.print("请选择您要租赁汽车的品牌(1、金杯  2、金龙):");
		int z = 0;
		do{
			if(input.hasNextInt())
			{
				z = input.nextInt();
				break;
			}else{
				input.next();
				System.out.print("输入有误,请重新输入:");
				}
		}while(true);
		brand = (z == 1)?"金杯":"金龙";
		System.out.print("请选择汽车座位数(1、16座  2、34座):");
		int q = 0;
		do{
			if(input.hasNextInt())
			{
				q = input.nextInt();
				break;
			}else{
				input.next();
				System.out.print("输入有误,请重新输入:");
				}
		}while(true);
		seatnum = (q == 1)?16:34;
		break;
	}
	
	//初始化汽车信息
	//Auto.init();
	Automobile a = Auto.rentalAutomobile(brand, seatnum, type);
	System.out.print("请输入您要租赁的天数:");
	int days = 0;
	do{
		if(input.hasNextInt())
		{
			days = input.nextInt();
			break;
		}else{
			input.next();
			System.out.print("输入有误,请重新输入:");
			}
	}while(true);
	float rent = a.totalPrice(days);
	System.out.println("您租赁的汽车车牌号为:" + a.getaNum());
	System.out.println("租金总计为:" + rent + "元"); 
	
	input.close();
}

}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值