Daryan997

semester 2 project - programming

Oct 18th, 2020
2,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. package semester_2_project;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. /*
  7. Group names:
  8. Daryan Latif Ali
  9. Hedy Soran Osman
  10. Hawre Zyad Muhammad
  11. */
  12.  
  13. public class Semester_2_project {
  14.  
  15.     public static void main(String[] args) {
  16.         Scanner input = new Scanner(System.in);
  17.         ArrayList<String> cars = new ArrayList<String>();
  18.         ArrayList<Integer> prices = new ArrayList<Integer>();
  19.         ArrayList<Boolean> state = new ArrayList<Boolean>();
  20.         String car_input;
  21.         int price_input;
  22.         boolean state_input;
  23.         int num_input;
  24.         int index_value;
  25.         System.out.println("Welcome to car dealership management:\n"
  26.                 + "type (1) to add a car, type (2) to update a car,"
  27.                 + " type (3) to remove a car from the list,"
  28.                 + " type (4) to show all the cars or"
  29.                 + " type (0) to exit.");
  30.         try {
  31.             int in = input.nextInt();
  32.             while (in != 0) {
  33.                 switch (in) {
  34.                     case 1:
  35.                         System.out.print("Enter car name: ");
  36.                         car_input = input.next(); // bmw
  37.                         System.out.print("Enter " + car_input + "\'s price: ");
  38.                         price_input = input.nextInt(); // 1000
  39.                         System.out.print("Is this a used car? [true/false]: ");
  40.                         state_input = input.nextBoolean(); // true
  41.                         cars.add(car_input); // 0 = bmw
  42.                         prices.add(price_input); // 0 = 1000
  43.                         state.add(state_input); // 0 = true
  44.                         System.out.printf("Successfully added %s to your list for %d [Car used: %s]\n", car_input, price_input, state_input);
  45.                         break;
  46.                     case 2:
  47.                         if (cars.size() > 0) {
  48.                             System.out.println("Which car would you like to update? (Enter index number)");
  49.                             for (int i = 0; i < cars.size(); i++) {
  50.                                 System.out.printf("index %d: %s\n", i, cars.get(i));
  51.                             }
  52.                             index_value = input.nextInt();
  53.                             System.out.printf("What would you like to update for %s? [1: name, 2: price, 3: state]\n", cars.get(index_value));
  54.                             num_input = input.nextInt();
  55.                             if (num_input == 1) {
  56.                                 String old_name = cars.get(index_value);
  57.                                 System.out.printf("Update name for %s\n", old_name);
  58.                                 car_input = input.next();
  59.                                 cars.set(index_value, car_input);
  60.                                 System.out.printf("Successfully updated name from %s to %s\n", old_name, cars.get(index_value));
  61.                             } else if (num_input == 2) {
  62.                                 int old_price = prices.get(index_value);
  63.                                 System.out.printf("Update price for %s (currently %d)\n", cars.get(index_value), old_price);
  64.                                 price_input = input.nextInt();
  65.                                 prices.set(index_value, price_input);
  66.                                 System.out.printf("Successfully updated name from %s to %s\n", old_price, prices.get(index_value));
  67.                             } else if (num_input == 3) {
  68.                                 boolean old_state = state.get(index_value);
  69.                                 System.out.printf("Update state for %s [true/false]\n", cars.get(index_value));
  70.                                 state_input = input.nextBoolean();
  71.                                 state.set(index_value, state_input);
  72.                                 System.out.printf("Successfully updated name from %s to %s\n", old_state, state.get(index_value));
  73.                             } else {
  74.                                 System.out.println("Invalid option");
  75.                             }
  76.                         } else {
  77.                             System.out.println("Your list is empty");
  78.                         }
  79.                         break;
  80.                     case 3:
  81.                         if (cars.size() > 0) {
  82.                             System.out.println("Which car would you like to remove? (enter index number)");
  83.                             for (int i = 0; i < cars.size(); i++) {
  84.                                 System.out.printf("index %d: %s\n", i, cars.get(i));
  85.                             }
  86.                             index_value = input.nextInt();
  87.                             String old_name = cars.get(index_value);
  88.                             cars.remove(index_value);
  89.                             prices.remove(index_value);
  90.                             state.remove(index_value);
  91.                             System.out.printf("Successfully removed %s\n", old_name);
  92.                         } else {
  93.                             System.out.println("Your list is empty");
  94.                         }
  95.                         break;
  96.                     case 4:
  97.                         if (cars.size() > 0) {
  98.                             System.out.println("NO.\t\tName\t\tPrice\t\tUsed car");
  99.                             for (int i = 0; i < cars.size(); i++) {
  100.                                 int car_id = i + 1;
  101.                                 System.out.println(car_id + "\t\t" + cars.get(i) + "\t\t" + prices.get(i) + "\t\t" + state.get(i));
  102.                             }
  103.                         } else {
  104.                             System.out.println("Your list is empty.");
  105.                         }
  106.                         break;
  107.                     default:
  108.                         System.out.println("Invalid option, try again.");
  109.                         break;
  110.                 }
  111.                 System.out.println("********************************************");
  112.                 System.out.println("type (1) to add a car, type (2) to update a car, type (3) to remove a car from the list, type (4) to show all the cars or type (0) to exit.");
  113.                 in = input.nextInt();
  114.             }
  115.         } catch (Exception e) {
  116.             System.out.println(e);
  117.         }
  118.     }
  119.  
  120. }
Add Comment
Please, Sign In to add comment