Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In JAVA While out on a standard run we come across a strange planet that has a s

ID: 3910475 • Letter: I

Question

In JAVA

While out on a standard run we come across a strange planet that has a ship emitting an SOS beacon. We decide to proceed, not to help any survivors but to search for items. After our descent we find a crashed ship with a full cargo hold! We decide to take the time to empty their cargo hold and search through all of the items to determine what to take. We notice that they don’t store their items in the same boxes that we do, but rather have a system that creates specific size boxes for each item. This means that they can store 200 items if they are light enough, or 1 large heavy item (Remember, weight matters when trying to leave a planet!).

While you continue doing the grunt work of unloading the ship I head back to our ship to try and update it with the same features as the newly discovered, but wrecked, ship. I get started as you finish unloading and reloading everything, so I head out to search for survivors while you finish the upgrades.

Items now have attributes such as Name, Weight, Value, Durability and ID. (Create an object called ‘Item’)

We can carry an unlimited number of items, as long as they don’t exceed the maximum weight of the cargo bay, 25 Tons. (Use an ArrayList that checks an item’s weight before placing it in the cargo hold)

We need to be able to add and remove items by their name.

We need to be able to search for a specific type of item in our cargo bay based on the item’s name and one of its attributes (Implement 2 searches – one on name and another on any attribute you choose).

We need to be able to sort items by their names alphabetically in descending order (A-Z)

We need to know how many of each item we have in our cargo bay and display their attributes.

We must also add a partial search (think of this as a ‘filter’ option).

Using the same code as assignment 2 you can make your changes. I have included some base code for your convenience (This is 2 classes, Assignment2 and Item.

import java.util.ArrayList;

import java.util.Scanner;

public class Assignment03Driver {

       Scanner input = new Scanner(System.in);

       public static void main(String[] args) {

              new Assignment01Driver();

       }

       // This will act as our program switchboard

       public Assignment03Driver() {

              ArrayList<Item> cargohold = new ArrayList<Item>();

              System.out.println("Welcome to the BlackStar Cargo Hold interface.");

              System.out.println("Please select a number from the options below");

              System.out.println("");

              while (true) {

                      // Give the user a list of their options

                      System.out.println("1: Add an item to the cargo hold.");

                      System.out.println("2: Remove an item from the cargo hold.");

                      System.out.println("3: Sort the contents of the cargo hold.");

                      System.out.println("4: Search for an item.");

                      System.out.println("5: Display the items in the cargo hold.");

                      System.out.println("6: Perform a partial search for an item.");

                      System.out.println("0: Exit the BlackStar Cargo Hold interface.");

                      // Get the user input

                      int userChoice = input.nextInt();

                      input.nextLine();

                      switch (userChoice) {

                      case 1:

                             addItem(cargohold);

                             break;

                      case 2:

                             removeItem(cargohold);

                             break;

                      case 3:

                             sortItems(cargohold);

                             break;

                      case 4:

                             searchItems(cargohold);

                             break;

                      case 5:

                             displayItems(cargohold);

                             break;

                      case 6:

                             partialSearch(cargohold);

                             break;

                      case 0:

                             System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");

                             System.exit(0);

                      }

              }

       }

       private void addItem(ArrayList<Item> cargohold) {

              // TODO: Add an item that is specified by the user

       }

       private void removeItem(ArrayList<Item> cargohold) {

              // TODO: Remove an item that is specified by the user

       }

       private void sortItems(ArrayList<Item> cargohold) {

              // TODO: Sort the items in the cargo hold (No need to display them here) - Use Selection or Insertion sorts

              // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings

       }

       private void searchItems(ArrayList<Item> cargohold) {

              // TODO: Search for a user specified item

       }

       private void displayItems(ArrayList<Item> cargohold) {

              // TODO: Display only the unique items along with a count of any duplicates

              //

              // For example it should say

              // Food - 2

              // Water - 3

              // Ammunition - 5

       }

       private void partialSearch(ArrayList<Item> cargohold) {

              // Search for an item based on a partial name

       }

}

// This item class should be stored in its own file.

public class Item {

       // Declare attributes here

      

       public Item(){

             

       }

      

       // Create an overridden constructor here

      

       // Create accessors and mutators for your traits

}

Explanation / Answer

Below is the solution:

code:

package chegg;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
//import Item.ComparableItem;
import java.util.ArrayList;

       public class Assignment03Driver {
          Scanner input = new Scanner(System.in);

          public static void main(String[] args) {
              new Assignment03Driver();
          }

          // This will act as our program switchboard
          public Assignment03Driver() {
             
              ArrayList<Item> cargohold = new ArrayList<Item>();
             
              System.out.println("Welcome to the BlackStar Cargo Hold interface.");
              System.out.println("Please select a number from the options below");
              System.out.println("");

              while (true) {
                  // Give the user a list of their options
                  System.out.println("1: Add an item to the cargo hold.");
                  System.out.println("2: Remove an item from the cargo hold.");
                  System.out.println("3: Sort the contents of the cargo hold.");
                  System.out.println("4: Search for an item.");
                  System.out.println("5: Display the items in the cargo hold.");
                  System.out.println("0: Exit the BlackStar Cargo Hold interface.");

                  // Get the user input
                  int userChoice = input.nextInt();
                  input.nextLine();
                 
                  switch (userChoice) {
                      case 1:
                          addItem(cargohold);
                          break;
                      case 2:
                          removeItem(cargohold);
                          break;
                      case 3:
                          //sortItems(cargohold);
                          break;
                      case 4:
                          searchItems(cargohold);
                          break;
                      case 5:
                          displayItems(cargohold);
                          break;
                      case 0:
                          System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");
                          System.exit(0);
                      default:
                          System.out.println("Invalid value. Choose a number 0-5 only.");
                          break;
                  }
              }

          }

          private void addItem(ArrayList<Item> cargohold) {
              cargohold.add(new Item());
              int index = cargohold.size();
              Item tempItem = new Item();
              System.out.println("Enter the Item's name");
              String userInput = input.nextLine();
              tempItem.setItemName(userInput);
              System.out.println("Enter the Item's weight");
              userInput = input.nextLine();
              tempItem.setItemWeight(userInput);
              System.out.println("Enter the Item's value");
              userInput = input.nextLine();
              tempItem.setItemValue(userInput);
              System.out.println("Enter the Item's durability");
              userInput = input.nextLine();
              tempItem.setItemDurability(userInput);
              System.out.println("Enter the Item's ID");
              userInput = input.nextLine();
              tempItem.setItemID(userInput);
             
              cargohold.set(index - 1, tempItem);
              System.out.println("Item added successfully");
              return;
          }

          private void removeItem(ArrayList<Item> cargohold) {
              int index = cargohold.size();
             
              if(index == 0) {
                  System.out.println("The cargohold has no items to remove!");
                  return;
              } else {
                 
                  System.out.println("Enter the name of the item to be removed.");
                  String userInput = input.nextLine();
                 
                  for(int i = 0; i < cargohold.size(); i++) {
                      cargohold.get(i);
                      if(Item.name.equals(userInput)) {
                          cargohold.get(i);
                          Item.name = "none";
                          cargohold.get(i);
                          Item.weight = "none";
                          cargohold.get(i);
                          Item.value = "none";
                          cargohold.get(i);
                          Item.durability = "none";
                          cargohold.get(i);
                          Item.ID = "none";
                          System.out.println("Item removed.");
                          break;
                      } else if (i == cargohold.size() - 1) {
                          System.out.println("That item is not in the cargohold.");
                      }
                  }
                  return;
              }
          }

       /*   private void sortItems(ArrayList<Item> cargohold) {
              int index = cargohold.size();
             
              if(index <= 1) {
                  System.out.println("The cargohold has too few items to sort.");
                  return;
              } else {
                  System.out.println(compareTo(cargohold.get(1), cargohold.get(2)));
              }
             
              for(int x = 0; x < 5; x++) { //this for loop repeats the sorting process according to the length of the array to enhance sorting accuracy
                  for (int i = 0; i < cargohold.size(); i++) {
                      int minimum = i;
                      //System.out.println(minimum);
                      for (int j = i + 1; j < cargohold.length; j++) {
                          //System.out.println(j);
                          //System.out.println(cargohold[i].compareTo(cargohold[minimum]));
                          if(!cargohold[j].name.equals("none")) {
                              if(cargohold[j].name.compareTo(cargohold[minimum].name) < 0) {
                                  minimum = j;
                                  //Debug Note: Outputs array reassignments in real time.
                                  //System.out.println("Swapping " + cargohold[i] + " with " + cargohold[minimum]);
                                  //System.out.println(Arrays.asList(cargohold));
                                  Item transfer = cargohold[i];
                                  cargohold[i] = cargohold[minimum];
                                  cargohold[j] = transfer;
                              }
                          }
                      }
                  }
              }
              System.out.println("cargohold sorted.");
             
              return;
          }*/

          private void searchItems(ArrayList<Item> cargohold) {
              System.out.println("Enter the name of the item you wish to search for.");
              String userInput = input.nextLine();
             
              for(int i = 0; i < cargohold.size(); i++) {
                  if (userInput.equals(cargohold.get(i).name)) {
                      System.out.println("The item was found at section number " + (i + 1));
                      return;
                  }
              }
              System.out.println("The item was not found in the cargo hold.");
          }

          private void displayItems(ArrayList<Item> cargohold) {
              while (true) {
                  // Give the user a list of their options
                  System.out.println("1: Display all items in the cargohold");
                  System.out.println("2: Display a specific item in the cargohold");

                  // Get the user input
                  int userChoice = input.nextInt();
                  input.nextLine();
                 
                  switch (userChoice) {
                      case 1:
                          //simple output
                          for(int i = 0; i < cargohold.size(); i++) {
                              cargohold.get(i);
                              System.out.println(Item.name);
                              cargohold.get(i);
                              System.out.println(Item.weight);
                              cargohold.get(i);
                              System.out.println(Item.value);
                              cargohold.get(i);
                              System.out.println(Item.durability);
                              cargohold.get(i);
                              System.out.println(Item.ID);
                         
                          }
                          return;
                      case 2:
                         
                          return;
                      default:
                          System.out.println("Invalid value. Choose a number 1-2 only.");
                          break;
                  }
              }
          }
       }

Item.java:

package chegg;

public class Item {
   static String name = "none";
   static String weight = "none";
   static String value = "none";
   static String durability = "none";
   static String ID = "none";
          
   Item(){}
  
   //constructor
   public Item(String itemName, String itemWeight, String itemValue, String itemDurability, String itemID) {
       Item.name = itemName;
       Item.weight = itemWeight;
       Item.value = itemValue;
       Item.durability = itemDurability;
       Item.ID = itemID;
   }
  
   //setter methods
   public void setItemName(String newItemName) {
       Item.name = newItemName;
   }
  
   public void setItemWeight(String newItemWeight) {
       Item.weight = newItemWeight;
   }
  
   public void setItemValue(String newItemValue) {
       Item.value = newItemValue;
   }
  
   public void setItemDurability(String newItemDurability) {
       Item.durability = newItemDurability;
   }
  
   public void setItemID(String newItemID) {
       Item.ID = newItemID;
   }
  
   //getter methods
   public String getItemName() {
       return name;
   }
  
   public String getItemWeight() {
       return weight;
   }
  
   public String getItemValue() {
       return value;
   }
  
   public String getItemDurability() {
       return durability;
   }
  
   public String getItemID() {
       return ID;
   }
  
  
   //comparison object
  
   public class ComparableItem extends Item
   implements Comparable<ComparableItem> {
       public ComparableItem(String itemName, String itemWeight, String itemValue, String itemDurability, String itemID) {
           super(name, weight, value, durability, ID);
       }
   }
  
   public int compareTo(ComparableItem o) {
       return this.getItemName().compareTo(o.getItemName());
   }
  
}

sample output:

Welcome to the BlackStar Cargo Hold interface.
Please select a number from the options below

1: Add an item to the cargo hold.
2: Remove an item from the cargo hold.
3: Sort the contents of the cargo hold.
4: Search for an item.
5: Display the items in the cargo hold.
0: Exit the BlackStar Cargo Hold interface.
1
Enter the Item's name
Hello
Enter the Item's weight
20
Enter the Item's value
3
Enter the Item's durability
5
Enter the Item's ID
111
Item added successfully
1: Add an item to the cargo hold.
2: Remove an item from the cargo hold.
3: Sort the contents of the cargo hold.
4: Search for an item.
5: Display the items in the cargo hold.
0: Exit the BlackStar Cargo Hold interface.
5
1: Display all items in the cargohold
2: Display a specific item in the cargohold
1
Hello
20
3
5
111
1: Add an item to the cargo hold.
2: Remove an item from the cargo hold.
3: Sort the contents of the cargo hold.
4: Search for an item.
5: Display the items in the cargo hold.
0: Exit the BlackStar Cargo Hold interface.
4
Enter the name of the item you wish to search for.
Hello
The item was found at section number 1
1: Add an item to the cargo hold.
2: Remove an item from the cargo hold.
3: Sort the contents of the cargo hold.
4: Search for an item.
5: Display the items in the cargo hold.
0: Exit the BlackStar Cargo Hold interface.
4
Enter the name of the item you wish to search for.
Hello
The item was found at section number 1
1: Add an item to the cargo hold.
2: Remove an item from the cargo hold.
3: Sort the contents of the cargo hold.
4: Search for an item.
5: Display the items in the cargo hold.
0: Exit the BlackStar Cargo Hold interface.
4
Enter the name of the item you wish to search for.
he
The item was not found in the cargo hold.
1: Add an item to the cargo hold.
2: Remove an item from the cargo hold.
3: Sort the contents of the cargo hold.
4: Search for an item.
5: Display the items in the cargo hold.
0: Exit the BlackStar Cargo Hold interface.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote