今天再给大家分享一个小项目:汽车租赁管理系统。用的是Java语言开发的,代码不多,大概260行左右吧,系统是实现图书的新增图书、删除图书、借阅图书、归还图书、查看图书等简单的功能(后附源代码)!!!
首先展示一下运行界面效果图:运行代码后,会在控制台显示如下界面:
然后让用户选择,如果用户不小心或者误输入了错误的数,会再次让用户进行输入。
当用户输入1后,可以选择要租赁的汽车品牌:
包括别克、宝马两大选项。
当用户继续输入1后,可以选择要租赁的汽车类型,效果如下:
用户继续输入1后,可以选择要租赁的汽车的天数,效果如下:
用户继续输入要租赁的汽车的天数后,效果如下:
会显示分配给自己的汽车牌号、以及需要支付的租赁费用:
同理,初始时,用户输入2后,接着输入租赁的汽车品牌,继续选择租赁的汽车车位数:
源代码如下:
1 . 汽车业务类
1 package com.manage; 2 3 import com.vehicles.Bus; 4 import com.vehicles.Car; 5 import com.vehicles.Vehicle; 6 7 //汽车业务类 8 public class VehicleOperation { 9 //汽车数组 10 Vehicle[] vehicles = new Vehicle[8]; 11 12 //汽车信息初始化 13 public void init(){ 14 //向上转型 15 vehicles[0] = new Car("京N85764","宝马",800,"X6"); //Vehicle v = new Car(); 16 vehicles[1] = new Car("京L79654","宝马",600,"550i"); //Vehicle v = new Car(); 17 vehicles[2] = new Car("京Y96584","别克",400,"林荫大道"); //Vehicle v = new Car(); 18 vehicles[3] = new Car("京M36589","别克",500,"GL8"); //Vehicle v = new Car(); 19 vehicles[4] = new Bus("京Y85754","金龙",1000,34); //Vehicle v = new Bus(); 20 vehicles[5] = new Bus("京U88888","金龙",800,16); //Vehicle v = new Bus(); 21 vehicles[6] = new Bus("京T66666","金杯",1200,34); //Vehicle v = new Bus(); 22 vehicles[7] = new Bus("京P90876","金杯",700,16); //Vehicle v = new Bus(); 23 } 24 25 //租车:简单工厂模式 26 //参数:品牌 座位数 型号 (客车:品牌 座位数 "";轿车:品牌 0 型号) 27 public Vehicle rentVehicle(String brand,int seatCount,String type){ 28 Vehicle v = null; 29 //根据用户提供的租车信息(方法参数)去遍历汽车数组,找到相应车辆返回给用户 30 for(Vehicle vehicle:vehicles){ 31 if(vehicle instanceof Car){ 32 //轿车 33 Car car = (Car)vehicle; //向下转型 34 //轿车的品牌和型号与用户想要的轿车品牌与型号吻合 35 if(car.getBrand().equals(brand) && car.getType().equals(type)){ 36 v = car; 37 break; 38 } 39 }else{ 40 //客车 41 Bus bus = (Bus)vehicle; //向下转型 42 //客车的品牌和座位数与用户想要的客车品牌与座位数吻合 43 if(bus.getBrand().equals(brand) && bus.getSeatCount()==seatCount){ 44 v = bus; 45 break; 46 } 47 } 48 } 49 return v; 50 } 51 }
2 . 汽车租赁管理类:入口测试类
1 package com.manage; 2 3 import java.util.Scanner; 4 5 import com.vehicles.Vehicle; 6 7 //汽车租赁管理类:入口测试类 8 public class VehicleRent { 9 public static void main(String[] args) { 10 Scanner input = new Scanner(System.in); 11 VehicleOperation vehicleOpr = new VehicleOperation(); 12 System.out.println("************欢迎光临腾飞汽车租赁有限公司*****************"); 13 System.out.print("请选择您要租赁的车型:1、轿车 2、客车 "); 14 int vehicleType = input.nextInt(); 15 16 //获取用户租赁汽车的三个条件:品牌 座位数 型号 17 String brand = ""; 18 int seatCount = 0; 19 String type = ""; 20 21 switch(vehicleType){ 22 case 1: //租赁轿车 获取到用户想租赁的轿车的品牌及型号信息 23 System.out.print("请选择您要租赁的汽车品牌:1、别克 2、宝马 "); 24 int choose = input.nextInt(); 25 if(choose == 1){ 26 brand = "别克"; 27 System.out.print("请选择您要租赁的汽车类型:1、林荫大道 2、GL8"); 28 type = (input.nextInt() == 1 )?"林荫大道":"GL8"; 29 }else{ 30 brand = "宝马"; 31 System.out.print("请选择您要租赁的汽车类型:1、X6 2、550i"); 32 type = (input.nextInt() == 1 )?"X6":"550i"; 33 } 34 35 break; 36 case 2://租赁客车 获取到用户想租赁的客车的品牌及座位数信息 37 System.out.print("请选择您要租赁的汽车品牌:1、金杯 2、金龙 "); 38 brand = (input.nextInt() == 1 )?"金杯":"金龙"; 39 System.out.print("请选择您要租赁的汽车座位数:1、16座 2、34座 "); 40 seatCount = (input.nextInt() == 1 )?16:34; 41 break; 42 } 43 44 //初始化汽车信息 45 vehicleOpr.init(); 46 //租车 47 Vehicle v = vehicleOpr.rentVehicle(brand, seatCount, type); 48 //提示用户租车的车牌号 计算租金(多态,会根据具体返回的汽车子类对象,调用重写后的计算租金方法) 49 System.out.print("请输入您要租赁汽车的天数:"); 50 int days = input.nextInt(); 51 float price = v.clacRent(days); 52 System.out.println("分配给您的汽车牌号为:"+v.getVehicleId()); 53 System.out.println("您需要支付的租赁费用为:"+price+"元"); 54 } 55 }
3 . 子类:客车类
1 package com.vehicles; 2 3 //子类:客车类 4 public class Bus extends Vehicle { 5 //座位数 6 private int seatCount; 7 8 public Bus(){} 9 public Bus(String vehicleId, String brand, int perRent,int seatCount){ 10 super(vehicleId,brand,perRent); 11 this.seatCount = seatCount; 12 } 13 14 public int getSeatCount() { 15 return seatCount; 16 } 17 public void setSeatCount(int seatCount) { 18 this.seatCount = seatCount; 19 } 20 21 //根据客车计算租金的规则重写父类方法 22 public float clacRent(int days) { 23 //租金 = 日租金 * 租赁周期 24 float price = this.getPerRent() * days; 25 //折扣规则 26 if(days>=3 && days<7){ 27 price *= 0.9f; 28 }else if(days>=7 &&days<30){ 29 price *= 0.8f; 30 }else if(days>=30 && days<150){ 31 price *= 0.7f; 32 }else if(days>=150){ 33 price *= 0.6f; 34 } 35 return price; 36 } 37 38 }
4 . 子类:轿车类
1 package com.vehicles; 2 3 //子类:轿车类 4 public class Car extends Vehicle { 5 //型号 6 private String type; 7 8 public Car(){} 9 public Car(String vehicleId, String brand, int perRent,String type){ 10 super(vehicleId,brand,perRent); 11 this.type = type; 12 } 13 14 public String getType() { 15 return type; 16 } 17 18 public void setType(String type) { 19 this.type = type; 20 } 21 22 //根据轿车计算租金的规则重写父类方法 23 public float clacRent(int days) { 24 //租金 = 日租金 * 租赁周期 25 float price = this.getPerRent() * days; 26 //折扣规则 27 if(days>7 && days<=30){ 28 price *= 0.9f; 29 }else if(days>30 &&days<=150){ 30 price *= 0.8f; 31 }else if(days>150){ 32 price *= 0.7f; 33 } 34 return price; 35 } 36 37 }
5 . 父类:汽车类
1 package com.vehicles; 2 3 //父类:汽车类 4 public abstract class Vehicle { 5 //车牌号 品牌 日租金 6 private String vehicleId; 7 private String brand; 8 private int perRent; 9 10 public Vehicle(){} 11 12 13 public Vehicle(String vehicleId, String brand, int perRent) { 14 this.vehicleId = vehicleId; 15 this.brand = brand; 16 this.perRent = perRent; 17 } 18 19 20 public String getVehicleId() { 21 return vehicleId; 22 } 23 public void setVehicleId(String vehicleId) { 24 this.vehicleId = vehicleId; 25 } 26 public String getBrand() { 27 return brand; 28 } 29 public void setBrand(String brand) { 30 this.brand = brand; 31 } 32 public int getPerRent() { 33 return perRent; 34 } 35 public void setPerRent(int perRent) { 36 this.perRent = perRent; 37 } 38 //抽象方法:计算租金-->根据租赁周期来计算租金 39 public abstract float clacRent(int days); 40 }
小伙伴可以多多互动,一起多交流交流!!!O(∩_∩)O
喜欢前端、后端java开发的可以加+qun:609565759,有详细视频、资料、教程,文档,值得拥有!!!希望可以一起努力,加油ヾ(◍°∇°◍)ノ゙!!!
B站有上传的更多项目视频,从Java基础到面向对象、Java高级API,以及SSM等框架的视频,地址:https://www.bilibili.com/video/BV15a411w7Jh