父类Car
package com.imooc;
public abstract class Car {
//定义全局变量,+m用于区分参数
public String mName;
public int mMoney;
public int mPeopleNum;
public int mThingNum;
//定义抽象方法用于创建租车类型与价目表
public abstract void show();
}
子类——载人车类
package com.imooc;
public class People extends Car {
// 定义构造方法,用于保存传递进来的name,money,peopleNum;
public People(String name, int money, int peopleNum) {
this.mName = name;// 将传递进来的name参数保存在当前对象中的mName属性中,nName是继承父类Car所得到的,下同
this.mMoney = money;
this.mPeopleNum = peopleNum;
}
public void show() {
// TODO Auto-generated method stub
System.out.println(mName + "\t\t" + mMoney + "天/元\t\t" + mPeopleNum
+ "人");// "\t"为制表符,为了让格式更好看
}
}
子类——载物车类
package com.imooc;
public class Thing extends Car {
public Thing(String name, int money, int thingNum) {
this.mName = name;// 将传递进来的name参数保存在当前对象中的mName属性中,nName是继承父类Car所得到的,下同
this.mMoney = money;
this.mThingNum = thingNum;
}
public void show() {
// TODO Auto-generated method stub
System.out.println(mName + "\t\t" + mMoney + "天/元\t\t" + mThingNum
+ "吨");// "\t"为制表符,为了让格式更好看
}
}
子类——皮卡类
package com.imooc;
public class PeopleAndThing extends Car {
public PeopleAndThing(String name, int money, int peopleNum, int thingNum) {
this.mName = name;// 将传递进来的name参数保存在当前对象中的mName属性中,nName是继承父类Car所得到的,下同
this.mMoney = money;
this.mPeopleNum = peopleNum;
this.mThingNum = thingNum;
}
public void show() {
// TODO Auto-generated method stub
System.out.println(mName + "\t\t" + mMoney + "天/元\t\t" + mPeopleNum
+ "人," + mThingNum + "吨");// "\t"为制表符,为了让格式更好看
}
}
函数主入口
package com.imooc;
import java.util.Scanner;//导入工具类Scanner用于输入数据
public class Main {
public static void main(String[] args) {
Car[] cars = { new People("奥迪A4", 500, 4), new People("马自达", 400, 4),
new PeopleAndThing("皮卡雪6", 450, 4, 2),
new People("金龙", 800, 20), new Thing("松花江", 400, 4),
new Thing("依维柯", 1000, 20) };// 用多态的方式初始化对象,并将对象保存在数组cars里
// new+构造方法(参数) = 对象
System.out.println("欢迎使用答答租车系统!您是否要租车:");
System.out.println("Y 是/ N 否");
Scanner scanner = new Scanner(System.in); // 创建Scanner对象
// ,并将地址传给引用类型变量scanner
String input = scanner.next(); // 将输入的值传给新定义的String类型的input
/**
* 判断输入的值,如果是其他数则一直循环,是Y跳出循环,输入N结束程序
*/
while (true) {
if (input.equals("Y") || input.equals("y")) {
System.out.println("序号\t\t汽车名称\t\t租金\t\t容量");
for (int i = 0; i < cars.length; i++) {// 通过遍历cars数组来访问对象
System.out.print(i + 1 + "\t\t");// 创建序号,注意用print而不是println
cars[i].show();// 调用各个对象的show()方法以显示菜单
}
break;
} else if (input.equals("N") || input.equals("n")) {
System.out.println("欢迎下次光临!");
System.exit(0);
} else {
System.out.println("输入有误,请重新输入");
input = scanner.next();// 重新将输入的值赋给input
}
}
System.out.println("请输入租车的数量:");
int nums = scanner.nextInt();// 定义nums保存车辆数量
int sumPeople = 0;
int sumThing = 0;
int sumMoney = 0;//一天的总金额
int sp = 0;// 可载人车的数量
int st = 0;// 可载物车的数量
String[] choicePeopleCarName = new String[nums];// 可载人车的名字数组
String[] choiceThingCarName = new String[nums];// 可载物车的名字数组
Car[] choiceCars = new Car[nums];// 定义初始化数组用于保存被选中的车辆对象
for (int i = 0; i < nums; i++) {
System.out.println("请输入车辆序号:");
int num = scanner.nextInt();// 定义num保存车辆序号
/**
* 判断车辆序号的正确性,如果有误,则使这次循环无效
*/
if(num > 6 || num <= 0){
System.out.println("输入有误,请输入正确的车辆序号!");
num = scanner.nextInt();
i--;
continue;
}
choiceCars[i] = cars[num - 1];// num-1是因为序号没有0,而数组有
if (choiceCars[i] instanceof People
|| choiceCars[i] instanceof PeopleAndThing) {
choicePeopleCarName[sp] = choiceCars[i].mName;
sp++;
sumPeople += choiceCars[i].mPeopleNum;
}
if (choiceCars[i] instanceof Thing
|| choiceCars[i] instanceof PeopleAndThing) {
choiceThingCarName[st] = choiceCars[i].mName;
st++;
sumThing += choiceCars[i].mThingNum;
}
sumMoney += choiceCars[i].mMoney;
}
System.out.println("请输入租车天数:");
int days = scanner.nextInt();
scanner.close();// 关闭输入流
System.out.println(">>>>>>>>>>>>>>>>>账单<<<<<<<<<<<<<<<<<<");
System.out.println("您所租车的总数量为:" + nums);
System.out.println("您所租的总天数为:" + days);
System.out.println("可载人车辆为:");
for (int i = 0; i < sp; i++) {
System.out.print(choicePeopleCarName[i] + " ");
}
System.out.println("可载人数为:" + sumPeople);
System.out.println("可载物车辆为:");
for (int i = 0; i < st; i++) {
System.out.print(choiceThingCarName[i] + " ");
}
System.out.println("可载物数为:" + sumThing);
System.out.println("总金额为:" + (sumMoney * days));
}
}