Question

Each stop at a space station usually means that we have to unload and reload our...

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 problem. We meet a fellow space pirate who has found a solution to the problem we shared. We sit back and listen as he tells us how to find a workaround.

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.

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)

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).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) PRIMARY BASE CODE

import java.io.BufferedReader;

import java.io.File;

import java.io.BufferedWriter;

import java.util.*;

import java.io.FileOutputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.UnsupportedEncodingException;

import java.io.IOException;

import java.io.PrintWriter;

import java.io.OutputStreamWriter;

import java.io.Writer;

public class Assignment9

{

public static void main(String[] args) throws IOException{

new Assignment9();

}

public Assignment9() throws IOException

{

Scanner input = new Scanner(System.in);  

Item temp = new Item();  

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

File file=new File("fileItems.txt");

boolean fvar=false;

try{

fvar=file.createNewFile(); //creates the file if it does not exist

}

catch(Exception e){

e.printStackTrace();

}

while(true){

System.out.println("1:To add item to the cargo hold.");

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

System.out.println("3:Remove cargo hold item.");

System.out.println("4:Sort cargo hold contents.");

System.out.println("5:For Item Search.");

System.out.println("6:Search for an item by attribute.");

System.out.println("7:to Display cargo hold items.");

System.out.println("8:To Perform a partial search.");

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

int userChoice = input.nextInt();

//create the file to store the items  

switch(userChoice){

case 1:

addItemB(cargohold, temp);

break;

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

switch(userChoice){

case 1:

temp = new Equipable();

addItem(cargohold, temp);

break;

case 2:

temp = new Consumable();

addItem(cargohold, temp);

break;

case 3:

temp = new Weapon();

addItem(cargohold, temp);

break;

}

break;

case 3:

removeItem(cargohold);

break;

case 4:

filterItem(cargohold);

break;

case 5:

searchItem(cargohold);

break;

case 6:

searchByAttribute(cargohold);

break;

case 7:

displayItem(cargohold);

break;

case 8:

partialSearch(cargohold);

break;

case 0:

System.out.println("Thank you for using..... See you again!");

System.exit(0);

}

if(changeInFile(cargohold, "fileItems.txt")){

saveToFile("fileItems.txt",cargohold);

}

System.out.println();

}

}

  

private boolean changeInFile(ArrayList<Item> cargohold, String filename) throws IOException{

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;

}

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");

}

writer.close();

}

private void addItemB(ArrayList<Item> cargohold, Item temp) {

Scanner input = new Scanner(System.in);

if(temp instanceof Item){

System.out.print("Enter item name: ");

temp.setName(input.nextLine());

System.out.print("Enter item ID: ");

temp.setID(input.nextLine());

}

double getWeight = 0;

System.out.print("Enter weight of the Item: ");

getWeight = input.nextDouble();

if(getWeight > 25)

{

System.out.println("Weight Exceed");

return;

}

else

{

System.out.println("Enter value of item : ");

temp.setValue(input.nextLine());

}

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

temp.setDurability(input.nextLine());

  

cargohold.add(temp);

}  

  

private void addItem(ArrayList<Item> cargohold, Item temp){

Scanner input = new Scanner(System.in);

if(temp instanceof Equipable){

  

System.out.print("Enter equipable item ID: ");

temp.setID(input.nextLine());

  

System.out.print("Enter equipable attribute name: ");

temp.setName(input.nextLine());

  

System.out.print("Enter equipable attribute color: ");

((Equipable)temp).setColor(input.nextLine());

  

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

  

System.out.print("Enter the consumable name: ");

((Consumable)temp).setName(input.nextLine());

  

System.out.print("Enter the consumable color: ");

((Consumable)temp).setColor(input.nextLine());

  

System.out.print("Is the consumable a food (Y/N): ");

String Food = input.nextLine();

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

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

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

  

  

System.out.print("What is the weapon name: ");

((Weapon)temp).setName(input.nextLine());

  

System.out.print("Enter color fungus : ");

((Weapon)temp).setColor(input.nextLine());

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

Scanner input = new Scanner(System.in);

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

  

Scanner input = new Scanner(System.in);

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

  

Scanner input = new Scanner(System.in);

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

  

Scanner input = new Scanner(System.in);

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;

//multiple items can be found in this case, so don't break

}

}

if(itemfound == 0){

System.out.println("Item NOT found");

}

}

private void searchByDurability(ArrayList<Item> cargohold) {

  

Scanner input = new Scanner(System.in);

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

  

Scanner input = new Scanner(System.in);

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

  

Scanner input = new Scanner(System.in);

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

Scanner input = new Scanner(System.in);

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

  

}

  

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: ");

Scanner input = new Scanner(System.in);

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");

}

}

(2 ITEM CLASS

public class Item_3

{

String name;

String ID;

String category;

Double weight;

String value;

String durability;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

  

public String getID() {

return ID;

}

public void setID(String ID) {

this.ID = ID;

}

  

public Double getWeight(){

return weight;

}

  

public void setWeight(Double weight){

this.weight = weight;

}

  

public String getValue(){

return 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 category;

}

  

}

  

(3 EQUIPABLE SUBCLASS

public class Equipable extends Item

{

// instance variables - replace the example below with your own

private String Color;

boolean outfit;

private int Size;

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;

}

}

(4 CONSUMABLE SUBCLASS

public class Consumable extends Item

{

  

String Color;

public boolean food;

public boolean water;

public boolean healthy;

public Consumable() {

}

// Create an overridden constructor here

public Consumable(boolean food, boolean water, boolean healthy) {

this.food = food;

this.water = water;

this.healthy = healthy;

}

  

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;

}

void Name(){

super.getName();

}

void ID(){

super.getID();

}

}

(4 WEAPON SUBCLASS

public class Weapon extends Item {

String Color;

public boolean projectile;

public Weapon() {

}

// Create an overridden constructor here

public Weapon(boolean projectile) {

  

this.projectile = projectile;

}

  

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

}

}

Add a comment
Know the answer?
Add Answer to:
Each stop at a space station usually means that we have to unload and reload our...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In JAVA While out on a standard run we stop in at our ‘local’ fence and...

    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...

  • 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...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • Program Challenge 10 Design a Ship class that the following members: ·         A field for the...

    Program Challenge 10 Design a Ship class that the following members: ·         A field for the name of the ship (a string). ·         A field for the year that the ship was built (a string). ·         A constructor and appropriate accessors and mutators. ·         A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: ·         A field for the...

  • Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new...

    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...

  • DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress,...

    DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress, Cart and Main. ITEM CLASS Your Item class should contain: Attributes (protected) String name - name of the Item double price - price of the item Methods (public) void setName(String n) - sets the name of Item void setPrice(double p) - sets the price of Item String getName() - retrieves name double getPrice() - retrieves price String formattedOutput() returns a string containing detail of...

  • "Each day before leaving our homes, we protect the property within. By locking our doors, closing...

    "Each day before leaving our homes, we protect the property within. By locking our doors, closing our windows, or activating our security systems, we go to great lengths to ensure that our homes have the necessary safeguards in place to thwart potential intruders and those who may try to steal our personal and precious belongings. When it comes to our confidential personal information, however, many of us fail to realize that this information is readily available and able to be...

  • Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...

    Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be used to compare two objects to see if they are equal. For our purposes, two objects are equal if their abbreviation and uldid are the same. You’ll need to make your overloaded operator a friend of the Cargo class. Create a unit1 object and load it with this data: uld – Pallet abbreviation – PAG uldid – PAG32597IB aircraft - 737 weight – 3321...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...

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
ADVERTISEMENT