提交作業,大家可以參考下,我覺得還能優化,望各位多提意見
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();
}
}
2019-10-15
我還是有個問題啊 ,就是載人的車和載重的車就沒有區分呀?
載人的車打印的也是
System.out.print(che[ID[i] - 1].name + "\t");
載重的車打印也是
System.out.print(che[ID[i] - 1].name + "\t");
2019-07-20
try-catch那段,else{} 可以省掉,或者將try-catch換成while測無限循環語句,并且用while的話能用continue而不用嵌套,try-catch用在這感覺不合適;
總體感覺挺簡潔到位的,贊?。
2019-07-20
請問那個means();是什么意思?
2019-07-10
你在父類里調用輸出函數就可以。不需要每個子類都寫