JAVA入门第二季结课大作业

答答租车系统

Posted by Ruizhi Ma on May 10, 2019

要求:开发一个租车系统。

功能:

  1. 展示所有可租车型
  2. 选择车型,租车量
  3. 展示租车清单,包括:总金额,总载货量,车型,总载人数,以及车型

分析

  1. 数据模型分析:将车的信息抽象为代码表示。车可分为载人的(Saloon Cars),载货的(Trucks),既载人也载货的(Pickups)。所以车的属性应该有车的名字,载客量,载货量。
  2. 业务模型分析:要明确应用程序要执行哪些任务。即考虑用户要执行什么操作。是否租车?租什么车?租几辆?租多久?
  3. 显示和流程分析:用户能看到的界面(目前只能用命令行显示)。除了引导信息,还应该有车的信息表(包括车的编号,名称,租金,载货量和载客量),和最后生成的账单。

代码

Cars父类

package project4;

public abstract class Cars {
	//Define the common properties of cars
	private String carsName;
	private int rentDays;
	private int getPeople;
	private double cargoWeight;
	
	//the setter and getter method of all properties
	public String getCarsName() {
		return carsName;
	}
	public void setCarsName(String carsName) {
		this.carsName = carsName;
	}
	public int getRentDays() {
		return rentDays;
	}
	public void setRentDays(int rentDays) {
		this.rentDays = rentDays;
	}
	public int getGetPeople() {
		return getPeople;
	}
	public void setGetPeople(int getPeople) {
		this.getPeople = getPeople;
	}
	public double getCargoWeight() {
		return cargoWeight;
	}
	public void setCargoWeight(double cargoWeight) {
		this.cargoWeight = cargoWeight;
	}
	
	
}

Pickups子类

package project4;

public class Pickups extends Cars {
	public Pickups(String carsName, int rentDays, int getPeople, double cargoWeight){
		this.setCarsName(carsName);
		this.setRentDays(rentDays);
		this.setGetPeople(getPeople);
		this.setCargoWeight(cargoWeight);
	}
	
	public String toString(){
		return this.getCarsName() + "\t" + this.getRentDays() + "/天" + "\t" + "载货:" + this.getCargoWeight() + "\t" + "载人:" + this.getGetPeople();
	}
}

SaloonCars子类

package project4;

public class SaloonCars extends Cars {
	public SaloonCars(String carsName, int rentDay, int getPeople){
		this.setCarsName(carsName);
		this.setRentDays(rentDay);
		this.setGetPeople(getPeople);
	}
	
	public String toString(){
		return this.getCarsName() + "\t" + this.getRentDays() + "/天" + "\t" + "载人:" + this.getGetPeople() + "\t";
	}
	
}

Trucks子类

package project4;

public class Trucks extends Cars {
	public Trucks(String carsName, int rentDay, int cargoWeight){
		this.setCarsName(carsName);
		this.setRentDays(rentDay);
		this.setCargoWeight(cargoWeight);
	}
	
	public String toString(){
		return this.getCarsName() + "\t" + this.getRentDays() + "/天" + "\t" + "载货:" + this.getCargoWeight() + "\t";
	}
}

测试类

package project4;

import java.util.Scanner;

public class InitialTest {

	public static void main(String[] args) {
		System.out.println("欢迎使用答答租车系统:");
		System.out.println("请问您是否需要租车:1.是 2.否");
		
		//Users print
		Scanner sc = new Scanner(System.in);
		int flag1 = sc.nextInt();
		
		//Deposit cars' information 
		Cars[] allCars = {new SaloonCars("奥迪A4", 800, 4), new SaloonCars("金龙",500,20), 
						new Trucks("东风", 800, 20), new Trucks("解放", 500, 10), 
						new Pickups("皮卡", 800, 4, 5)};
		
		//Judge the print and choose get or exit the system
		if(flag1 == 1){
			System.out.println("您可租车的类型及其价目表:");
			System.out.println("号\t汽车名称\t租金\t容量\t");
			System.out.println((allCars[4].toString()));
			
			//Print all the information of cars
			for(int i = 0; i < allCars.length; i++){
				System.out.println((i + 1) + "\t" + allCars[i].toString());
			}
		}else{
			System.out.println("退出系统");
			System.exit(0);
		}
		
		System.out.println("请输入您需要租车的数量:");
		
		//Users input the amount of rental cars
		int flag2 = sc.nextInt();
		
		//Judge the boundary condition
		if(flag2 < 1){
			System.out.println("数量有错,请重新输入");
			flag2 = sc.nextInt();
		}
		
		//Open up an array to store rental cars
		Cars[] newCars = new Cars[flag2];

		for(int i = 0; i < flag2; i++){
			System.out.println("请输入第" + (i+1) + "辆车的序号");
			
			//Input the sequence number of rental cars
			int flag3 = sc.nextInt();
			
			//Judge the boundary condition
			if(flag3 > 5 || flag3 < 1){
				System.out.println("目标不存在,请重新输入");
				flag3 = sc.nextInt();
			}
			
			//correspond the inputting sequence number to car's number
			newCars[i] = allCars[flag3 - 1];
		}
		
		System.out.println("请输入你需要租的天数:");
		
		//Input total days for rent
		double bills = 0;
		int days = sc.nextInt();
		
		//Count total bills 
		for(int i = 0; i < flag2; i++){
			bills = newCars[i].getRentDays() * days + bills;
		}
		
		int totalPeople = 0;
		double totalWeight = 0;
		
		System.out.println("您的账单信息如下:");
		System.out.println("***可以载人的车有:");
		
		//Count numbers of total people
		for(int i = 0; i<flag2; i++){
			if(newCars[i].getGetPeople() != 0){
				System.out.println(newCars[i].getCarsName() + "\t");
				totalPeople += newCars[i].getGetPeople();
			}
		}
		
		//Output numbers of total people
		if(totalPeople == 0){
			System.out.println("没有可以载人的车");
		}else{
			System.out.println("总载客数为:" + totalPeople + "人");
		}
		
		
		System.out.println("***可以载人的车有:");
		//Count numbers of total weight
		for(int i = 0; i < flag2; i++){
			if(newCars[i].getCargoWeight() != 0){
				System.out.println(newCars[i].getCarsName() + "\t");
				totalWeight += newCars[i].getCargoWeight();
			}
		}
		
		//Output numbers of total weight
		if(totalWeight == 0){
			System.out.println("没有可以载货的车");
		}else{
			System.out.println("总载货量为:" + totalWeight + "吨");
		}
		
		//Output total expense of rental bills
		System.out.println("总金额:" + bills);
	}

}

运行结果:

欢迎使用答答租车系统:
请问您是否需要租车:1.是 2.否
1
您可租车的类型及其价目表:
号 汽车名称 租金 容量
皮卡 800/天 载货:5.0 载人:4
1 奥迪A4 800/天 载人:4
2 金龙 500/天 载人:20 3 东风 800/天 载货:20.0
4 解放 500/天 载货:10.0
5 皮卡 800/天 载货:5.0 载人:4
请输入您需要租车的数量:
4
请输入第1辆车的序号
1
请输入第2辆车的序号
2
请输入第3辆车的序号
3
请输入第4辆车的序号
5
请输入你需要租的天数:
10
您的账单信息如下:
**可以载人的车有:
奥迪A4 金龙 皮卡 总载客数为:28人
**
可以载人的车有:
东风
皮卡
总载货量为:25.0吨
总金额:29000.0

小总结

这是第一次写这么完整的一个小程序,虽然很简陋,而且自己一开始也无从下手,多方借鉴别人写出来的程序,最后完成了代码,深感于面向对象编程的优越性。同时,有几点总结如下:
第一,在父类中定义车的共同属性(用private修饰符),并且在父类中生成get和set方法,提供给子类继承。
第二,子类在初始化的时候,可以通过构造方法进行初始化属性,并且通过重写toString方法将属性打印出来(否则toString出来的是内存地址)。
第三, 测试类中,通过父类的索引执行子类的对象更符合书写规范和逻辑。
目前独立完成整个程序还是有困难的,但磕磕绊绊写完整个程序之后,觉得把Java入门第一季和第二季所有的知识点,将Java语言三大特性:继承,多态,重载全部用上了,收益颇丰,继续学习Java入门第三季了!!!