1 PackageGps.start;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 Importjava.util.Collections;6 Importjava.util.List;7 ImportJava.util.Random;8 ImportJava.util.Scanner;9 ImportJava.util.Set;Ten One ImportGps.car.Car; A - /** - * the * @author - * Feng Ke - * Ideas: - * 1. Control vehicle generation information via random, including license plate + * 2. Store vehicles by list: type, color; - * 3. Implementation logic: + * According to the parking space, randomly pull out the vehicle to determine whether the user to seize the parking space is their own vehicle A * at */ - - Public classStartgamecomplex { - //Vehicle Information Management - StaticArraylist<car> List_car =NewArraylist<car>(); - //Parking Management in StaticArraylist<car> List_park =NewArraylist<car>(); - //Number of cars to Static intNum_car = 0; + //parking Spaces - Static intNum_park = 0; the //system provides vehicle type * StaticList<string> Car_type =NewArraylist<string>(); $ //system provides vehicle colorPanax Notoginseng StaticList<string> Car_colour =NewArraylist<string>(); - the Static { +Car_type.add ("Porsche"); ACar_type.add ("QQ")); theCar_type.add ("Mazda"); +Car_type.add ("Mercedes_benz"); -Car_colour.add ("Blue"); $Car_colour.add ("Red"); $Car_colour.add ("Whitee"); -Car_colour.add ("Yellow"); - the } - Wuyi //Main Method: Set Main method the Public Static voidMain (string[] args) { - WuStartgamecomplex Startgamecomplex =NewStartgamecomplex (); - //Set Game difficulty About Startgamecomplex.setstartscreen (); $ //Prepare the game (enter personal vehicle information) - Startgamecomplex.ready (); - //Start the game - startgamecomplex.startgame (); A //result Generation + startgamecomplex.resultgame (); the - } $ the Private voidResultgame () { theSystem.out.println ("Please enter the parking space to be preempted (1-" +list_park.size () + ")"); theScanner scanner=NewScanner (system.in); the intnum=scanner.nextint (); - while(true){ in if(List_park.get (num-1). GetId (). Equals (List_car.get (0). GetId ())) { theSystem.out.println ("Seize the parking space success!!! "); the Break; About}Else{ theSystem.out.println ("The parking space has been preempted"); theSYSTEM.OUT.PRINTLN ("Please re-select the Parking space:"); thenum=scanner.nextint (); + - } the }Bayi the } the - /** - * Rules of the game: 1. The vehicle information is then deposited into the List_car 2. Deployment of parking spaces; the */ the Private voidStartgame () { the //Generate vehicle Information theRandom random =NewRandom (); - for(inti = 1; i < Num_car; i++) { theCar car_temp =NewCar (Integer.tostring (Random.nextint (100000)), Car_type.get (Random.nextint (4)), theCar_colour.get (Random.nextint (4))); the List_car.add (car_temp);94 } the //SelectAll (List_car, "car"); the //Deploying parking spaces the for(inti = 0; i < Num_park; i++) {98Car car_temp =List_car.get (Random.nextint (List_car.size ())); About List_park.add (car_temp); - }101 //Show Parking Information102 //SelectAll (List_park, "Park");103 104 } the 106 /**107 * Generate your own vehicle information108 * 109 * @return the */111 PublicCar Ready () { theSystem.out.println ("Please enter vehicle information:");113Scanner cursor =NewScanner (system.in); theSYSTEM.OUT.PRINTLN ("License plate:"); theString ID =Cursor.next (); theSystem.out.println ("Type:");117String type =Cursor.next ();118System.out.println ("Color:");119String colour =Cursor.next (); -Car Usercar =NewCar (ID, type, colour);121 List_car.add (usercar);122 returnUsercar;123 }124 the /**126 * Set the game difficulty127 */ - Public voidSetstartscreen () {129System.out.println ("******* game starts ********"); theSystem.out.println ("******* please set the game difficulty ********");131Scanner Scanner =NewScanner (system.in); theSystem.out.println ("Please enter the total number of vehicles involved:");133Num_car =scanner.nextint ();134 Do {135 136System.out.println ("Please enter the parking space:");137Num_park =scanner.nextint ();138 if(Num_car >Num_park) {139System.out.println ("The number of parking spaces is too small, please re-enter:"); $ }141} while(Num_car >Num_park);142 143 }144 145 /**146 * Print List collection information147 * 148 * @paramList149 */ Max Public voidSelectAll (list<car>list, String type) {151 Switch(type) { the Case"Car":153 for(Car car:list) {154SYSTEM.OUT.PRINTLN ("License plate number:" + car.getid () + "| Model:" + car.gettype () + "| Color:" +Car.getcolour ());155 }156 Break;157 Case"Park":158 for(Car car:list) {159SYSTEM.OUT.PRINTLN ("Space occupancy:" + car.getid () + "| Model:" + car.gettype () + "| Color:" +Car.getcolour ()); the }161 Break;162 default:163 Break;164 }165 166 }167 168}
Grabparkingspacesgame (Comlex)