Question

Java Programming Question

After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would like to sell any extra weight we might have. We agree to sell him some items, however, we do not want to sell just anything, as some of our loot might have a better credit rate elsewhere. Unfortunately, we are unable to tell what kind of item is what, so we are unable to make the sale. On our way to the nearest space station, we think of a way to classify the items in our cargo bay. We decide that it would be best to classify the items in our hold as equipmentconsumable, or material. When we arrive at the station, we create a clear hierarchy for all of our items. After fencing our loot, we decide to rest at this station for the night.

  • 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 Equipment, Consumable, or Material. (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 up to 10 items, as long as they don't exceed the maximum weight of the cargo bay, 25 Tons. (Use an Array 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 the name and another on any attribute you choose).

  • We need to be able to sort items by their names alphabetically in ascending order (Using an Insertion or Selection sort only)

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

The driver you are to use has been attached to the module.

Note: You CANNOT modify the AssignmentDriver file, you should only modify the file called StudentMethods.java and you CANNOT change the method headers. You can add other methods as you see fit, but do not modify any of the method headers I have given you. This will break the testing program I have, and will result in a lower grade on your assignment.


Use the following code as your base.


package Assignments;



import java.util.Scanner;


public class Assignment03Driver {

StudentMethods03 studentMethods = new StudentMethods03();

Scanner input = new Scanner(System.in);


public static void main(String[] args) {

new Assignment03Driver();

}


public Assignment03Driver() {

Item[] cargohold = new Item[10];

System.out.println("Welcome to the Discipulus 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 by name.");

System.out.println("5: Search for an item by ID.");

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

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

int userChoice = input.nextInt();

input.nextLine();

switch (userChoice) {

case 1:

studentMethods.addItem(cargohold);

break;

case 2:

studentMethods.removeItem(cargohold);

break;

case 3:

studentMethods.sortItems(cargohold);

break;

case 4:

studentMethods.searchItemsByName(cargohold);

break;

case 5:

studentMethods.searchItemsByID(cargohold);

break;

case 6:

studentMethods.displayItems(cargohold);

break;

case 0:

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

System.exit(0);

}

}

}

}



package Assignments;



import java.util.Scanner;


public class StudentMethods03 {

  Item[] cargohold;

  int numItems;

  Scanner input = new Scanner(System.in);

   

  public static void main(String[] args) {

     new StudentMethods03();

      

  }


  public StudentMethods03() {

     cargohold = new Item[10];

     numItems = 0;

      

     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("0: Exit the BlackStar Cargo Hold interface.");

         

        int userChoice = input.nextInt();

        input.nextLine();

         

        switch (userChoice) {

        case 1:

           addItem();

           break;

        case 2:

           removeItem();

           break;

        case 3:

           sortItems();

           break;

        case 4:

           searchItems();

           break;

        case 5:

           displayItems();

           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() {

     if (numItems == cargohold.length) {

        System.out.println("Cargo Hold is full, remove some items first");

        return;

     }  

      

     System.out.println("Enter Item Name: ");

     String name = input.nextLine();

      

     System.out.println("Enter Weight: ");

     double weight = Double.parseDouble(input.nextLine());

      

     System.out.println("Enter Value: ");

     double value = Double.parseDouble(input.nextLine());

      

     System.out.println("Enter durability: ");

     int durability = Integer.parseInt(input.nextLine());


     System.out.println("Enter Numeric ID: ");

     int id = Integer.parseInt(input.nextLine());


     Item item = new Item(name, weight, value, durability, id);

     cargohold[numItems] = item;

     numItems++;


     System.out.println("Item added successfully!");

      

  }

   

  private void removeItem() {

     if (numItems == 0) {

        System.out.println("Cargo hold is empty!");

        return;

     }


     System.out.println("Enter item name");

     String name = input.nextLine();

     int index = searchByName(name);

     if (index == -1) {

       

        System.out.println("Item doesn't exist in the Cargo Hold");

     } else {

        for (int i = index; i < numItems - 1; i++) {

           cargohold[i] = cargohold[i + 1];

        }

        numItems--;

         

        System.out.println("Item removed successfuly");


     }


  }


  private void sortItems() {

  for (int i = 0; i < numItems - 1; ++i) {

        int min = i;

        for (int j = i + 1; j < numItems; ++j) {

           if (cargohold[j].getName().compareTo(cargohold[min].getName()) < 0) {

              min = j;

           }

        }

        Item temp = cargohold[i];

        cargohold[i] = cargohold[min];

        cargohold[min] = temp;

     }

     System.out.println("Items are sorted!");


  }

   

  private void searchItems() {

     System.out.println("1. Search by Name");

     System.out.println("2. Search by ID");

     int choice = Integer.parseInt(input.nextLine());

     if (choice == 1) {

       

        System.out.println("Enter name: ");

        String name = input.nextLine();

        int index = searchByName(name);

        if (index == -1) {

        

           System.out.println("Not found");

        } else {

        

           System.out.println("Item found at position " + index);

           System.out.println("Name: " + cargohold[index].getName());

           System.out.println("ID: " + cargohold[index].getID());

        }

     } else if (choice == 2) {

       

        System.out.println("Enter ID: ");

        int id = Integer.parseInt(input.nextLine());

        int index = searchByID(id);

        if (index == -1) {

        

           System.out.println("Not found");

        } else {

        

           System.out.println("Item found at position " + index);

           System.out.println("Name: " + cargohold[index].getName());

           System.out.println("ID: " + cargohold[index].getID());

        }

     }

  }


  private int searchByName(String name) {

     for (int i = 0; i < numItems; i++) {

        if (cargohold[i].getName().equalsIgnoreCase(name)) {

           return i;

        }

     }

     return -1;

  }

   

  private int searchByID(int ID) {

     for (int i = 0; i < numItems; i++) {

        if (cargohold[i].getID() == ID) {

           return i;

        }

     }

     return -1;

  }


  private void displayItems() {

     Item[] uniqueItems = new Item[numItems];     

     int[] counts = new int[numItems];

     int counter = 0;

     for (int i = 0; i < numItems; i++) {

        int foundIndex = -1;

        String name = cargohold[i].getName();

        for (int j = 0; j < counter; j++) {

           if (uniqueItems[j].getName().equalsIgnoreCase(name)) {

              foundIndex = j;

           }

        }

        if (foundIndex == -1) {

           uniqueItems[counter] = cargohold[i];

           counts[counter]++;

           counter++;

        } else {          

           counts[foundIndex]++;

        }

     }

     for (int i = 0; i < counter; i++) {

        System.out.println("Item Name: " + uniqueItems[i].getName()

              + ", count - " + counts[i]);

     }


  }


public void addItem(Item cargohold[]) {

}


public void removeItem(Item cargohold[]) {

}


public void sortItems(Item cargohold[]) {


}


public void searchItemsByName(Item cargohold[]) {

}


public void searchItemsByID(Item cargohold[]) {

}


public void displayItems(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

}






}


package Assignments;


public class Item {


   private String name;

   private double weight;

   private double value;

   private int durability;

   private int ID;



   public Item(){

    

   }


   public Item(String name, double weight, double value, int durability, int iD) {

      this.name = name;

      this.weight = weight;

      this.value = value;

      this.durability = durability;

      ID = iD;

   }


   /* getters and setters

    * */


   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

   public double getWeight() {

      return weight;

   }

   public void setWeight(double weight) {

      this.weight = weight;

   }

   public double getValue() {

      return value;

   }

   public void setValue(double value) {

      this.value = value;

   }

   public int getDurability() {

      return durability;

   }

   public void setDurability(int durability) {

      this.durability = durability;

   }


   public int getID() {

      return ID;

   }

   public void setID(int iD) {

      ID = iD;

   }


}


0 0
Add a comment Improve this question
✔ Recommended Answer
Answer #1

package Project;


import java.util.Arrays;

import java.util.Collections;

import java.util.Scanner;


import Item.ComparableItem;


import java.util.ArrayList;


public class Assignment3 {

   Scanner input = new Scanner(System.in);

   

   public static void main(String[] args) {

      new Assignment3();

   }

   

   // This will act as our program switchboard

   public Assignment3() {

      

      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;

         }

      }

   }

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Java Programming Question
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • java problems

    1)Which of the following class definitions is correct in Java?(i)public class Employee{private String name;private double salary;private int id;public Employee(){name = "";salary = 0.0;id = 0;}public Employee(String n, double s, int i){name = n;salary = s;id = i;}public void print(){System.out.println(name + " " + id + " " + salary);}}(ii)public class Employee{private String name;private double salary;private int id;public void Employee(){name = "";salary = 0.0;id = 0;}public void Employee(String n, double s, int i){name = n;salary = s;id = i;}public void print(){System.out.println(name...

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • Purchase Class Java help

    Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items asindicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemoprogram. Then print the total bill at the endUse the readInput() method for the following input dataOranges: 10 for 2.99 buy 2 dozen orangesEggs: 12 for 1.69 buy 3...

  • Java Programming exercise 10.7 simulating an atm

    The question is:Use the Account class created in exercise 8.7 (I will add my code for that problem at the bottom) to simulate an ATM machine. Create ten accounts in an arrray with id0,1, ...., 9. and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. once an id isaccepted the main menu is displayed as shown in the sample run. You can...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Hey guys I am having trouble getting my table to work with a java GUI if...

    Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT