父类Car.java
package 答答租车;
public abstract class Car {
public String xuhao;
public String name;
public int money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Car(){};
public Car(String xuhao,String name,int money){
this.xuhao=xuhao;
this.name=name;
this.money=money;
}
public String getXuhao() {
return xuhao;
}
public void setXuhao(String xuhao) {
this.xuhao = xuhao;
}
}
子类:huoche.java
package 答答租车;
public class huoche extends Car {
public int dun;
public int getDun() {
return dun;
}
public void setDun(int dun) {
this.dun = dun;
}
public huoche(String xuhao,String name,int money,int dun){
super(xuhao, name, money);
this.dun=dun;
}
@Override
public String toString() {
return "["+ xuhao+'\t' + name+'\t' + money+"元/天"+'\t' + "载货:"+ dun+"吨"+'\t' + "]";
}
}
子类:keche.java
package 答答租车;
public class keche extends Car{
public keche(){};
public int ren;
public int getRen() {
return ren;
}
public void setRen(int ren) {
this.ren = ren;
}
public keche(String xuhao,String name,int money,int ren){
super(xuhao,name,money);
this.ren=ren;
}
@Override
public String toString() {
return "["+ xuhao+'\t' + name+'\t' +money+"元/天"+'\t' + "载人:"+ ren+"人" + "]";
}
}
子类:pika.java
package 答答租车;
public class pika extends Car{
private int dun;
private int ren;
public pika(String xuhao,String name,int money,int dun,int ren){
super(xuhao,name, money);
this.dun=dun;
this.ren=ren;
}
@Override
public String toString() {
return "["+ xuhao+'\t' + name+'\t' +money+"元/天"+'\t' + "载人:"+ ren+"人"+"载货:"+ dun+"吨"+'\t'+"]";
}
}
测试:test.java
package 答答租车;
import java.util.Scanner;
public class test {
static Car[] car = new Car[] { new keche("1", "奥迪A4", 500, 4),
new keche("2", "马自达6", 400, 4), new keche("3", "金龙", 800, 20),
new pika("4", "皮卡雪6", 450, 2, 4), new huoche("5", "松花江", 400, 4),
new huoche("6", "依维柯", 1000, 20) };
public static void main(String[] args) {
double amt = 0;
Scanner scan = new Scanner(System.in);
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
int s = scan.nextInt();
if (s == 1) {
System.out.println(" " + "序号" + '\t' + "汽车名称" + '\t' + "租金" + '\t'
+ "容量");
for (Car c : car) {
System.out.println(c);
}
System.out.println("请输入您要租汽车的数量:");
int num = scan.nextInt();
int id = 0;
// String totalChe[]=new String [num];
int List[] = new int[6];
for (int i = 0; i <= num - 1; i++) {
System.out.println("请输入您要租第" + (i + 1) + "辆汽车的序号:");
id = scan.nextInt();
id = id - 1;
System.out.println("您要租的车是:" + car[id]);
amt += car[id].getMoney();
List[i] = id;
}
System.out.println("请输入您要租的天数:");
Scanner s3 = new Scanner(System.in);
int tian = s3.nextInt();
System.out.println("您的账单:");
System.out.println("共需支付:" + amt * tian);
System.out.println("可载人的车有:");
for (int i = 0; i <= num - 1; i++) {
id = List[i];
if (car[id] instanceof keche) {
System.out.println(car[id]);
}
if (car[id] instanceof pika) {
System.out.println(car[id]);
}
}
System.out.println("可载货的车有:");
for (int i = 0; i <= num - 1; i++) {
id = List[i];
if (car[id] instanceof pika) {
System.out.println(car[id]);
}
if (car[id] instanceof huoche) {
System.out.println(car[id]);
}
}
System.out.println("\n" + "************完毕*************");
} else {
System.out.println("感谢您使用答答租车系统");
}
}
}