租车系统
测试类
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; } }
运行截图