提交作業(yè),大家可以參考下,我覺(jué)得還能優(yōu)化,望各位多提意見(jiàn)
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 {? ? ? //測(cè)試類
public void ZuChe() {? ? ? //是否進(jìn)人租車系統(tǒng)方法
Scanner input = new Scanner(System.in);
try {
int select = input.nextInt();
if (select == 1) {
means();
} else if (select == 0) {
System.out.println("感謝您使用答答租車系統(tǒng)~再見(jiàn)!");
} else {
System.out.println("您輸入有誤,請(qǐng)重新輸入:");
System.out.println("您是否需要租車:1、是" + "\t" + "0、否");
ZuChe();
}
} catch (Exception e) {
System.out.println("您輸入有誤,請(qǐng)重新輸入:");
System.out.println("您是否需要租車:1、是" + "\t" + "0、否");
ZuChe();
}
}
public void means() {? ? ?//租車系統(tǒng)選車主方法
car che[] = { new buscar(1, "奧迪4A", 500, 4), new buscar(2, "馬自達(dá)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("您可租車的類型及其價(jià)目表");
System.out.println("序號(hào)" + "\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("請(qǐng)輸入您要汽車的數(shù)量:");
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("請(qǐng)輸入第" + (i + 1) + "輛車的序號(hào)");
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("請(qǐng)輸入租車天數(shù):");
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("***租車總價(jià)格:" + money * day + "元");
}
public static void main(String[] args) {
System.out.println("歡迎您是使用答答租車系統(tǒng):");
System.out.println("您是否需要租車:1、是" + "\t" + "0、否");
Demotest t = new Demotest();
t.ZuChe();
}
}
2019-10-15
我還是有個(gè)問(wèn)題啊 ,就是載人的車和載重的車就沒(méi)有區(qū)分呀?
載人的車打印的也是
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測(cè)無(wú)限循環(huán)語(yǔ)句,并且用while的話能用continue而不用嵌套,try-catch用在這感覺(jué)不合適;
總體感覺(jué)挺簡(jiǎn)潔到位的,贊?。
2019-07-20
請(qǐng)問(wèn)那個(gè)means();是什么意思?
2019-07-10
你在父類里調(diào)用輸出函數(shù)就可以。不需要每個(gè)子類都寫(xiě)