In JAVA
While out on a standard run we stop in at our ‘local’ fence and try to sell some of our items. When we board his ship we notice that we are the only people on board, so we instantly start grabbing things and loading them on our ship and get ready to run. While grabbing things we realize that we are going to run out of space extremely quickly and we should prioritize what we grab to maximize our financial gain while staying under our weight limit.
We don’t have time to figure this out now as Gweedar might come back at any moment and catch us stealing from him – which would result in our instant death.
Create a RECURSIVE 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.
ItemsListProject.java
//Import required packages
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;
//class Declaration
public class ItemsListProject
{
//main method
public static void main(String[] args) throws IOException
{
//Create Constructor default to instantiate the class
new ItemsListProject();
}
//Create scanner object
Scanner input = new Scanner(System.in);
//Constructor initialization
public ItemsListProject() throws IOException
{
//Create a temp object
Item item = new Item();
//Create arraylist
ArrayList<Item> cargohold = new ArrayList<Item>();
File file=new File("fileItems.txt");
//declare boolean flag variable
boolean flag=false;
//Using try catch block to create file if not exists
try
{
flag=file.createNewFile();
}
catch(Exception e)
{
e.printStackTrace();
}
//Using while loop to prompt for the values of items
while(true)
{
System.out.println("1:Add an item to the cargo hold.");
System.out.println("2:Add an item attribute to the cargo hold.");
System.out.println("3:Remove an item from the cargo hold.");
System.out.println("4:Sort the contents of the cargo hold .");
System.out.println("5:Search for an item.");
System.out.println("6:Search for an item by attribute.");
System.out.println("7:Display the items in the cargo hold.");
System.out.println("8:Perform a partial search for an item.");
System.out.println("0:Exit the BlackStar Cargo Hold interface.");
System.out.print("\nPlease enter the option: ");
//Prompt and read the choice
int userChoice = input.nextInt();
//create the file to store the items
switch(userChoice)
{
//Case 1 to read the item and the values
case 1:
addItemB(cargohold, item);
break;
//To prompt for the item attributes
case 2:
System.out.println("1: Add an equipable attributre.");
System.out.println("2: Add a consumble attribute.");
System.out.println("3: Add a weapon attribute.");
userChoice = input.nextInt();
//Prompt for the item attributes
switch(userChoice)
{
case 1:
item = new Equipable();
addItem(cargohold, item);
break;
case 2:
item = new Consumable();
addItem(cargohold, item);
break;
case 3:
item = new Weapon();
addItem(cargohold, item);
break;
}
break;
//For the case 3
case 3:
removeItem(cargohold);
break;
//For Filter Item
case 4:
filterItem(cargohold);
break;
// search Item
case 5:
searchItem(cargohold);
break;
//case 6 for search trhough Item Attribute
case 6:
searchByAttribute(cargohold);
break;
case 7:
//DisplayItems
displayItem(cargohold);
break;
case 8:
partialSearch(cargohold);
break;
case 0:
System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");
System.exit(0);
}
if(changeInFile(cargohold, "fileItems.txt"))
{
//save the list to the text file
saveToFile("fileItems.txt",cargohold);
}
System.out.println();
}
}
private boolean changeInFile(ArrayList<Item> cargohold, String filename) throws IOException
{
//compare list of items with file content, return true incase of a change
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String itemInfo=null;
int i=0;
boolean change=false;
while((itemInfo = br.readLine()) != null && i<cargohold.size())
{
Item item=cargohold.get(i);
if(!itemInfo.equals(item.getID()+"\t"+item.getName()+"\t"+item.getWeight()+"\t"+item.getDurability()+"\t"+
item.getCategory()+"\t"+item.getValue())){
change=true;
break;
}
i++;
}
if(br.readLine()!=null || i<cargohold.size())
return true;
return false;
}
//Implement the method to save the items to the file
private void saveToFile(String fileName, ArrayList<Item> cargohold) throws IOException{
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
for(Item item: cargohold)
{
writer.write(item.getID()+"\t"+item.getName()+"\t"+item.getWeight()+"\t"+item.getDurability()+"\t"
+item.getCategory()+"\t"+item.getValue()+"\n");
}
//close the file
writer.close();
}
//add the items to the arrat list
private void addItemB(ArrayList<Item> cargohold, Item temp)
{
double getWeight = 0;
if(temp instanceof Item)
{
System.out.print("Enter the name of the item: ");
temp.setName(input.nextLine());
input.next();
System.out.print("Enter the item ID: ");
temp.setID(input.nextLine());
input.next();
System.out.print("Enter item weight: ");
getWeight = input.nextDouble();
if(getWeight > 25)
{
System.out.println("Weight Limit Exceed");
return;
}
else
{
System.out.print("Enter the item value: ");
temp.setValue(input.next());
System.out.print("Enter the item durability: ");
temp.setDurability(input.next());
cargohold.add(temp);
}
}
}
//add items to the list
private void addItem(ArrayList<Item> cargohold, Item temp)
{
if(temp instanceof Equipable)
{
System.out.print("Enter equipable item ID: ");
temp.setID(input.nextLine());
input.next();
System.out.print("Enter equipable attribute name: ");
temp.setName(input.nextLine());
input.next();
System.out.print("Enter equipable attribute color: ");
((Equipable)temp).setColor(input.nextLine());
input.next();
System.out.print("Is this an outfit (Y/N): ");
String outfit = input.nextLine();
String y = "y";
if(y.equalsIgnoreCase(outfit.trim()))
{
((Equipable)temp).setoutfit(true);
}
else
{
((Equipable)temp).setoutfit(false);
}
System.out.print("What is the item size: ");
((Equipable)temp).setSize(input.nextInt());
cargohold.add(temp);
}
else if(temp instanceof Consumable)
{
System.out.print("Enter the consumable ID: ");
((Consumable)temp).setID(input.nextLine());
input.next();
System.out.print("Enter the consumable name: ");
((Consumable)temp).setName(input.nextLine());
input.next();
System.out.print("Enter the consumable color: ");
((Consumable)temp).setColor(input.nextLine());
input.next();
System.out.print("Is the consumable a food (Y/N): ");
String Food = input.nextLine();
input.next();
String y = "y";
if(y.equalsIgnoreCase(Food.trim())){
((Consumable)temp).setFood(true);
}
else{
((Consumable)temp).setFood(false);
}
System.out.print("Is the consumable water(Y/N): ");
String Water = input.nextLine();
input.next();
if(y.equalsIgnoreCase(Water.trim())){
((Consumable)temp).setWater(true);
}
else{
((Consumable)temp).setWater(false);
}
System.out.print("Is the consumable healthy (Y/N): ");
String Healthy = input.nextLine();
input.next();
if(y.equalsIgnoreCase(Healthy.trim())){
((Consumable)temp).setHealthy(true);
}
else
{
((Consumable)temp).setHealthy(false);
}
cargohold.add(temp);
}
else
{
System.out.print("Enter weapon ID: ");
((Weapon)temp).setID(input.nextLine());
input.next();
System.out.print("What is the weapon name: ");
((Weapon)temp).setName(input.nextLine());
input.next();
System.out.print("Enter fungus color: ");
((Weapon)temp).setColor(input.nextLine());
input.next();
System.out.print("Is it a projectile (Y/N): ");
String Projectile = input.nextLine();
String y = "y";
if(y.equalsIgnoreCase(Projectile.trim())){
((Weapon)temp).setProjectile(true);
}
else{
((Weapon)temp).setProjectile(false);
}
cargohold.add(temp);
}
}
private void removeItem(ArrayList<Item> cargohold){
System.out.print("Name of item to Remove: ");
String item = input.nextLine();
for(Item x: cargohold){
if (x.getName().equals(item))
{
cargohold.remove(x);
break;
}
}
}
private void filterItem(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));
}
System.out.println("Item NOT found");
}
private void searchItem(ArrayList<Item> cargohold)
{
int itemfound = 0;
System.out.print("Search items: ");
String item = input.nextLine();
for (Item x : cargohold) {
if (x.getName().equalsIgnoreCase(item))
{
System.out.println(x.getName() + " found");
itemfound = 1;
break;
}
}
if(itemfound == 0)
{
System.out.println("Item NOT found");
}
}
private void searchByWeight(ArrayList<Item> cargohold) {
int itemfound = 0;
System.out.print("Provide weight: ");
int weight = Integer.parseInt(input.nextLine());
for (Item x : cargohold) {
if (x.getWeight()==weight)
{
System.out.println(x.getName() + " found to have this weight");
itemfound = 1;
//multiple items can be found in this case, so don't break
}
}
if(itemfound == 0){
System.out.println("Item NOT found");
}
}
private void searchByCategory(ArrayList<Item> cargohold) {
int itemfound = 0;
System.out.print("Provide weight: ");
String category = input.nextLine();
for (Item x : cargohold) {
if (x.getCategory().equalsIgnoreCase(category))
{
System.out.println(x.getName() + " found to have this category");
itemfound = 1;
}
}
if(itemfound == 0)
{
System.out.println("Item NOT found");
}
}
private void searchByDurability(ArrayList<Item> cargohold)
{
int itemfound = 0;
System.out.print("Provide durability: ");
String durability = input.nextLine();
for (Item x : cargohold) {
if (x.getDurability().equalsIgnoreCase(durability))
{
System.out.println(x.getName() + " found to have this durability");
itemfound = 1;
//multiple items can be found in this case, so don't break
}
}
if(itemfound == 0)
{
System.out.println("Item NOT found");
}
}
private void searchByValue(ArrayList<Item> cargohold)
{
int itemfound = 0;
System.out.print("Provide durability: ");
String value = input.nextLine();
for (Item x : cargohold) {
if (x.getValue().equalsIgnoreCase(value))
{
System.out.println(x.getName() + " found to have this value");
itemfound = 1;
//multiple items can be found in this case, so don't break
}
}
if(itemfound == 0){
System.out.println("Item NOT found");
}
}
private void searchByID(ArrayList<Item> cargohold) {
int itemfound = 0;
System.out.print("Provide durability: ");
String Id = input.nextLine();
for (Item x : cargohold) {
if (x.getID().equalsIgnoreCase(Id))
{
System.out.println(x.getName() + " found to have this ID");
itemfound = 1;
break;
}
}
if(itemfound == 0){
System.out.println("Item NOT found");
}
}
private void searchByAttribute (ArrayList<Item> cargohold){
System.out.println("What the is the item attribute?");
String attribute=input.nextLine();
if(("name").equalsIgnoreCase(attribute))
searchItem(cargohold);
else if(("weight").equalsIgnoreCase(attribute))
searchByWeight(cargohold);
else if(("category").equalsIgnoreCase(attribute))
searchByCategory(cargohold);
else if(("durability").equalsIgnoreCase(attribute))
searchByDurability(cargohold);
else if(("value").equalsIgnoreCase(attribute))
searchByValue(cargohold);
else
searchByID(cargohold); //In default case we search by ID
}
//Displayelements in the list
private void displayItem(ArrayList<Item> cargohold)
{
for (Item x : cargohold)
{
System.out.println("Item ID " + x.getID() + " Item Name " + x.getName());
}
}
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("The items are not found");
}
}
Consumable.java
//Declare the class Consumable
public class Consumable extends Item
{
//Declare the local variables
String Color;
public boolean food;
public boolean water;
public boolean healthy;
//Constructor
public Consumable()
{
}
// Create an overridden constructor here
public Consumable(boolean food, boolean water, boolean healthy)
{
this.food = food;
this.water = water;
this.healthy = healthy;
}
//Implemnt getter and setter methods
public boolean isFood() {
return food;
}
public void setFood(boolean food) {
this.food = food;
}
public boolean isWater() {
return water;
}
public void setWater(boolean water) {
this.water = water;
}
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
public boolean isHealthy() {
return healthy;
}
public void setHealthy(boolean healthy) {
this.healthy = healthy;
}
public void Name()
{
super.getName();
}
public void ID()
{
super.getID();
}
}
Equipable.java
//Declare the class Equipable
public class Equipable extends Item
{
//declare the local variables
private String Color;
boolean outfit;
private int Size;
//Implement the getter and setter methods
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
public boolean isoutfit() {
return outfit;
}
public void setoutfit(boolean outfit) {
this.outfit = outfit;
}
public int getSize() {
return Size;
}
public void setSize(int Size) {
this.Size = Size;
}
}
Weapon.java
//Declare the class Weapon
public class Weapon extends Item {
//Declare the local variables
String Color;
public boolean projectile;
//constructor
public Weapon() {
}
// Create an overridden constructor here
public Weapon(boolean projectile) {
this.projectile = projectile;
}
//getter and setter methods
public boolean isProjectile() {
return projectile;
}
public void setProjectile(boolean projectile) {
this.projectile = projectile;
}
public String getColor() {
return Color;
}
public void setColor(String Color) {
this.Color = Color;
}
//from super
void Name(){
super.getName();
}
void ID(){
super.getID();
}
}
Item.java
//Declare the class
public class Item
{
//declare the variables
String name;
String ID;
String category;
double weight;
String value;
String durability;
//getter and setter methods
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public String getID()
{
return this.ID;
}
public void setID(String ID)
{
this.ID = ID;
}
public double getWeight()
{
return this.weight;
}
public void setWeight(Double weight)
{
this.weight = weight;
}
public String getValue(){
return this.value;
}
public void setValue(String value)
{
this.value = value;
}
public String getDurability()
{
return durability;
}
public void setDurability(String durability){
this.durability = durability;
}
//Create accessors and mutators for your triats.
public void setCategory(String category) {
this.category = category;
}
public String getCategory() {
return this.category;
}
}
Output
In JAVA While out on a standard run we stop in at our ‘local’ fence and...
Each stop at a space station usually means that we have to unload and reload our cargo bay. Each time we arrive at a station, we must unload our cargo into the station hold for inspection and reload it back into our own ship’s cargo hold when we wish to leave. This is becoming very tedious and taking away time that we could be plundering in space. We head into the station central to research a way to eliminate this...
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...
Aristotle makes the claim to achieve happiness in our soul we must find contentment in our daily living. We should aim for the “mean” in our lives. We can achieve this state by not doing things in excess or by putting very little effort into something. To achieve the most happiness, we should aim for the middle. Moral virtue is achieved as the result of being in a “mean” state. Don’t halfway do things in life. On the flip side,...
As nurses it is our job to step up and speak out to always keep improving our roles as healthcare providers. In legislation all the time people who have not ever been nurses themselves are now trying to change rules and improve healthcare with such little knowledge. How can we let those 99 members of the House of Representatives and the 33 Ohio Senators decide what is best for the people working in and around hospitals constantly when they have...
Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new base class. This will be the base for two classes created through inheritance. The Cargo class will need to have virtual functions in order to have them redefined in the child classes. You will be able to use many parts of the new Cargo class in the child classes since they will have the same arguments/parameters and the same functionality. Child class one will...
Language: C# In this assignment we are going to convert weight and height. So, the user will have the ability to convert either weight or height and as many times as they want. There conversions will only be one way. By that I mean that you will only convert Pounds to Kilograms and Feet and Inches to Centimeters. NOT the other direction (i.e. to Pounds). There will be 3 options that do the conversion, one for each type of loop....
Without plagiarizing describe how you feel about the statement below; When it comes to logistics for my business, I need to have a full-time employee on staff. This job tasking has several tasks that are required at any given time and are used more when we run an event for our customers. The logistics person keeps the field clean and mowed along with moving items around and placing them in the correct location for our customers. This person is also...
in java : Create Java Application you are to create a project using our designated IDE (which you must download to your laptop). Create the code to fulfill the requirements below. Demonstrate as stipulated below. Create a Main Application Class called AddressBookApplication (a counsole application / command line application). It should Contain a Class called Menu that contains the following methods which print out to standard output a corresponding prompt asking for related information which will be used to update...
This program will reinforce our knowledge on basic ‘C’ program control, arrays, and editing and compiling ‘C’ programs. The objective of this assignment is to create a program that tallies a shopping list of items and displays each selected item, item cost, and total cost of all the selected items plus tax. The program will setup a simple sporting goods store with a few items and prices. The user will submit a shopping list in a textfile and submit it...
The most successful innovations tend to come from solutions to problems. Many entrepreneurs get ideas for new products from the needs they have themselves, or from problems they identify when observing or talking to other people. For this assignment, you must generate a list of at least 100 things that really “bug” you. Do not try to solve any of these problems yet – that is not the objective of this exercise. At this point, you are only looking for...