Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package semester_2_project;
- import java.util.ArrayList;
- import java.util.Scanner;
- /*
- Group names:
- Daryan Latif Ali
- Hedy Soran Osman
- Hawre Zyad Muhammad
- */
- public class Semester_2_project {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- ArrayList<String> cars = new ArrayList<String>();
- ArrayList<Integer> prices = new ArrayList<Integer>();
- ArrayList<Boolean> state = new ArrayList<Boolean>();
- String car_input;
- int price_input;
- boolean state_input;
- int num_input;
- int index_value;
- System.out.println("Welcome to car dealership management:\n"
- + "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.");
- try {
- int in = input.nextInt();
- while (in != 0) {
- switch (in) {
- case 1:
- System.out.print("Enter car name: ");
- car_input = input.next(); // bmw
- System.out.print("Enter " + car_input + "\'s price: ");
- price_input = input.nextInt(); // 1000
- System.out.print("Is this a used car? [true/false]: ");
- state_input = input.nextBoolean(); // true
- cars.add(car_input); // 0 = bmw
- prices.add(price_input); // 0 = 1000
- state.add(state_input); // 0 = true
- System.out.printf("Successfully added %s to your list for %d [Car used: %s]\n", car_input, price_input, state_input);
- break;
- case 2:
- if (cars.size() > 0) {
- System.out.println("Which car would you like to update? (Enter index number)");
- for (int i = 0; i < cars.size(); i++) {
- System.out.printf("index %d: %s\n", i, cars.get(i));
- }
- index_value = input.nextInt();
- System.out.printf("What would you like to update for %s? [1: name, 2: price, 3: state]\n", cars.get(index_value));
- num_input = input.nextInt();
- if (num_input == 1) {
- String old_name = cars.get(index_value);
- System.out.printf("Update name for %s\n", old_name);
- car_input = input.next();
- cars.set(index_value, car_input);
- System.out.printf("Successfully updated name from %s to %s\n", old_name, cars.get(index_value));
- } else if (num_input == 2) {
- int old_price = prices.get(index_value);
- System.out.printf("Update price for %s (currently %d)\n", cars.get(index_value), old_price);
- price_input = input.nextInt();
- prices.set(index_value, price_input);
- System.out.printf("Successfully updated name from %s to %s\n", old_price, prices.get(index_value));
- } else if (num_input == 3) {
- boolean old_state = state.get(index_value);
- System.out.printf("Update state for %s [true/false]\n", cars.get(index_value));
- state_input = input.nextBoolean();
- state.set(index_value, state_input);
- System.out.printf("Successfully updated name from %s to %s\n", old_state, state.get(index_value));
- } else {
- System.out.println("Invalid option");
- }
- } else {
- System.out.println("Your list is empty");
- }
- break;
- case 3:
- if (cars.size() > 0) {
- System.out.println("Which car would you like to remove? (enter index number)");
- for (int i = 0; i < cars.size(); i++) {
- System.out.printf("index %d: %s\n", i, cars.get(i));
- }
- index_value = input.nextInt();
- String old_name = cars.get(index_value);
- cars.remove(index_value);
- prices.remove(index_value);
- state.remove(index_value);
- System.out.printf("Successfully removed %s\n", old_name);
- } else {
- System.out.println("Your list is empty");
- }
- break;
- case 4:
- if (cars.size() > 0) {
- System.out.println("NO.\t\tName\t\tPrice\t\tUsed car");
- for (int i = 0; i < cars.size(); i++) {
- int car_id = i + 1;
- System.out.println(car_id + "\t\t" + cars.get(i) + "\t\t" + prices.get(i) + "\t\t" + state.get(i));
- }
- } else {
- System.out.println("Your list is empty.");
- }
- break;
- default:
- System.out.println("Invalid option, try again.");
- break;
- }
- System.out.println("********************************************");
- 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.");
- in = input.nextInt();
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Add Comment
Please, Sign In to add comment