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

• Create an algorithm that maximizes the amount of money we will get if we ever

ID: 3585536 • Letter: #

Question

• Create an algorithm that maximizes the amount of money we will get if we ever come across this situation again. We will call this method ‘ransack’ and it should accept a large list of items and return the optimal list of items we should grab based on our weight limit. • We must 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. • 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 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) • The following methods still have the same requirements from the assignment previous to this one: Add, Remove, Search, Sort, Filter, and Display. Note: With your submission of this assignment you must include all of your files AND your file that you created to load into your program.

Base code:

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

/**

*
* @author Sam
*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

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("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) {
        System.out.println("Enter name of item to add: ");
        cargohold.add(new Item(input.nextLine()));
    }

    private void removeItem(ArrayList<Item> cargohold) {
        System.out.println("Enter name of item to remove: ");
        cargohold.remove(new Item(input.nextLine()));

    }

    private void sortItems(ArrayList<Item> 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<Item> 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<Item> cargohold) {
         Map<String, Integer> 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<String> item = map.keySet().iterator();
      
         while(item.hasNext()) {
             String name = item.next();
             System.out.println(name + " - " + map.get(name));
          
         }
    }

   private void partialSearch(ArrayList<Item> 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");
   }
}

// This item class should be stored in its own file.
class Item {
   String name;
   double weight, value, durability;
   String id;
   public Item(String name){
         this.name = name;
   }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Item other = (Item) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}