租车用例 (大连大学 Java作业)

咱们的Java老师最近留了一个租车项目,我简单的写了一下希望能给大家一些帮助
首先我们先来看一下我们的运行效果:

要实现这样一个效果我们需要用到java类继承重写的一些知识,以及java的基础知识。
首先我们先看看这个项目的中的文件:

我们项目中一共有五个java文件,每个文件都有它存在的意义,彼此之间也有着千丝万缕的联系。
下面我们就先来看看我们的Vehicle.java

public abstract class Vehicle {
	//车牌号
	public String[][][] Vehicle_id;
	//品牌
	public static String[]Brand;
	//日租金
	public static double[][] perRent;
	//计算租金
	public abstract double calRent(int days);
}

对于轿车、货车而言它们的属性肯定不会完全一样。所以我们定义抽象类的时候只找它们的共性,也就是它们的交集。

接下来我们看看 Cars.java 文件中是如何继承实现 Vehicle.java吧

public class Cars extends Vehicle {
    //获取用户选择的品牌号
	public static int select_Brand;
	//获取用户选择的车型号
	public static int select_Type;
	//车型号构成的二维数组
	public static String[][] Model;
	//Setter
	public static void setSelect_Brand(int select_Brand) {
	//由于用户前面的选择号里没有0,最小是1
	//所以-1
		Cars.select_Brand = select_Brand - 1;
	}
	//Setter
	public static void setSelect_Type(int select_Type) {
		Cars.select_Type = select_Type - 1;
	}
	//计算租金
	@Override
	public double calRent(int days) {
		// TODO 自动生成的方法存根
		double rent_Money = 0;
		if (days < 7) {
			rent_Money = Vehicle.perRent[select_Brand][select_Type] * days;
		} else if (days < 30) {
			rent_Money = Vehicle.perRent[select_Brand][select_Type] * days * 0.9;
		} else if (days < 150) {
			rent_Money = Vehicle.perRent[select_Brand][select_Type] * days * 0.8;
		} else {
			rent_Money = Vehicle.perRent[select_Brand][select_Type] * days * 0.7;
		}
		return rent_Money;
	}

	static {
	    //品牌数组
		Vehicle.Brand = new String[] { "宝马", "奔驰", "奥拓", "别克" };
		//每个品牌有多个型号
		//型号二维数组
		Cars.Model = new String[][] { { "BMW1", "BMW2", "BMW3", "BMW4" }, { "W124", "W140", "W121", "W201" },
				{ "AT1002", "AT1030", "AT8556", "S900" }, { "BK0101", "BK1010", "Bk0909" } };
		//每个型号都有日租金
		//日租金二维数组 对应各个型号
		Vehicle.perRent = new double[][] { { 600, 673, 678, 800 }, { 300, 500, 689, 788 }, { 298, 308, 426, 783 },
				{ 1200, 1543, 1900 } };
	}
}

我们再来看看Bus.java

public class Bus extends Vehicle {
    //记录座位数的二维数组
	public static int[][] seatCount;
	//获取用户选择的 座位数
	public static int choice_seat;
	//获取用户选择的品牌
	public static int choice_brand;
	//Setter
	public static void setChoice_seat(int choice_seat) {
	//由于用户前面的选择号里没有0,最小是1
	//所以-1
		Bus.choice_seat = choice_seat - 1;
	}
	//Setter
	public static void setChoice_brand(int choice_brand) {
		Bus.choice_brand = choice_brand - 1;
	}

	@Override
	public double calRent(int days) {
		// TODO 自动生成的方法存根
		double rent_Money = 0;
		if (days < 3) {
			rent_Money = Vehicle.perRent[choice_brand][choice_seat];
		} else if (days < 7) {
			rent_Money = Vehicle.perRent[choice_brand][choice_seat] * 0.9;
		} else if (days < 30) {
			rent_Money = Vehicle.perRent[choice_brand][choice_seat] * 0.8;
		} else if (days < 150) {
			rent_Money = Vehicle.perRent[choice_brand][choice_seat] * 0.7;
		} else {
			rent_Money = Vehicle.perRent[choice_brand][choice_seat] * 0.6;
		}
		return rent_Money;
	}

	static {
		Vehicle.Brand = new String[] { "金杯", "腾跃", "海格", "安凯" };
		Vehicle.perRent = new double[][] { { 800, 1500 }, { 800, 1500 }, { 800, 1500 }, { 800, 1500 } };
		seatCount = new int[][] { { 16, 34 }, { 16, 34 }, { 16, 34 }, { 16, 34 } };
	}
}

我们看看Factory.java是如何分配车牌号的:

public class Factory {
    //获取用户选择的品牌号
	public static int brand_is_selected;
	//获取用户选择的型号
	public static int type_is_selected;
	//循环遍历变量
	static int i=0;
	static int q=0;
	//获取数组长度
	static int length;
	//构造方法传入对象
	public Factory(Object obj) {
		// TODO 自动生成的构造函数存根
		//情况一:传入的是Cars对象
		if(obj instanceof Cars){
		    //object类强制转换为Cars类
		    //并承接对象
			Cars cars=(Cars)obj;
			//获取选择的品牌
			brand_is_selected=cars.select_Brand;
			//获取选择的型号
			type_is_selected=cars.select_Type;
			//这是一个三维数组
			//为什么是三维数组?
			//因为每个品牌有多个型号
			//而每个型号的车有多个车牌号
			//所以就三维数组了
			//我们通过用户选择的品牌号和型号来确定究竟派出哪一个车牌号数组
			cars.Vehicle_id=new String[][][] {
				{ { "辽A7365", "辽B9321" }, { "辽B8341", "辽B8412", "辽B7865" }, { "辽B8767", "辽B3212" },
					{ "辽C3818", "辽C1542" } },
			{ { "辽B1203", "辽B9087" }, { "辽B1243", "辽B4356" }, { "辽C6787", "辽C0978" }, { "辽C9089", "辽C8976" } },
			{ { "辽B87ac" }, { "辽A7356" }, { "辽C7567", "辽C8756" }, { "辽Cjk90" } },
			{ { "辽A7341", "辽B7654" }, { "辽C9076", "辽A9087" }, { "辽D8976", "辽C7865" } } };
			//获取数组长度
			length=cars.Vehicle_id[brand_is_selected][type_is_selected].length;
			System.out.println("车牌号:"+vehicleid(cars.Vehicle_id[brand_is_selected][type_is_selected][i]));
			//为了防止越界循环取余数派单
			i++;
			i=i%length;
		}
		else{
		//和Cars类似
			Bus buses=(Bus)obj;
			brand_is_selected=buses.choice_brand;
			type_is_selected=buses.choice_seat;
			//这是一个三维数组
			//为什么是三维数组?
			//因为每个品牌有多个型号
			//而每个型号的车有多个车牌号
			//所以就三维数组了
			//我们通过用户选择的品牌号和型号来确定究竟派出哪一个车牌号数组
			buses.Vehicle_id=new String[][][] { { { "辽A76bd", "辽B92o3" }, { "辽B843l", "辽B80k2", "辽B7p65" } },
				{ { "辽B1b03", "辽B9l87" }, { "辽B1o43", "辽B4j56" } }, { { "辽B8jac" }, { "辽Ab356" } },
				{ { "辽A0341", "辽Bj654" }, { "辽C9076", "辽A9p87" } } };
				length=buses.Vehicle_id[brand_is_selected][type_is_selected].length;
				System.out.println("车牌号:"+vehicleid(buses.Vehicle_id[brand_is_selected][type_is_selected][q]));
				q++;
				q=q%length;	
		}
	}
	public static String vehicleid(String number){
		return number;
	}
}

关于三维数组大家看不懂我说一下:
大家对照着看:

//这是品牌
Vehicle.Brand = new String[] { "宝马", "奔驰", "奥拓", "别克" };
//每个品牌有多个型号
//最外面的括号不要看,一个括号一个括号看
Cars.Model = new String[][] { { "BMW1", "BMW2", "BMW3", "BMW4" }, { "W124", "W140", "W121", "W201" },
				{ "AT1002", "AT1030", "AT8556", "S900" }, { "BK0101", "BK1010", "Bk0909" } };
//每个型号有指定的那几个车牌号
//这个三维数组就是把BMW1换成{"辽A7365", "辽B9321"}了
//把BMW2换成{ "辽B8341", "辽B8412", "辽B7865" }了
//对照二维数组看是不是容易多了
cars.Vehicle_id=new String[][][] {
				{ { "辽A7365", "辽B9321" }, { "辽B8341", "辽B8412", "辽B7865" }, { "辽B8767", "辽B3212" },
					{ "辽C3818", "辽C1542" } },
			{ { "辽B1203", "辽B9087" }, { "辽B1243", "辽B4356" }, { "辽C6787", "辽C0978" }, { "辽C9089", "辽C8976" } },
			{ { "辽B87ac" }, { "辽A7356" }, { "辽C7567", "辽C8756" }, { "辽Cjk90" } },
			{ { "辽A7341", "辽B7654" }, { "辽C9076", "辽A9087" }, { "辽D8976", "辽C7865" } } };

最后我们看看Main.java主函数怎么写的:

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		char ch=' ';
		boolean b=true;
		do{
			System.out.println("***********腾飞汽车租借有限公司欢迎您**********");
			System.out.println("+++++++++请选择服务项目+++++++++");
			System.out.println("1 轿车服务                      2 客车服务");
			int i0=sc.nextInt();

			if(i0==1){
				Cars cars=new Cars();
				System.out.println("*********请选择品牌*********");
				System.out.println("1 宝马          2 奔驰           3 奥拓           4 别克");
				int i1=sc.nextInt();
				//获取品牌号
				Cars.setSelect_Brand(i1);
				i1--;
				System.out.println("*********请选择型号*********");
				for(int i=0;i<Cars.Model[i1].length;i++){
					System.out.print((i+1)+" "+Cars.Model[i1][i]+"    ");
				}
				System.out.println();
				int i2=sc.nextInt();
				//获取种类号
				Cars.setSelect_Type(i2);
				i2--;
				System.out.println("您需要支付的费用"+Vehicle.perRent[i1][i2]);
				System.out.println("是否支付?   Y/N");
				ch=sc.next().charAt(0);
				if(ch=='Y'||ch=='y'){
					System.out.println("***********租车成功************");
					System.out.println("品牌:"+Vehicle.Brand[i1]);
					System.out.println("型号:"+Cars.Model[i1][i2]);
					new Factory(cars);
				}
				else{
					continue;
				}
				
			}else{
				Bus bus=new Bus();
				System.out.println("*********请选择品牌*********");
				System.out.println("1 金杯       2 腾跃        3 海格            4 安凯");
				int i1=sc.nextInt();
				Bus.setChoice_brand(i1);
				i1--;
				System.out.println("*********请选择座位数目*********");
				System.out.println("1 16座位                2 34座位");
				int i2=sc.nextInt();
				Bus.setChoice_seat(i2);
				i2--;
				System.out.println("您需要支付的费用"+Vehicle.perRent[i1][i2]);
				System.out.println("是否支付?   Y/N");
				ch=sc.next().charAt(0);
				if(ch=='Y'||ch=='y'){
					System.out.println("***********租车成功************");
					System.out.println("品牌:"+Vehicle.Brand[i1]);
					System.out.println("座位:"+Bus.seatCount[i1][i2]);
					new Factory(bus);
				}
				else{
					continue;
				}
				
			}
		}while(b);
		
	}
}

好了这回就可以实现租车项目了

发布了2 篇原创文章 · 获赞 4 · 访问量 147

猜你喜欢

转载自blog.csdn.net/chengqige/article/details/105262252