package com.tingyunguan;
public abstract class Car {//定义一个Car父类
public String Name;//用于接收汽车的名字
public int money;//用于接收每天租用所需费用
public int carry;//接收载货量
public int men;//载人数
//返回各个参数
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getCarry() {
return carry;
}
public int getMen() {
return men;
}
}
package com.tingyunguan;
public class Audi extends Car {//继承Car父类
public Audi(String Name, int money,int men) {//定义构造方法并接收车 名字 价格 载人量
this.Name=Name;
this.money=money;
this.men=men;
}
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getMen() {
return men;
}
}
package com.tingyunguan;
public class Iveco extends Car{
public Iveco(String Name, int money,int carry) {//定义构造方法并接收车 名字 价格 载货量
this.Name=Name;
this.money=money;
this.carry=carry;
}
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getCarry() {
return carry;
}
}
package com.tingyunguan;
public class Kinglong extends Car {
public Kinglong(String Name, int money,int men) {//定义构造方法并接收车 名字 价格 载人量
this.Name=Name;
this.money=money;
this.men=men;
}
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getMen() {
return men;
}
}
package com.tingyunguan;
public class Mazda extends Car {
public Mazda(String Name, int money, int men) {//定义构造方法并接收车 名字 价格 载人量
this.Name=Name;
this.money=money;
this.men=men;
}
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getMen() {
return men;
}
}
package com.tingyunguan;
public class pickup extends Car {
public pickup(String Name, int money, int men, int carry) {//定义构造方法并接收车 名字 价格 载人量 和载货量
this.Name=Name;
this.money=money;
this.carry=carry;
this.men=men;
}
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getCarry() {
return carry;
}
public int getMen() {
return men;
}
}
package com.tingyunguan;
public class Songhua extends Car {
public Songhua(String Name, int money,int carry) {//定义构造方法并接收车 名字 价格 载货量
this.Name=Name;
this.money=money;
this.carry=carry;
}
public String getName() {
return Name;
}
public int getMoney() {
return money;
}
public int getCarry() {
return carry;
}
}
package com.tingyunguan;
/*****项目名:答答租车系统
-
*****制作日期:2016.8.18
-
**制作人:4666-3545
-
*/
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//创建一个Car类数组,保存各个子类对象,并传入参数
Car[] carForRent = { new Audi("奥迪A4", 500, 4), new Mazda("马自达6", 600, 4), new pickup("皮卡雪6", 450, 4, 2),
new Kinglong("金龙", 800, 20), new Songhua("松花江", 400, 4), new Iveco("依维柯", 1000, 20) };
Scanner scan = new Scanner(System.in);//创建Scanner对象
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1.是 0否");
int input = scan.nextInt();//接收用户输入的数字
if (input == 0) {//判断用户输入的数字是否符合要求
System.out.println("退出系统");
} else if (!(input == 1) && !(input == 0)) {
System.out.println("输入错误");
} else if (input == 1) {
System.out.println("您可租车的类型及其价格目录:");
System.out.println("序号\t汽车名字\t租金\t容量");
for (int i = 0; i < carForRent.length; i++) {//循环遍历子类中,并打印其信息
if (i == 0 i == 1 i == 3) {
System.out.println(i + 1 + ".\t" + carForRent[i].getName() + "\t" + carForRent[i].getMoney()
- "元/天\t载人:" + carForRent[i].getMen() + "人");
} else if (i == 4 i == 5) {
System.out.println(i + 1 + ".\t" + carForRent[i].getName() + "\t" + carForRent[i].getMoney() - "元/天\t载货:" + carForRent[i].getCarry() + "吨");
} else if (i == 2) {
System.out.println(i + 1 + ".\t" + carForRent[i].getName() + "\t" + carForRent[i].getMoney() - "元/天\t载人:" + carForRent[i].getMen() + "人" + "载货:" + carForRent[i].getCarry() + "吨");
}
}
System.out.println("输入您要租汽车的数量:");
int num=0;//保存用户输入的车辆序列号
int day=0;//用户保存天数
int peopleNum = 0;//人数
int carrys=0;//载货量
int moneys=0;//每天租金
int peopleSum=0;//人数总和
int carrySum=0;//载货量总和
int moneySum=0;//租金总和
String carName = "";//载客汽车名字
String freightCar="";//载货汽车名字
String freightCarSum="";//载货汽车名字 总和
String carNameSum = "";//载客汽车名字 总和
int number = scan.nextInt();//用户输入所需汽车数量
for (int i = 1; i <= number; i++) {//循环输入所需汽车序列号
System.out.println("请输入第"+i+"辆车的序号");
num=scan.nextInt();//用户输入序列号
if(num==1num==2num==4){//判断用户输入序列号是否为1,2,4
peopleNum=carForRent[num-1].getMen();//获取数组中第num-1个数组成员的值
moneys=carForRent[num-1].getMoney();
carName=carForRent[num-1].getName();
carNameSum=carNameSum+" "+carName;//保存载客车来那个名称
peopleSum+=peopleNum;
}else if(num==5num==6){//判断用户输入序列号是否为5,6
carrys=carForRent[num-1].getCarry();
moneys=carForRent[num-1].getMoney();
freightCar=carForRent[num-1].getName();
freightCarSum=freightCarSum+" "+freightCar;//保存载货车辆名称
carrySum+=carrys;
}else if(num==3){//判断用户输入序列号是否为3
peopleNum=carForRent[num-1].getMen();
carrys=carForRent[num-1].getCarry();
moneys=carForRent[num-1].getMoney();
carName=carForRent[num-1].getName();
freightCar=carForRent[num-1].getName();
carNameSum=carNameSum+" "+carName;
freightCarSum=freightCarSum+" "+freightCar;
peopleSum+=peopleNum;
carrySum+=carrys;
}
moneySum+=moneys;
}
System.out.println("请输入租车的天数:");
day=scan.nextInt();
System.out.println("您的账单:");
System.out.println("可载人的车有:");
System.out.println(carNameSum+" 共载人:"+peopleSum);
System.out.println("可载货的车有:");
System.out.println(freightCarSum+" 共载货:"+carrySum);
int sum=moneySum*day;//计算所需总金额
System.out.println("租车总价格为"+sum+"");
}
}
}