Question: I have some base code below. I just need to know how to implement this part.(We must now store our items in a file and
I have some base code below. I just need to know how to implement this part.(We must now store our items in a file and read from the file if prompted by the user. You can create a file with the list of items so you never have to type them in again, and you can control the items that are introduced to our world.)The problem questions are below and the code is below that. //The sample test code can be any file that fits the parameters of the Item class. Items have attributes such as Name, Weight, Value, Durability and ID. (Create an object called Item) We now classify our items by separating them into 3 distinct categories Equipable, Consumable or Weapon. (You must implement these 3 classes that are subclasses of Item and they must have at least 3 unique attributes in each subclass) We can carry an unlimited number of items, as long as they dont exceed the maximum weight of the cargo bay, 25 Tons. (Use an ArrayList that checks an items 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 items 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). import java.util.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class Assignment 3 { Scanner input = new Scanner(System.in); public static void main(String[] args) { new Assignment 3(); } public Assignment 3() { ArrayList cargohold = new ArrayList(); 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) { 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."); 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 cargohold) { System.out.println("Enter name of item to add: "); cargohold.add(new Item(input.nextLine())); } private void removeItem(ArrayList cargohold) { System.out.println("Enter name of item to remove: "); cargohold.remove(new Item(input.nextLine())); } private void sortItems(ArrayList cargohold) { for (int i = 0; i < cargohold.size() - 1; i++ ) for (int j = 0; j < cargohold.size() - 1 - i; j++) if (cargohold.get(j).getName().compareToIgnoreCase(cargohold.get(j+1).getName()) > 0) { cargohold.add(j+1, cargohold.remove(j)); } } private void searchItems(ArrayList cargohold) { int n; System.out.println("Enter name of item to search: "); if ((n = cargohold.indexOf(new Item(input.nextLine()))) == -1) System.out.println("Item not found"); else System.out.println("Item found at location " + n); } private void displayItems(ArrayList cargohold) { Map map = new HashMap<>(cargohold.size()); for (int i = 0; i < cargohold.size(); i++) if (map.containsKey(cargohold.get(i).getName())) { int count = map.get(cargohold.get(i).getName()) + 1; map.put(cargohold.get(i).getName(), count); } else map.put(cargohold.get(i).getName(), 1); Iterator item = map.keySet().iterator(); while(item.hasNext()) { String name = item.next(); System.out.println(name + " - " + map.get(name)); } } private void partialSearch(ArrayList cargohold) { boolean flagFound = false; System.out.println("Enter name of item to search: "); String partialName = input.nextLine(); for (int i = 0; i < cargohold.size(); i++) if (cargohold.get(i).getName().contains(partialName)) { System.out.println("Found " + cargohold.get(i).getName() + " at location " + i); flagFound = true; } if (!flagFound) System.out.println("No such item found"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
