通过半个月的java语言学习,完成一个简单的滴滴租车小demo,都是自己手动输入,跟参考代码的答案基本不一样,但实现思路基本一致;
1 package project9; 2 import java.util.Scanner; 3 // 测试类 4 public class testClass9 { 5 public static void main(String[] args){ 6 Car[] car ={new LightCar(),new HeavyCar(),new LightPassengerCar(),new HeavyPassengerCar(),new PikaCar()}; 7 System.out.println("欢迎使用答答租车系统:"); 8 System.out.println("您是否要租车:1是 0否"); 9 Scanner input = new Scanner(System.in); 10 int isNeedCar = input.nextInt(); //是否要租车 11 if(isNeedCar ==0){ 12 System.out.println("感谢您的使用,欢迎再次光临!"); 13 }else if(isNeedCar !=0 && isNeedCar!=1){ 14 System.out.println("您的输入有误,请输入0或者1!"); 15 }else if(isNeedCar ==1){ 16 System.out.println("请输入您要租汽车的数量"); 17 int needCarNum = input.nextInt(); 18 if(needCarNum>car.length){ 19 System.out.println("您要租汽车的数量大于本店汽车的数量,请重新输入要租的汽车的数量!"); 20 }else{ 21 int[] saveSelectedCarNo = new int[needCarNum];//存储被客服选择组的汽车的下标值集合 22 for(int i=1;i<=needCarNum;i++){ 23 System.out.println("请输入第"+i+"辆车的序号:"); 24 int whichCarNo = input.nextInt(); //某个车的下标值 25 saveSelectedCarNo[i-1] =whichCarNo; 26 } 27 System.out.println("您输入租车天数:"); 28 int needCarDay = input.nextInt(); //租车天数 29 System.out.println("您的租车账单:"); 30 System.out.println("***可载人的车有:"); 31 int canManned = 0; //可载人的数量总和,初始值设为0 32 double canCarryCargo = 0; //可载货物的数量总和,初始值设为0 33 double money =0 ; //租车总价格,初始值设为0 34 for(int j=0;j<saveSelectedCarNo.length;j++){ 35 if(car[saveSelectedCarNo[j]].getCanManned() == true){ 36 System.out.print(car[saveSelectedCarNo[j]].getCarName()+" "); 37 canManned = canManned + car[saveSelectedCarNo[j]].getNumberOfPeople(); 38 money = money + car[saveSelectedCarNo[j]].getMoney(); 39 } 40 } 41 System.out.println("共载人:"+canManned+"人"); 42 System.out.println("***可载货的车有:"); 43 for(int k=0;k<saveSelectedCarNo.length;k++){ 44 if(car[saveSelectedCarNo[k]].getcanCarryCargo() == true){ 45 System.out.print(car[saveSelectedCarNo[k]].getCarName()+" "); 46 canCarryCargo = canCarryCargo + car[saveSelectedCarNo[k]].getVolumeOfCargo(); 47 money = money + car[saveSelectedCarNo[k]].getMoney(); 48 } 49 } 50 System.out.println("共载货:"+canCarryCargo+"吨"); 51 System.out.println("租车总价格:"+money*needCarDay+"元"); 52 53 } 54 } 55 } 56 57 }
实现效果如下: