一、使用技术
javaSE
二、实现功能
汽车租赁系统
具体要求如下:
使用集合,面向对象的知识实现一个汽车租赁系统b
1.汽车租赁信息表如下
2.类和属性
三、运行效果图如下:
汽车类:
//父类
public abstract class Vehicle {
private String num;
private String brand;
private double rent;
@Override
public String toString() {
return "汽车{" +
"车牌号='" + num + '\'' +
", 品牌='" + brand + '\'' +
", 日租金=" + rent +
'}';
}
//不重写Object ,重写汽车父类的
public abstract boolean equals(Vehicle o) ;
@Override
public int hashCode() {
return Objects.hash(brand);
}
public String getnum() {
return num;
}
public void setnum(String num) {
num = num;
}
public String getbrand() {
return brand;
}
public void setbrand(String brand) {
brand = brand;
}
public double getrent() {
return rent;
}
public void setrent(double rent) {
rent = rent;
}
public Vehicle(){}
public Vehicle(String num, String brand, double rent){
this.num = num;
this.brand = brand;
this.rent = rent;
}
//计算租金
public abstract double totalMoney(int Days, double rent);
}
客车类:
public class Bus extends Vehicle {
private int seat;
@Override
public String toString() {
return super.toString() +
"座位数=" + seat ;
}
public int getseat() {
return seat;
}
public void setseat(int seat) {
seat = seat;
}
public Bus(){}
public Bus(String PlateNumber, String brand, double rent, int seat){
super(PlateNumber, brand, rent);
this.seat = seat;
}
//2个参数的有参构造
public Bus(String brand, int seat){
super.setbrand(brand);
this.seat = seat;
}
public boolean equals(Vehicle o) {
if(o instanceof Bus){
//比较品牌(父类的)和座位数(自己的)
Bus p = (Bus) o;
return this.getbrand().equals(o.getbrand())&&this.getseat()==p.getseat();
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), seat);
}
@Override
public double totalMoney(int Days, double rent){
if (Days>=150){
return Days*rent*0.6;
}else if (Days>=30){
return Days*rent*0.7;
}else if (Days>=7){
return Days*rent*0.8;
}else if (Days>=3){
return Days*rent*0.9;
}else {
return Days*rent;
}
}
}
轿车类:
public class Cars extends Vehicle {
private String type;
@Override
public String toString() {
return super.toString()+
"类型=" + type + '\'' ;
}
public String gettype() {
return type;
}
public void settype(String type) {
type = type;
}
public Cars(){}
public Cars(String PlateNumber,String brand,double DailyRent,String type){
super(PlateNumber, brand, DailyRent);
this.type = type;
}
//2个参数有参构造
public Cars(String brand,String type){
super.setbrand(brand);
this.type = type;
}
public boolean equals(Vehicle o) {
if(o instanceof Cars){
Cars cars=(Cars)o;
//比较品牌和类型,一个是特有的属性,一个是父类的属性
return this.gettype().equals(cars.gettype())&&this.getbrand().equals(o.getbrand());
}
return false;
}
@Override
public double totalMoney(int Days, double DailyRent){
if (Days>150){
return Days*DailyRent*0.7;
}else if (Days>30){
return Days*DailyRent*0.8;
}else if (Days>7){
return Days*DailyRent*0.9;
}else {
return Days*DailyRent;
}
}
}
汽车租赁管理类:
public class TestVehicle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//初始化集合,将8辆车存入集合中
List<Vehicle> Cars = VehicleService.initVehicle();
System.out.println("* * * * * * * * * *欢迎光临秋名山守望者汽车租赁公司* * * * * * * * * *");
System.out.println("1、轿车 2、客车");
System.out.print("请选择你要租赁的汽车类型:");
int type = sc.nextInt();
//定义3个属性备用,分别属于客车或汽车
String brank;//品牌
String model;//型号
int b;
//父类引用类型,接收子类对象
Vehicle automobile;
if (type==1){//轿车
System.out.println("请选择你要租赁的汽车品牌(1、别克 2、宝马):");
b = sc.nextInt();
brank = b==1?"别克":"宝马";
if (b==1){
System.out.println("请输入你要租赁的汽车类型:1、林荫大道 2、GLB");
model = sc.nextInt()==1?"林荫大道":"GLB";
}else {
System.out.println("请输入你要租赁的汽车类型:1、X6 2、550i");
model = sc.nextInt()==1?"X6":"550i";
}
automobile = new Cars(brank,model);
}else {//客车
System.out.println("请选择你要租赁的汽车品牌:1、金杯 2、金龙");
brank = sc.nextInt()==1?"金杯":"金龙";
System.out.println("请选择你要租赁的汽车座位数;1、16座 2、34座");
int seating = sc.nextInt()==1?16:34;
automobile = new Bus(brank,seating);
}
//根据选好的车型,输出车牌和总价
for (Vehicle a:Cars) {
//根据用户输入的两个属性构建的对象和集合中的对象比较:a有完整的属性值,automobile只有2个属性值
if (a.equals(automobile)) {
System.out.println("请输入您要租赁的天数:");
int days = sc.nextInt();
System.out.println("分配给您的汽车牌号是:"+a.getnum());
System.out.println("您需要支付的租赁费用是:"+a.totalMoney(days,a.getrent()));
break;
}
}
}
}
汽车业务类:
public class VehicleService {
public static List initVehicle(){
Vehicle c1 = new Cars("京NT37465","别克",300,"林荫大道");
Vehicle c2 = new Cars("京NT96968","别克",600,"GLB");
Vehicle c3 = new Cars("京NY28588","宝马",800,"X6");
Vehicle c4 = new Cars("京CNY3284","宝马",600,"550i");
Vehicle p1 = new Bus("京6566754","金杯",800,16);
Vehicle p2 = new Bus("京9696996","金杯",1500,34);
Vehicle p3 = new Bus("京8696997","金龙",800,16);
Vehicle p4 = new Bus("京8696998","金龙",1500,34);
//先装入数组中
Vehicle[] ve = {c1,c2,c3,c4,p1,p2,p3,p4};
//将数组转换成集合
List<Vehicle> vehicles = Arrays.asList(ve);
return vehicles;
}
}