汽车租赁管理系统(java)–隔壁小河
代码亲测有效,各位大佬就不用看了吧,小白看过来呀
一个简单的汽车租赁问题,分为四个类:父类CarHire,子类Bus和Vehicle,再加一个业务类(Service).测试类我可能写的有点复杂------QQ1942295707;欢迎交流
import java.util.Scanner;
abstract class CarHire {
String number;//车牌号
String brand;//品牌
int rent;//租金
CarHire(){}
CarHire(String number, String brand, int rent){
this.number = number;
this.brand = brand;
this.rent = rent;
}
public String getBrand() {
return brand;
}
public int getRent() {
return rent;
}
public String getNumber() {
return number;
}
public abstract int calRent(int days);//计算租金的抽象函数
}
class Bus extends CarHire{
int setCount;//客车座位数
public Bus() {}
public Bus(int setCount, String number, String brand, int rent) {
super(number, brand, rent);
this.setCount = setCount;
}
public int calRent(int days) {
int price;
if (setCount <= 16)
price = days*800;
else
price = days*1000;
return price;
}
}
class Vehicle extends CarHire{
String type;
public Vehicle() {}
public Vehicle(String number, String brand, int rent, String type) {
super(number, brand, rent);
this.type = type;
}
public int calRent(int days) {
int price;
price=days*rent;
return price;
}
public String getType() {
return type;
}
}
class Service{
Vehicle [] motos0 = new Vehicle[4];
Bus [] motos1 = new Bus[4];
Vehicle rentService0=null;
Bus rentService1=null;
public void init1() {
motos0[0]=new Vehicle("京NY28688", "宝马", 880, "林荫大道");
motos0[1]=new Vehicle("京JB38589", "别克", 990, "X9");
motos0[2]=new Vehicle("京DJ52166", "宝马", 600, "GL9");
motos0[3]=new Vehicle("京LI78956", "别克", 300, "500i");
}
public void init2() {
//汽车的初始化数据
motos1[0]=new Bus(16,"京89888", "金龙", 800);
motos1[1]=new Bus(34,"京85688", "金龙", 1500);
motos1[2]=new Bus(16,"京68974", "金杯", 800);
motos1[3]=new Bus(34,"京96854", "金杯", 1500);
}
public Vehicle rentService(String brand, String type){
init1();
Vehicle rentService0 = new Vehicle();
for (int i=0; i<motos0.length; i++) {
if (type.equals(motos0[i].getType()) && brand.equals(motos0[i].getBrand())){
rentService0=motos0[i];break;
}
}
return rentService0;
}
public Bus rentService(String brand, int rent){
init2();
Bus rentService1 = new Bus();
for (int i=0; i<motos1.length; i++) {
if (rent == motos1[i].getRent() && brand.equals(motos1[i].getBrand())){
rentService1=motos1[i];break;
}
}
return rentService1;
}
}
public class Car {
public static void main(String[] args) {
String brand="";//品牌
String type="";//轿车型号
int rent=0;//租金
Scanner input = new Scanner (System.in);
System.out.println("欢迎来到汽车租赁系统:\n请选择汽车车型:1.轿车 2.客车");
int choose;//记录选择的汽车车型
choose = input.nextInt();
boolean flag = false;//用来记录选的是轿车还是客车
switch(choose) {
case 1:
//flag = false;
System.out.println("请选择轿车的品牌:1.宝马 2.别克");
int flash0 = input.nextInt();
switch(flash0) {
case 1:
brand="宝马";
System.out.println("请选择宝马的型号:1.林荫大道 2.GL9");
int book0 = input.nextInt();//book0 记录选择
if (book0 == 1) {
type="林荫大道";break;
}
if (book0 == 2) {
type = "GL9";break;
}
else
System.out.println("输入错误,请输入1或2选择");break;
case 2:
brand = "别克";
System.out.println("请选择别克的型号:1.X9 2.500i");
int book1 = input.nextInt();//book1 记录选择
if (book1 == 1) {
type="X9";break;
}
if (book1 == 2) {
type = "500i";break;
}
else
System.out.println("输入错误,请输入1或2选择");break;
}break;
case 2:
flag = true;
System.out.println("请输入客车的型号:1.金龙 2.金杯");
int flash1 = input.nextInt();
switch(flash1) {
case 1:
brand = "金龙";
System.out.println("请选择金龙的座位数:1.<=16个座位 2.>16个座位");
int book2 = input.nextInt();//book3 记录选择
if (book2 == 1) {
rent= 800;break;
}
if (book2 == 2) {
rent = 1500;break;
}
else
System.out.println("输入错误,请输入1或2选择");break;
case 2:
brand = "金杯";
System.out.println("请选择金杯的座位数:1.<=16个座位数 2.>16个座位数");
int book3 = input.nextInt();//book4 记录选择
if (book3 == 1) {
rent = 800;break;
}
if (book3 == 2) {
rent = 1500;break;
}
else {
System.out.println("输入错误,请输入1或2选择");break;
}
default: System.out.println("输入错误!请输入1或2");
}break;
default: System.out.println("输入错误!请输入1或2");
}
Service book = new Service();//这里的book记录Service的行为
int money;
String number;
System.out.println("请输入租赁的天数:");
int days = input.nextInt();
if (flag == false) {//计算租金
money = book.rentService(brand, type).calRent(days);
number = book.rentService(brand, type).getNumber();//调用轿车的函数
}
else {
money = book.rentService(brand, rent).calRent(days);
number = book.rentService(brand, rent).getNumber();//调用卡车的函数
}
System.out.println("**********您好!租车结果如下**********");
System.out.println("租车成功!您的车是:" + number + " " + brand + " " + type);
System.out.println("您应付的金额为:" + money);
}
}