Advertisement
Guest User

Assignment01Driver.java

a guest
Jan 29th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5.  
  6.     public class Assignment01Driver {
  7.         Scanner input = new Scanner(System.in);
  8.  
  9.         public static void main(String[] args) {
  10.             new Assignment01Driver();
  11.         }
  12.  
  13.         // This will act as our program switchboard
  14.         public Assignment01Driver() {
  15.             String[] cargohold = new String[10];
  16.             System.out.println("Welcome to the BlackStar Cargo Hold interface.");
  17.             System.out.println("Please select a number from the options below");
  18.             System.out.println("");
  19.  
  20.             while (true) {
  21.                 // Give the user a list of their options
  22.                 System.out.println("1: Add an item to the cargo hold.");
  23.                 System.out.println("2: Remove an item from the cargo hold.");
  24.                 System.out.println("3: Sort the contents of the cargo hold.");
  25.                 System.out.println("4: Search for an item.");
  26.                 System.out.println("5: Display the items in the cargo hold.");
  27.                 System.out.println("0: Exit the BlackStar Cargo Hold interface.");
  28.                 try{
  29.                 // Get the user input
  30.                 int userChoice = input.nextInt();
  31.                 input.nextLine();
  32.  
  33.                 switch (userChoice) {
  34.                     case 1:
  35.                         addItem(cargohold);
  36.                         break;
  37.                     case 2:
  38.                         removeItem(cargohold);
  39.                         break;
  40.                     case 3:
  41.                         sortItems(cargohold);
  42.                         break;
  43.                     case 4:
  44.                         searchItems(cargohold);
  45.                         break;
  46.                     case 5:
  47.                         displayItems(cargohold);
  48.                         break;
  49.                     case 0:
  50.                         System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");
  51.                         System.exit(0);
  52.                     default:
  53.                         System.err.println("Please select a valid option");
  54.                         continue;
  55.                 }
  56.                 }catch (InputMismatchException e){
  57.                     System.out.println("Please select an option 1-5 or 0 to exit the program");
  58.                     input.nextLine();
  59.                     }
  60.  
  61.             }
  62.             }
  63.  
  64.  
  65.         // TODO: Add an item that is specified by the user
  66.         private void addItem(String cargohold[]) {
  67.             System.out.print("Enter the name of the item: ");
  68.             String item = input.nextLine();
  69.             int index = -1;
  70.             for(int i=0; i<cargohold.length;i++){
  71.                 if (cargohold[i]==(null)){
  72.                     index = i;
  73.                     break;
  74.                 }
  75.             }
  76.             if(index!=cargohold.length&&index != -1){
  77.                 cargohold[index]=item;
  78.                 System.out.println("You have added " + cargohold[index] + "\n");
  79.                 System.out.println("Press Enter to continue");
  80.                 input.nextLine();
  81.             }else{
  82.                 System.out.println("No more items can be stored");
  83.                 System.out.println("Press Enter to continue");
  84.                 input.nextLine();
  85.             }
  86.  
  87.  
  88.  
  89.  
  90.         }
  91.  
  92.         // TODO: Remove an item that is specified by the user
  93.         // removeItem() to remove an item from the array
  94.         private void removeItem(String cargohold[]) {
  95.             String item;
  96.             int index = 0;
  97.             // prompt the user for the item name to remove
  98.             System.out.println("Enter item you would like to remove: ");
  99.             item = input.nextLine();
  100.             // search for the item
  101.             for (int i = 0; i < cargohold.length; i++) {
  102.                 if (!cargohold[i].equals(null)) {
  103.                     if (cargohold[i].equalsIgnoreCase(item)) {
  104.                         index = i;
  105.                         break;
  106.                     }
  107.                 }
  108.             }
  109.             // if index is 0 display an error message
  110.             if (index == -1) {
  111.                 System.out.println("Sorry, your item is not found in the list.\n");
  112.                 System.out.println("Press Enter to continue");
  113.                 input.nextLine();
  114.             }else {
  115.  
  116.                 // if item is found
  117.                 for (int i = index; i < cargohold.length - 1; i++) {
  118.                     cargohold[i] = cargohold[i];
  119.                     System.out.println("You have removed " + item + " from the hold\n");
  120.                     break;
  121.                 }
  122.                 System.out.println("Press Enter to continue");
  123.                 input.nextLine();
  124.             }
  125.             // TODO: Sort the items in the cargo hold (No need to display them here) - Use Selection or Insertion sorts
  126.             // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings
  127.         }
  128.             private void sortItems(String cargohold[]) {
  129.             String sorting;
  130.             for (int i = 0; i < cargohold.length; i++) {
  131.                 for (int j = i + 1; j < cargohold.length; j++) {
  132.                     if (cargohold[i] != null && cargohold[j] != null && cargohold[i].compareTo(cargohold[j]) > 0) {
  133.                         sorting = cargohold[i];
  134.                         cargohold[i] = cargohold[j];
  135.                         cargohold[j] = sorting;
  136.                     }
  137.                 }
  138.  
  139.         }
  140.             System.out.println("Sort complete\nPress Enter to continue");
  141.             input.nextLine();
  142.         }
  143.  
  144.         // TODO: Search for a user specified item
  145.         private void searchItems(String cargohold[]) {
  146.             System.out.print("What Item would you like to search? ");
  147.             boolean found = false;
  148.             int counter = 0;
  149.             String choice = input.nextLine();
  150.             for (int i = 0; i < cargohold.length; i++) {
  151.                 if (cargohold[i] != null && cargohold[i].equals(choice)) {
  152.                     counter++;
  153.                     found = true;
  154.                 }
  155.             }
  156.             if (found) {
  157.                 System.out.print(choice + " - " + counter + "\n");
  158.             }
  159.             if (!found) {
  160.                 System.out.print("Item " + choice + " not found\n");
  161.             }
  162.             System.out.print("Press any key to continue");
  163.             input.nextLine();
  164.  
  165.         }
  166.             // TODO: Display only the unique items along with a count of any duplicates
  167.             private void displayItems(String cargohold[]) {
  168.                 ArrayList replicatedData = new ArrayList();
  169.                 for (int i = 0; i < cargohold.length; i++) {
  170.                     boolean found = false;
  171.                     for(int k=0;k<replicatedData.size();k++){
  172.                         if(replicatedData.get(k).equals(cargohold[i])){
  173.                             found = true;
  174.                         }
  175.                     }
  176.                     if(!found){
  177.                         replicatedData.add(cargohold[i]);
  178.                         int count = 0;
  179.                         for(int j=0;j<cargohold.length;j++){
  180.                             if(cargohold[i].equals(cargohold[j])){
  181.                                 count++;
  182.                             }
  183.                         }
  184.                         System.out.print(cargohold[i] + "-"+count+"\n");
  185.                     }
  186.                 }
  187.             }
  188.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement