租車系統 測試類 抽象類Car 子類ren (只能載人) 子類wu (只能載貨) 子類quan (都能載) 運行截圖 ...
租車系統
測試類
1 import java.util.Scanner; 2 3 public class Init { 4 static int allrennum=0; 5 static int allhuonum=0; 6 static int allprice=0; 7 8 public static void main(String[] args) { 9 System.out.println("歡迎使用答答租車系統:"); 10 System.out.println("您是否要租車:1是 0否"); 11 Scanner in=new Scanner(System.in); 12 int check=in.nextInt(); 13 if(check==1){ 14 ren ren=new ren(); 15 wu wu=new wu(); 16 quan qu=new quan(); 17 ren.print(1,"奧迪A4",500,4); 18 ren.print(2,"馬自達6",400,4); 19 qu.print(3,"皮卡雪6",450,4,2); 20 ren.print(4,"金龍 ",800,20); 21 wu.print(5, "松花江 ", 400, 4); 22 wu.print(6, "依維柯", 1000, 20); 23 System.out.println("請輸入您要租的汽車的數量:"); 24 Scanner input=new Scanner(System.in); 25 int nums=input.nextInt(); 26 for(int i=1;i<=nums;i++){ 27 System.out.println("請輸入第"+i+"輛車的序號"); 28 int xuhao=in.nextInt(); 29 switch(xuhao){ 30 case 1: 31 ren.fuzhi("奧迪A4",500,4); 32 break; 33 case 2: 34 ren.fuzhi("馬自達6",400,4); 35 break; 36 case 3: 37 qu.fuzhi("皮卡雪6",450,4,2); 38 break; 39 case 4: 40 ren.fuzhi("金龍 ",800,20); 41 break; 42 case 5: 43 wu.fuzhi( "松花江 ", 400, 4); 44 break; 45 case 6: 46 wu.fuzhi( "依維柯", 1000, 20); 47 break; 48 } 49 } 50 System.out.println("請輸入天數:"); 51 Scanner input2=new Scanner(System.in); 52 int day=input.nextInt(); 53 System.out.println("共可載人:"+allrennum+"人"); 54 System.out.println("共可載貨:"+allhuonum+"噸"); 55 System.out.println("總金額為:"+day*allprice+"元"); 56 } 57 else{ 58 System.out.println("離開"); 59 60 } 61 } 62 } 63
抽象類Car
public abstract class Car { int num; String name; int price; }
子類ren (只能載人)
public class ren extends Car { int rennum; public void print(int num,String name,int price,int rennum) { System.out.println(num+" "+name+" "+price+"元/天 "+"載人:"+rennum+"人"); } public void fuzhi(String name,int price,int rennum) { Init.allprice+=price; Init.allrennum+=rennum; } }
子類wu (只能載貨)
public class wu extends Car { int huonum; public void print(int num,String name,int price,int huonum) { System.out.println(num+" "+name+" "+price+"元/天 "+"載貨:"+huonum+"噸"); } public void fuzhi(String name,int price,int huonum) { Init.allprice+=price; Init.allhuonum+=huonum; } }
子類quan (都能載)
public class quan extends Car { int huonum; int rennum; public void print(int num,String name,int price,int rennum,int huonum) { System.out.println(num+" "+name+" "+price+"元/天 "+"載貨:"+rennum+"人"+huonum+"噸"); } public void fuzhi(String name,int price,int rennum,int huonum){ Init.allprice+=price; Init.allhuonum+=huonum; Init.allrennum+=rennum; } }
運行截圖