雖然還不是很嚴謹,還是完成了
第二次吐槽 沒寫完,最后部分計算總載人和載貨量忘記了 哭唧唧
先吐槽下自己,還是看著老師給的參考控制臺頁面寫出來的
結構 創建了一個car父類 只定義了兩個屬性?
? ? ? ?定義了兩個接口 一個LoadPeople? ?LoadThing(最開始沒搞定皮卡 寫完了其他的沒辦法才改成了用兩個接口)
? ? ? ?創建了 Bus? Truck? PickUP 三個子類 bus實現第一個接口? 貨車實現第二個? 皮卡both
不嚴謹處:輸入選擇車序號時,如果為不合理參數,判斷不全(這里循環有點問題)
用到了swith? 判斷輸入的序號數,將對應序號的對象名(車名)依次儲存在一個數組中(數組長度為輸入的選擇車輛輛數)
用了equals判斷,分別輸出裝人的車名和裝貨的車名
主函數:
package com.dadaCarRentalSystem;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO 自動生成的方法存根
Bus c1=new Bus();
c1.name="奧迪A4";
c1.price=500;
Bus c2=new Bus();
c2.name="馬自達6";
c2.price=400;
Bus c3=new Bus();
c3.name="金龍";
c3.price=800;
Truck c4 = new Truck();
c4.name="松花江";
c4.price=400;
Truck c5=new Truck();
c5.name="依維柯";
c5.price=1000;
PickUp c6=new PickUp();
c6.name="皮卡雪6";
c6.price=450;
? ? ? System.out.println("歡迎使用答答租車系統;");
? ? ? System.out.println("您是否要租車:1是 0否");
? ? ? Scanner input = new Scanner(System.in);
? ? ? int a= input.nextInt();
? ? ? for(;(a!=1)&&(a!=0);a=input.nextInt()) {
? ? ? System.out.println("輸入有誤,請重新輸入");
? ? ? }
? ? ? if(a==0) {
? ? ? System.out.println("再見!歡迎您下次使用");
? ? ? }
? ? ? if(a==1){
? ? ? //顯示租車清單
? ? ? System.out.println("您可租車的類型及其價目表:");
? ? ? System.out.println("序號? ? 汽車名稱? ? ? ? ? ? ?租金? ? ? ? ? ? ? ? ? ? ? ? ?容量");
? ? ? System.out.print("1.? "+c1.name+" "+ c1.price+"元/天"+"? ? ? ? ? ");
? ? ? c1.load1(4);
? ? ? System.out.print("2.? "+c2.name+" "+ c2.price+"元/天"+"? ? ? ? ? ");
? ? ? c2.load1(4);
? ? ? System.out.print("3.? "+c3.name+"? ? ? ? ?"+ c3.price+"元/天"+"? ? ? ? ? ");
? ? ? c3.load1(20);
? ? ? System.out.print("4.? "+c4.name+"? ? ? ? ?"+ c4.price+"元/天"+"? ? ? ? ? ");
? ? ? c4.load2(4);
? ? ? System.out.print("5.? "+c5.name+"? ? ? ? ?"+ c5.price+"元/天"+"? ? ? ? ? ");
? ? ? c5.load2(20);
? ? ? System.out.print("6.? "+c6.name+" "+ c6.price+"元/天"+"? ? ? ? ? ");
? ? ? c6.load1(4);
? ? ? c6.load2(2);
? ? //用戶選擇租車
? ? ? System.out.println("請輸入您要租車的數量:");
? ? ? int num = input.nextInt();//租車數量
? ? ? String select[]=new String[num];
? ? ? float sum=0;
? ? ? for(int i=1;i<=num;i++) {
? ? ? System.out.println("請輸入第"+i+"輛車的序號:");
? ? ? int sequence = input.nextInt();? ? ??
? ? ? switch(sequence) {
? ? ? case 1:
? ? ? select[i-1]=c1.name;? ?
? ? ? sum=sum+c1.price;
? ? ? break;
? ? ? case 2:
? ? ? select[i-1]=c2.name;
? ? ? sum=sum+c2.price;
? ? ? break;
? ? ? case 3:
? ? ? select[i-1]=c3.name;
? ? ? sum=sum+c3.price;
? ? ? break;
? ? ? case 4:
? ? ? select[i-1]=c4.name;
? ? ? sum=sum+c4.price;
? ? ? break;
? ? ? case 5:
? ? ? select[i-1]=c5.name;
? ? ? sum=sum+c5.price;
? ? ? break;
? ? ? case 6:
? ? ? select[i-1]=c6.name;
? ? ? sum=sum+c6.price;
? ? ? break;
? ? default:
? ? System.out.println("輸入有誤,請重新輸入:");
? ? sequence=input.nextInt();
? ? ? }
? ? ? }
? ? ? System.out.println("請輸入租車天數:");
? ? ? int day = input.nextInt();
? ? ? System.out.println("您的賬單:");
? ? ? System.out.println("***可載人的車有:");
? ? ? for(int j=0;j<select.length;j++) {
? ? ? if(select[j].equals("奧迪A4")||select[j].equals("馬自達6")||select[j].equals("金龍")||select[j].equals("皮卡雪6")) {
? ? System.out.println(select[j]+"? ? ");?
? ? ? }
? ? ? }
? ? ? System.out.println("***可載貨的車有:");
? ? ? for(int j=0;j<select.length;j++) {
? ? ? if(select[j].equals("松花江")||select[j].equals("依維柯")||select[j].equals("皮卡雪6")) {
? ? System.out.println(select[j]+"? ? ");?
? ? ? }
? ? ? }
? ? ? sum=sum*day;
? ? ? System.out.println("***租車總價格:"+sum+"元");
? ? ? }
}
}
2018-09-29
好好,贊一個??!