建立数据包,建立Car父类,编辑构造方法,编辑getinfo方法
package daDaZuCheXiTong;
public class Car {
String name;
int id;
int rent;
int seat;
int capacity;
public Car(int id, String name,int rent, int seat, int capacity){
this.id = id;
this.name = name;
this.rent = rent;
this.seat = seat;
this.capacity = capacity;
}
public String getinfo(){
return id + ".\t" + name + "\t" + seat + "\t" + capacity + "\t" + rent;
}
}
建立Sedan子类和Truck子类,继承Car父类,利用super关键字调用父类构造方法,不需要赋值的变量赋值为零,重写getinfo方法,不需要显示的变量值设置返回值为N/A。
package daDaZuCheXiTong;
public class Sedan extends Car {
public Sedan(int id,String name,int rent, int seat){
super(id, name, rent, seat, 0);
}
public String getinfo(){
return id + ".\t" + name + "\t" + seat + "\t" + "N/A" + "\t" + rent;
}
}
package daDaZuCheXiTong;
public class Truck extends Car {
public Truck(int id,String name,int rent, int capacity){
super(id, name, rent, 0, capacity);
}
public String getinfo(){
return id + ".\t" + name + "\t" + "N/A" + "\t" + capacity + "\t" + rent;
}
}
编写主方法,导入Scanner数据包,利用try catch finally结构,在try里面利用switch case循环编写程序主体,如果在主体中出现问题,利用catch语句记录异常,最后在finally语句中close掉输入变量
package daDaZuCheXiTong;
import java.util.Scanner;
public class XiTong {
public static void main(String[] args) {
int totalSeat = 0;
int totalCapacity = 0;
int totalRent = 0;
int choose = 0;
String truckStr = "";
String SedanList = "";
Scanner input1 = null;
Scanner input2 = null;
Scanner input3 = null;
Scanner input4 = null;
Car[] car = { new Sedan(1, "奥迪A6", 500, 4),
new Sedan(2, "马自达6", 400, 4),
new Sedan(3, "雪铁龙", 450, 4),
new Sedan(4, "荣威RX5", 500, 4),
new Sedan(5, "金江大客", 8000, 20),
new Truck(6, "松花江", 4000, 4) };
System.out.println("欢迎使用达达租车系统!");
System.out.println("请问你是否要继续 ? 1:继续 2:不继续");
try {
input1 = new Scanner(System.in);
input2 = new Scanner(System.in);
input3 = new Scanner(System.in);
input4 = new Scanner(System.in);
choose = input1.nextInt();
switch (choose) {
case 1:
System.out.println("您可选择的车型为:");
System.out.println("序号\t汽车名称\t载人(人)\t载货(吨)\t租金(元/天)\t");
for (Car a : car) {
System.out.println(a.getinfo());
}
System.out.println("请输入你想租赁车的数量:");
int carNum = input2.nextInt();
int[] carDay = new int[carNum];
for (int i = 0; i < carNum;) {
System.out.println("请输入您想租赁的第" + (i + 1) + "辆车的序号");
int carId = input3.nextInt();
if (carId < 1 || carId > 6) {
System.out.println("您输入的序号有误,请重新输入!");
continue;
}
System.out.println("请输入你想租这辆车的天数");
carDay[i] = input4.nextInt();
totalSeat += car[(carId - 1)].seat;
totalCapacity += car[(carId - 1)].capacity;
totalRent += (car[(carId - 1)].rent) * carDay[i];
if (car[(carId - 1)].capacity != 0) {
truckStr = truckStr + car[(carId - 1)].name + ": "
+ carDay[i] + " " + "天"+" ";
}
if (car[(carId - 1)].seat != 0) {
SedanList = SedanList + car[(carId - 1)].name + ": "
+ carDay[i] + " " + "天"+" ";
}
i++;
}
System.out.println("您的账单如下:");
if (truckStr == "") {
System.out.println("您没有租赁可以载货的车辆");
} else {
System.out.println("您租赁的可载货的车辆以及天数为:" + truckStr);
System.out.println("总载货量:" + totalCapacity + "吨"+" ");
}
if (SedanList == "") {
System.out.println("您没有租赁可以载人的车辆");
} else {
System.out.println("您租赁的可载人的车辆以及天数为:" + SedanList);
System.out.println("总载人数:" + totalSeat + "人"+" ");
}
if (totalRent != 0) {
System.out.println("您租赁车辆的总价格为" + totalRent + "元");
} else {
System.out.println("感谢您的使用!");
}
break;
case 2:
System.out.println("感谢您的使用!");
break;
default: {
System.out.println("您输入的信息有误");
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (input1 != null) {
input1.close();
}
if (input2 != null) {
input2.close();
}
if (input3 != null) {
input3.close();
}
if (input4 != null) {
input4.close();
}
}
}
}