提交作业,大家可以参考下,我觉得还能优化,望各位多提意见
package DDproject;
public abstract class car { //汽车父类
public int id;
public String name;
public int price;
public int people;
public int carry;
public abstract void run();
}
public class HuoCar extends car { //货车子类
public HuoCar(int id,String name,int price,int carry) {
this.id=id;
this.name=name;
this.price=price;
this.carry=carry;
}
public void run() {
System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载货:"+carry+"吨");
}
}
public class buscar extends car { //载人子类
public buscar(int id,String name,int price,int people) {
this.id=id;
this.name=name;
this.price=price;
this.people=people;
}
public void run() {
System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载人:"+people+"人");
}
}
public class PKcar extends car { //皮卡子类
public PKcar(int id,String name,int price,int people,int carry) {
this.id=id;
this.name=name;
this.price=price;
this.people=people;
this.carry=carry;
}
public void run() {
System.out.println(id+"\t"+name+"\t"+price+"元/天"+"\t"+"载人:"+people+"人"+"\t"+"载货:"+carry+"吨");
}
}
package DDproject;
import java.util.Scanner;
public class Demotest { //测试类
public void ZuChe() { //是否进人租车系统方法
Scanner input = new Scanner(System.in);
try {
int select = input.nextInt();
if (select == 1) {
means();
} else if (select == 0) {
System.out.println("感谢您使用答答租车系统~再见!");
} else {
System.out.println("您输入有误,请重新输入:");
System.out.println("您是否需要租车:1、是" + "\t" + "0、否");
ZuChe();
}
} catch (Exception e) {
System.out.println("您输入有误,请重新输入:");
System.out.println("您是否需要租车:1、是" + "\t" + "0、否");
ZuChe();
}
}
public void means() { //租车系统选车主方法
car che[] = { new buscar(1, "奥迪4A", 500, 4), new buscar(2, "马自达6", 400, 4), new PKcar(3, "皮卡雪6", 450, 4, 2),
new buscar(4, "金龙", 800, 20), new HuoCar(5, "松花江", 400, 4), new HuoCar(6, "依维柯", 1000, 20) };
System.out.println("您可租车的类型及其价目表");
System.out.println("序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量");
for (int i = 0; i < che.length; i++) {
che[i].run();
}
double money = 0, ton = 0;
int sum = 0;
System.out.println("请输入您要汽车的数量:");
Scanner input = new Scanner(System.in);
int amount = input.nextInt();
int ID[] = new int[amount];
for (int i = 0; i < amount; i++) {
System.out.println("请输入第" + (i + 1) + "辆车的序号");
int id = input.nextInt();
money = money + che[id - 1].price;
sum = sum + che[id - 1].people;
ton = ton + che[id - 1].carry;
ID[i] = id;
}
System.out.println("请输入租车天数:");
int day = input.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
for (int i = 0; i < ID.length; i++) {
if (che[ID[i] - 1].people != 0) {
System.out.print(che[ID[i] - 1].name + "\t");
}
}
System.out.println("共载人:" + sum + "人");
System.out.println("***可载货的车有:");
for (int i = 0; i < ID.length; i++) {
if (che[ID[i] - 1].carry != 0) {
System.out.print(che[ID[i] - 1].name + "\t");
}
}
System.out.println("共载货:" + ton + "吨");
System.out.println("***租车总价格:" + money * day + "元");
}
public static void main(String[] args) {
System.out.println("欢迎您是使用答答租车系统:");
System.out.println("您是否需要租车:1、是" + "\t" + "0、否");
Demotest t = new Demotest();
t.ZuChe();
}
}