今日份图片
大家想要什么类型的图片也可以在评论区里打出来
今天给大家带来一个java的小案例,只用java面向对象的知识写。
首先看需求
- 汽车租赁信息表
- 类和属性
- 运行效果
分析
首先我们先来看看谁是父类,谁是子类,根据雷和属性这张图,我们知道汽车类有:车牌号、品牌、日租金,轿车类有:车牌号、品牌、型号、日租金,客车类有:车牌号、品牌、日租金、座位数等这些参数,那么我们可以发现轿车类和客车类他们共有的属性是车牌号、品牌和日租金,我们就可以把车牌号、品牌和日租金这三个属性提取到父类中去,也就是汽车类中,租金我们搞一个方法。那么此时轿车类和客车只用去继承父类就可以。
- 父类 汽车类
public abstract class Vehicles {
private String vhId;//车牌号
private String brand;//品牌
private double dataRent;//日租金
public Vehicles() {//无参构造
}
//全参构造
public Vehicles(String vhId, String brand, double dataRent) {
this.vhId = vhId;
this.brand = brand;
this.dataRent = dataRent;
}
public String getVhId() {
return vhId;
}
public void setVhId(String vhId) {
this.vhId = vhId;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getDataRent() {
return dataRent;
}
public void setDataRent(double dataRent) {
this.dataRent = dataRent;
}
public abstract double calRent(int days);//计算租金
}
- 子类 轿车类
子类我们去继承父类,此时我们的子类中就有了父类中的那些属性和方法,其中计算租金的方法我们要重写,因为轿车和客车的计算租金的方式不同。
//子类 轿车
public class Car extends Vehicles{
private String carType;//汽车型号
public Car(){}//无参构造
public Car(String vhId, String brand, double dataRent, String carType) {//全参构造
super(vhId, brand, dataRent);
this.carType = carType;
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
@Override//重写父类方法
public double calRent(int days) {
double price = getDataRent()*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;
}
}
- 子类 客车类
//子类 客车
public class Bus extends Vehicles{
private int seatNum;//座位数
public Bus(){}//无参构造
public Bus(String vhId, String brand, double dataRent, int seatNum) {//全参构造
super(vhId, brand, dataRent);
this.seatNum = seatNum;
}
public int getSeatNum() {
return seatNum;
}
public void setSeatNum(int seatNum) {
this.seatNum = seatNum;
}
@Override//重写父类方法
public double calRent(int days) {
double price = getDataRent()*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;
}
}
子类和父类我们都写好了,下面我们要给车辆初始化,也就是赋值了,没有车辆我们写这些也是白搭。我们在此选择一种较为简单易懂的赋值方式,每个类单独赋值,也就是先给轿车赋值再给客车类赋值,或者两个调换过来,这个都没有影响。基础好的同学可以用汽车去给客车和轿车同时赋值(向上向下转型)。下面这两趴都是我们的汽车业务类(VehicleBusiness)。
//汽车初始化 赋值
static Car [] car = new Car[4];
static Bus [] bus = new Bus[4];
public static void init(){
//轿车
car[0] = new Car("京NY28588","宝马",800 ,"X6");
car[1] = new Car("京CNY3284","宝马",600 ,"550i");
car[2] = new Car("京NT37465","别克",300 ,"林荫大道");
car[3] = new Car("京NT96968","别克",600 ,"GL8");
//客车
bus[0] = new Bus("京6566754","金杯",800,16);
bus[1] = new Bus("京8696997","金龙",800,16);
bus[2] = new Bus("京9696996","金杯",1500,34);
bus[3] = new Bus("京8696998","金龙",1500,34);
}
我们分析可知,不管是轿车还是客车,最多我们确定其中的两个属性我们就可以确定是哪辆车了,在这我们轿车类取品牌和型号,客车类我们取品牌和座位数。
//租轿车
public Car rentCar(String brand,String type){
Car c = null;
for (Car car:car) {
if (car.getBrand().equals(brand)&&car.getCarType().equals(type)){
c=car;
break;
}
}
return c;
}
//租客车
public Bus rentBus(String brand,int seatNum){
Bus b = null;
for (Bus bus:bus) {
if (bus.getBrand().equals(brand)&&bus.getSeatNum()==seatNum){
b=bus;
break;
}
}
return b;
}
下面就该是测试类
- 测试类
public class TestVehicle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
VehicleBusiness vb = new VehicleBusiness();
System.out.println("**************************欢迎光临秋名山守望者汽车租赁公司***************************");
System.out.println("1、轿车 2、客车");
System.out.print("请选择你要租赁的汽车类型:");
int i = sc.nextInt();
//获取品牌 座位数 型号
String brand=null;
int seatNum = 0;
String type=null;
if (i==1){
System.out.print("请选择你要租赁的汽车品牌:1、别克 2、宝马");
int carBrand=sc.nextInt();
if (carBrand==1){
brand="别克";
System.out.print("请选择你要租赁的汽车类型:1、GL8 2、林荫大道");
int choose = sc.nextInt();
if (choose==1){
type="林荫大道";
}else {
type="GL8";
}
}else {
brand="宝马";
System.out.print("请选择你要租赁的汽车类型:1、X6 2、550i");
int choose = sc.nextInt();
if (choose==1){
type="X6";
}else {
type="550i";
}
}
}else {
System.out.print("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
int BusBrand=sc.nextInt();
if (BusBrand==1){
brand="金龙";
System.out.print("请选择你要租赁的汽车座位数:1、16座 2、34座");
int choose = sc.nextInt();
if (choose==1){
seatNum=16;
}else {
seatNum=34;
}
}else {
brand="金杯";
System.out.print("请选择你要租赁的汽车座位数:1、16座 2、34座");
int choose = sc.nextInt();
if (choose==1){
seatNum=16;
}else {
seatNum=34;
}
}
}
//最后一步
//汽车初始化
VehicleBusiness.init();
//租车
if (i==1){
Car c = vb.rentCar(brand,type);
System.out.print("请输入您要租赁汽车的天数:");
double price=c.calRent(sc.nextInt());
//车牌号
System.out.println("分配给您的汽车牌号是::"+c.getVhId());
System.out.println("您需要支付的租赁费用为:"+price+"元。");
}else {
Bus b = vb.rentBus(brand,seatNum);
System.out.print("请输入您要租赁汽车的天数:");
double price=b.calRent(sc.nextInt());
//车牌号
System.out.println("分配给您的汽车牌号是::"+b.getVhId());
System.out.println("您需要支付的租赁费用为:"+price+"元。");
}
}
}
其中在最后一步如果有的同学是选择同时给轿车和客车赋值的话,那么这里写的也不一样,这里要注意。
这样一个简单的用java面向对象写的汽车租赁系统就好了~~~