來交作業了!備注了知識點
package?com.stduy;
import??java.util.Scanner;
//從功能上分,總共分為三種,載人汽車,載貨汽車,既載人又載貨。
//對應Bus,Truck,PickUp
public?class?Initail?{
????private?static?int?choice;
????static?Scanner??input?=?new?Scanner(System.in);
????private?static?Car[]?cars=?new?Car[]{
????????????new?Bus("巴士車",500,10),
????????????new?Truck("卡車",300,5),
????????????new?PickUp("皮卡車",?600,?6,?7)
????};
????public?static?void?main(String[]?args)?{
????????System.out.println("歡迎使用租車系統");
????????//Java不需要先聲明,和C不同。
????????//?原因是:類加載編譯時就把程序的入口初始化了,執行時是JAVA虛擬機所識別的特定字節碼,
????????//?C卻是處理器可直接執行的二進制指令,編譯的原理不一樣,語言獨特的特性與運行環境不一樣。
????????IsNeed();//判斷是否要租車
????????if(choice==1){
????????????DisplayList();?//顯示可租車清單
????????????int?num=RentNum();//獲取租車的種類數
????????????RentList[]?rentLists=new?RentList[num];//填寫出租信息表
????????????rentLists=RentForm(num);
????????????Pay(rentLists,cars);//生成賬單
????????}
????}
????public?static?void?IsNeed()?{
????????System.out.println("您是否要租車:1是?0否");
????????choice?=?input.nextInt();
????????if?(choice?==?0)?{
????????????System.out.println("感謝使用,再見");
????????}?else?if?(choice?!=?1)?{
????????????System.out.println("輸入錯誤,請重新輸入");
????????}
????}
????//關鍵字instanceof,測試一個對象是否為一個類的實例
????public?static?void?DisplayList(){
????????System.out.println("您可租車的型號和價格表");
????????for(int?i?=?0;?i<cars.length;?i++){
????????????????if(cars[i]?instanceof?Bus){
????????????????????Bus?car=(Bus)cars[i];//強制類型轉換
????????????????????System.out.println((i?+?1)?+?"?"?+?car.name+"?"+car.rent+"元/天"+"?"+"核載"+car.person+"人");
????????????????}
????????????????else?if(cars[i]?instanceof?Truck){
????????????????????Truck?car=(Truck)cars[i];
????????????????????System.out.println((i?+?1)?+?"?"?+?car.name+"?"+car.rent+"元/天"+"?"+"核載"+car.goods+"噸");
????????????????}
????????????????else?if(cars[i]?instanceof?PickUp){
????????????????????PickUp?car=(PickUp)cars[i];
????????????????????System.out.println((i?+?1)?+?"?"?+?car.name+"?"+car.rent+"元/天"+"?"+"核載"+car.person+"人"+"?"+"核載"+car.goods+"噸");
????????????????}
????????????}
????}
????//以下為輸入部分
????//獲取租車數量
????private?static?int?RentNum(){
????????System.out.println("請輸入您要總共租車的種類數:");
????????int?RentNum=input.nextInt();
????????return?RentNum;
????}
????//租車信息表
????private??static??RentList[]?RentForm??(int?num){
????????//租車信息對象數組
????????RentList[]??rentLists=new?RentList[num];
????????for(int?i=0;i<num;i++){
????????????//型號
????????????System.out.println("請輸入租的第"+(i+1)+"類車的信息");
????????????System.out.print("型號:");
????????????int?Model=input.nextInt();
????????????//該型號的數量
????????????System.out.print("數量:");
????????????int?Num=input.nextInt();
????????????//該型號租的天數
????????????System.out.print("天數:");
????????????int?Day=input.nextInt();
????????????//將信息存入對象數組
????????????rentLists[i]=new?RentList();//這步不能掉
????????????rentLists[i].setModel(Model);
????????????rentLists[i].setNum(Num);
????????????rentLists[i].setDay(Day);
????????}
????????return?rentLists;
????}
????//生成賬單
????private?static?void?Pay(RentList[]?rentLists,Car[]?cars){
????????//打印清單
????????System.out.println("************結算清單*************");
????????System.out.println("型號????????數量????????租用天數");
????????double?SumMoney=0;
????????for(int?i=0;i<rentLists.length;i++){
????????????//用get方法獲得數據,更好的封裝RentList類
????????????int?Model=rentLists[i].getModel();
????????????int?Num=rentLists[i].getNum();
????????????int?Day=rentLists[i].getDay();
????????????double?Rent=cars[i].getRent();
????????????SumMoney+=Num*Day*Rent;
????????????System.out.println(Model+"????????????"+Num+"????????????"+Day);
????????}
????????System.out.println("您總共要支付"+SumMoney+"元");
????}
????//這里也可以通過重寫toString方法輸出
????//toString方法為Object類中的方法,返回對象的哈希碼,即地址
????//不重寫toString方法,則直接輸入對象是輸出該對象的地址
}//父類,車
package?com.stduy;
//車
public?class?Car?{
????protected???String?name;//車型,protect方便繼承
????protected???double?rent;//租金
????public?Car(String?name,double?rent){//用構造方法初始化
????????this.name=name;
????????this.rent=rent;
????}
????public?String?getName()?{
????????return?name;
????}
????public?double?getRent()?{
????????return?rent;
????}
????public?void?setName(String?name)?{
????????this.name?=?name;
????}
????public?void?setRent(double?rent)?{
????????this.rent?=?rent;
????}
}//載人車
package?com.stduy;
//Bus,載人汽車
public?class?Bus?extends?Car?{
????//子類的構造過程當中必須調用其父類的構造方法
????//無參時隱式執行super(),子類如果沒有顯式調用,父類又沒有無參的構造方法則編譯出錯
????//java里一個類可以有多個構造方法
????//super關鍵字必須放在第一行
????protected?int?person;//載人數
????//構造方法
????public?Bus(String?name,?double?rent,int?person)?{
????????super(name,?rent);
????????this.person=person;
????}
????public?int?getPerson()?{
????????return?person;
????}
????public?void?setPerson(int?person)?{
????????this.person?=?person;
????}
}//載貨車
package?com.stduy;
//貨車
public?class?Truck?extends?Car{
????protected?double?goods;//貨物
????//構造方法
????public?Truck(String?name,?double?rent,?double?goods)?{
????????super(name,?rent);
????????this.goods=goods;
????}
????public?double?getGoods()?{
????????return?goods;
????}
????public?void?setGoods(double?goods)?{
????????this.goods?=?goods;
????}
}//既載貨又載人
package?com.stduy;
//皮卡,既能載貨又能載人
public?class?PickUp?extends?Car?{
????protected?int?person;//載人數
????protected?double?goods;//貨物
????//構造方法
????public?PickUp(String?name,?double?rent,int?person,double?goods)?{
????????super(name,?rent);
????????this.person=person;
????????this.goods=goods;
????}
????public?int?getPerson()?{
????????return?person;
????}
????public?double?getGoods()?{
????????return?goods;
????}
????public?void?setPerson(int?person)?{
????????this.person?=?person;
????}
????public?void?setGoods(double?goods)?{
????????this.goods?=?goods;
????}
}//租車信息表
package?com.stduy;
//租車信息表
public?class?RentList?{
????private??int?Model;//型號
????private??int?Num;//要租該型號的數量
????private??int?Day;//要租該型號的天數
????public?int?getModel()?{
????????return?Model;
????}
????public?int?getNum()?{
????????return?Num;
????}
????public?int?getDay()?{
????????return?Day;
????}
????public?void?setModel(int?model)?{
????????Model?=?model;
????}
????public?void?setNum(int?num)?{
????????Num?=?num;
????}
????public?void?setDay(int?day)?{
????????Day?=?day;
????}
}
2020-04-05
膜拜大佬,大佬學習多久了
2020-03-19
為啥我自己就寫不出來呢??難過
2020-03-19
大神厲害!我自己按照你的這個寫了一遍,發現了一個bug;如果只租兩種或一種車,結果就會出現錯誤。建議把double Rent = cars[i].getRent();改成double Rent = cars[Model-1].getRent();
2020-03-18
你好,想問個問題,你的Pay方法中,rentList 和 cars的型號沒有判斷是否匹配,如果用戶不按照順序選擇車的類型,車的租金應該會匹配有誤吧
2020-03-17
寫掉了要算的總核載量和總核載人數,不過大同小異,就那里加幾行
2020-03-17
附一下運行結果