using while loop to run java program multiple times in java

Solutions on MaxInterview for using while loop to run java program multiple times in java by the best coders in the world

showing results for - "using while loop to run java program multiple times in java"
Gabriela
17 Jan 2019
1import java.io.Console;
2import java.util.List;
3import java.util.ArrayList;
4
5public class App {
6  public static void main(String[] args) {
7    Console myConsole = System.console();
8
9    Vehicle hatchback = new Vehicle(1994, "Subaru", "Legacy", 170000, 4000);
10    Vehicle suv = new Vehicle(2002, "Ford", "Explorer", 100000, 7000);
11    Vehicle sedan = new Vehicle(2015, "Toyota", "Camry", 50000, 30000);
12    Vehicle truck = new Vehicle(1999, "Ford", "Ranger", 100000, 4000);
13    Vehicle crossover = new Vehicle(1998, "Toyota", "Rav-4", 200000, 3500);
14
15    List<Vehicle> allVehicles = new ArrayList<Vehicle>();
16    allVehicles.add(hatchback);
17    allVehicles.add(suv);
18    allVehicles.add(sedan);
19    allVehicles.add(truck);
20    allVehicles.add(crossover);
21
22    while(true){
23      System.out.println("Welcome to our car dealership. What would you like to do? Enter one of the following options: All Vehicles, Search Price or Add Vehicle");
24      String navigationChoice = myConsole.readLine();
25
26      if (navigationChoice.equals("All Vehicles")){
27        for ( Vehicle individualVehicle : allVehicles ) {
28          System.out.println( "----------------------" );
29          System.out.println( individualVehicle.mYear );
30          System.out.println( individualVehicle.mBrand );
31          System.out.println( individualVehicle.mModel );
32          System.out.println( individualVehicle.mMiles );
33          System.out.println( individualVehicle.mPrice );
34        }
35      } else if (navigationChoice.equals("Search Price")){
36          System.out.println("What is your maximum budget for a vehicle?");
37          String stringUserMaxBudget = myConsole.readLine();
38          int userMaxBudget = Integer.parseInt(stringUserMaxBudget);
39          System.out.println("Alright, here's what we have in your price range:");
40        for ( Vehicle individualVehicle : allVehicles ) {
41          if (individualVehicle.worthBuying(userMaxBudget)){
42            System.out.println( "----------------------" );
43            System.out.println( individualVehicle.mYear );
44            System.out.println( individualVehicle.mBrand );
45            System.out.println( individualVehicle.mModel );
46            System.out.println( individualVehicle.mMiles );
47            System.out.println( individualVehicle.mPrice );
48          }
49        }
50      } else if (navigationChoice.equals("Add Vehicle")){
51          System.out.println("Alright, let's add a vehicle! What year was this vehicle made?");
52          int userVehicleYear = Integer.parseInt(myConsole.readLine());
53          System.out.println("Great! What make or brand is the vehicle?");
54          String userVehicleBrand = myConsole.readLine();
55          System.out.println("Got it! What model is it?");
56          String userVehicleModel = myConsole.readLine();
57          System.out.println("And how many miles does it have on it?");
58          int userVehicleMiles = Integer.parseInt(myConsole.readLine());
59          System.out.println("Finally, what's its price?");
60          int userVehiclePrice = Integer.parseInt(myConsole.readLine());
61          Vehicle userVehicle = new Vehicle(userVehicleYear, userVehicleBrand, userVehicleModel, userVehicleMiles, userVehiclePrice);
62          allVehicles.add(userVehicle);
63          System.out.println("Alright, here's your new vehicle:");
64          System.out.println( "----------------------" );
65          System.out.println( userVehicle.mYear );
66          System.out.println( userVehicle.mBrand );
67          System.out.println( userVehicle.mModel );
68          System.out.println( userVehicle.mMiles );
69          System.out.println( userVehicle.mPrice );
70      } else {
71          System.out.println("I'm sorry, we don't recognize your input");
72      }
73    }
74  }
75}
76
77
similar questions