代码参考了@nuanchazy的手记
创建了Vehicle父类,Car、Cargo、Pickup子类,没有使用接口。
Test.java
```package com.DaDa;
import java.util.Scanner;
public class Test {
int sumPeople;//总载客量
int sumCargo;//总载货量
int sumPrice;//总价格
String cargoName="";//可载货的汽车名称
String carName="";//可载人的汽车名称
//询问是否使用系统
public void start() {
System.out.println("欢迎使用答答租车系统:\n您是否要租车:1.是 0.否");
Scanner sc1=new Scanner(System.in);
int sc1a=sc1.nextInt();
while (sc1a!=1&&sc1a!=0) {//当输入值不是1或0时要求重新输入
System.out.println("您输入的信息有误,请重新输入:");
Scanner sc2=new Scanner(System.in);
sc1a=sc2.nextInt();
}
if (sc1a==0) {
System.out.println("******感谢使用答答租车系统******");
return;
}
//输出价目表
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
Vehicle[] vehicles= {new Car(1, "奥迪A4", 500, 4),
new Car(2, "马自达6", 400, 4),
new Pickup(3, "皮卡", 450, 4, 2),
new Car(4, "金龙", 800, 20),
new Cargo(5, "松花江", 400, 4),
new Cargo(6, "依维柯", 1000, 20)};
for (int i = 0; i < vehicles.length; i++) {//遍历输出价目表的信息
System.out.println(vehicles[i]);
}
//输入租车数量
System.out.println("请输入您要租车的数量:");
Scanner sc3=new Scanner(System.in);
int sc3a=sc3.nextInt();
while (sc3a<=0) {//当输入值小于1时要求重新输入
System.out.println("您输入的数量有误,请重新输入:");
Scanner sc4=new Scanner(System.in);
sc3a=sc4.nextInt();
}
//输入租车序号
for (int i = 0; i < sc3a; i++) {//遍历输入租车的序号
System.out.println("请输入第"+(i+1)+"辆车的序号:");
Scanner sc5=new Scanner(System.in);
int sc5a=sc5.nextInt();
while (sc5a<=0||sc5a>=7) {//确保输入值为1~6
System.out.println("您输入的序号有误,请重新输入:");
Scanner sc6=new Scanner(System.in);
sc5a=sc6.nextInt();
}
sumPeople+=vehicles[sc5a-1].people;//将所选汽车的载客量相加得出总载客量
sumCargo+=vehicles[sc5a-1].cargo;//将所选汽车的载货量相加得出总载货量
sumPrice+=vehicles[sc5a-1].price;//将所选汽车的价格相加得出总价格
if (vehicles[sc5a-1].getClass()==Car.class){//判断所选汽车可是否可载人
carName+=vehicles[sc5a-1].name+" ";
} else if (vehicles[sc5a-1].getClass()==Cargo.class) {//判断所选汽车可是否可载货
cargoName+=vehicles[sc5a-1].name+" ";
}else if (vehicles[sc5a-1].getClass()==Pickup.class) {
carName+=vehicles[sc5a-1].name+" ";
cargoName+=vehicles[sc5a-1].name+" ";
}
}
//输入租车天数
System.out.println("请输入您要租车的天数:");
Scanner sc7=new Scanner(System.in);
int sc7a=sc7.nextInt();
while (sc7a<=0) {//当输入值小于1时要求重新输入
System.out.println("您输入的天数有误,请重新输入:");
Scanner sc8=new Scanner(System.in);
sc7a=sc8.nextInt();
}
sumPrice=sumPrice*sc7a;
//输出信息
System.out.println("您的账单:");
if (sumPeople>0) {//无可载人汽车则不输出
System.out.println("***可载人的车有:\n"+carName+" 共载:"+sumPeople+"人");
}
if (sumCargo>0) {//无可载货汽车则不输出
System.out.println("***可载货的车有:\n"+cargoName+" 共载:"+sumCargo+"吨");
}
System.out.println("***租车总价格:"+sumPrice+"元");
}
//主方法
public static void main(String[] args) {
Test aTest=new Test();
aTest.start();
}
}
Vehicle.java
package com.DaDa;
//创建车辆父类,用于被3种车型的子类基础,子类可使用父类的成员变量
public abstract class Vehicle {
int id;
String name;
int price;
int people;
int cargo;
}
Car.java
package com.DaDa;
//只载人汽车
public class Car extends Vehicle {
public Car() {
}
public Car(int id,String name,int price,int people) {
this.id=id;
this.name=name;
this.price=price;
this.people=people;
}
@Override
public String toString() {
return id+".\t"+name+"\t"+price+"元/天\t"+"载人:"+people+"人";
}
}
Cargo.java
package com.DaDa;
//只载货的货车
public class Cargo extends Vehicle {
public Cargo(int id,String name,int price,int cargo) {
this.id=id;
this.name=name;
this.price=price;
this.cargo=cargo;
}
@Override
public String toString() {
return id+".\t"+name+"\t"+price+"元/天\t"+"载货:"+cargo+"吨";
}
}
Pickup.java
package com.DaDa;
//即可载人又可载货的皮卡
public class Pickup extends Vehicle {
public Pickup(int id,String name,int price,int people,int cargo) {
this.id=id;
this.name=name;
this.price=price;
this.people=people;
this.cargo=cargo;
}
@Override
public String toString() {
return id+".\t"+name+"\t"+price+"元/天\t"+"载人:"+people+"人,载货:"+cargo+"吨";
}
}