1.定义汽车抽象类
package proje2;
public abstract class Acar {
private int num; //序号
private String name; //车名
private int price; //价格
private int busload; //载客量
private int cargoload; //载货量
public int getNum() {
return num;
}
public void setNum(int num) {
this.num=num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price=price;
}
public int getBusload() {
return busload;
}
public void setBusload(int busload) {
this.busload=busload;
}
public int getCargoload() {
return cargoload;
}
public void setCargoload(int cargoload) {
this.cargoload=cargoload;
}
public String toString() {
return num+" "+name+" "+price+"元/天 "+"载客量:"+busload+"人/载货量:"+cargoload;
}
}
2.定义轿车,客车,货车,皮卡车类,分别继承汽车抽象类
package proje2;
public class Car extends Acar {
public Car(int num,String name,int price,int busload) {
this.setNum(num);
this.setName(name);
this.setPrice(price);
this.setBusload(busload);
}
}
package proje2;
public class Bus extends Acar {
public Bus(int num,String name,int price,int busload) {
this.setNum(num);
this.setName(name);
this.setPrice(price);
this.setBusload(busload);
}
}
package proje2;
public class Truck extends Acar {
public Truck(int num,String name,int price,int cargoload) {
this.setNum(num);
this.setName(name);
this.setPrice(price);
this.setCargoload(cargoload);
}
}
package proje2;
public class Pickup extends Acar {
public Pickup(int num,String name,int price,int busload,int cargoload) {
this.setNum(num);
this.setName(name);
this.setPrice(price);
this.setBusload(busload);
this.setCargoload(cargoload);
}
}
3.测试类
package proje2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Acar c[] = new Acar[6];
c[0]=new Car(1,"奥迪A4",500,4);
c[1]=new Car(2,"马自达6",400,4);
c[2]=new Pickup(3,"皮卡雪6",450,4,2);
c[3]=new Bus(4,"金龙",800,20);
c[4]=new Truck(5,"松花江",400,4);
c[5]=new Truck(6,"依维柯",1000,20);
Scanner input = new Scanner(System.in); //创建Scanner对象
System.out.println("欢迎使用答答租车系统!");
System.out.println("你是否要租车:1是? 0否?");
int i = input.nextInt(); //获取用户输入信息并保存到变量中
while(i != 0 && i != 1) {
System.out.println("您输入的信息有误,请重试");
i = input.nextInt();
}
if(i == 0) {
System.out.println("欢迎再次光临答答租车系统!");
System.exit(0);
}else if(i == 1){
System.out.println("您可选择的租车类型及价目表:");
System.out.println("序号 车名 租金 载客/载货量");
for(int j=0;j<c.length;j++) {
System.out.println(c[j]);
}
System.out.println("请输入您想要租的汽车数量:");
int m = input.nextInt();
while( m <= 0 ) {
System.out.println("您输入的信息有误,请重试");
m = input.nextInt();
}
String[] d = new String[m];
int[] e = new int[m];
int[] f = new int[m];
int[] g = new int[m];
for(int k=0;k<m;k++) {
System.out.println("请输入第"+ (k+1) +"辆车的序号:");
int l = input.nextInt();
while(l < 1 || l > 6) {
System.out.println("不存在该序号的车辆,请输入数字为1-6之间的数字");
l = input.nextInt();
}
d[k] = c[l-1].getName();
e[k] = c[l-1].getPrice();
f[k] = c[l-1].getBusload();
g[k] = c[l-1].getCargoload();
}
System.out.println("请输入您想要租的天数:");
int n = input.nextInt();
while( n <= 0) {
System.out.println("您输入的信息有误,请重试");
n = input.nextInt();
}
System.out.println("您的账单为:");
System.out.println("****************");
int s = 0;
int sumBusload = 0;
int sumCargoload =0;
int sumPrice=0;
System.out.println("您租的车为:");
for(int j=0;j<d.length;j++) {
System.out.println(d[j]);
}
System.out.println("****************");
for(int j=0;j<f.length;j++) {
s=f[j];
sumBusload = sumBusload + s;
}
System.out.println("载客量共计为:"+sumBusload+"人");
System.out.println("****************");
for(int j=0;j<g.length;j++) {
s=g[j];
sumCargoload = sumCargoload + s;
}
System.out.println("载货量共计为:"+sumCargoload+"吨");
System.out.println("****************");
for(int j=0;j<e.length;j++) {
s=e[j];
sumPrice = sumPrice + s;
}
System.out.println("您共计应花费为:"+(sumPrice*n)+"元");
System.out.println("****************");
System.out.println("感谢您使用答答租车系统!");
System.out.println("欢迎您再次光临!");
}
}
}