Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 build 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. (need 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).

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.

I keep getting error everytime I compile this program ,what could Ive done that keep causing these errors

// Using the classes from the java.util package with the help of import statement

import java.util.*;

import java.util.function.Predicate;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.time.LocalDate;

import java.time.Month;

// MidtermDriver.java class

public class MidtermDriver {

// Creating an object of class named Scanner from the package java.util.

Scanner input = new Scanner(System.in);

//File name to store Item name

String fileName ="items.txt";

// Main method.

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

// Creating the constructor of the class.

new MidtermDriver();

}

// Constructor of the class

public MidtermDriver() throws ClassNotFoundException, IOException {

// Creating an object of the class named ArrayList.java.

ArrayList cargohold = new ArrayList();

// Displays the message on console window.

System.out.println("Welcome to the BlackStar Cargo Hold interface.");

// Displays the message on console window.

System.out.println("Please select a number from the options below");

// New line on colsole window.

System.out.println("");

// Using the loop to prompt the user to enter the options.

while (true) {

// Displays the message on console window.

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

// Displays the message on console window.

System.out.println("2: Remove an item from the cargo hold.");

// Displays the message on console window.

System.out.println("3: Sort the contents of the cargo hold.");

// Displays the message on console window.

System.out.println("4: Search for an item.");

// Displays the message on console window.

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

// Displays the message on console window.

System.out.println("6: Perform a partial search for an item.");

// Displays the message on console window.

System.out.println("7: Read Items from file.");

// Displays the message on console window.

System.out.println("8: Save Items in file.");

// Displays the message on console window.

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

System.out.println("Enter your choice: ");

// Gets the user input.

int userChoice = input.nextInt();

input.nextLine();

// Using the switch-case and invoking the methods.

switch (userChoice) {

case 1:

// Invoking the method.

addItem(cargohold);

break;

case 2:

// Invoking the method.

removeItem(cargohold);

// Using the break keyword.

break;

case 3:

// Invoking the method.

sortItems(cargohold);

// Using the break keyword.

break;

case 4:

// Invoking the method.

searchItems(cargohold);

// Using the break keyword.

break;

case 5:

// Invoking the method.

displayItems(cargohold);

// Using the break keyword.

break;

case 6:

// Invoking the method.

partialSearch(cargohold);

// Using the break keyword.

break;

case 7:

// Invoking the method.

cargohold = readDataFromFile();

// Using the break keyword.

break;

case 8:

// Invoking the method.

saveDataInFile(cargohold);

// Using the break keyword.

break;

case 0:

// Displays the message on console window.

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

System.exit(0);

}

}

}

//Read items from file

private ArrayList readDataFromFile() throws IOException, ClassNotFoundException{

//FileOutputStream object to read items object from file

FileInputStream fileIn = new FileInputStream(fileName);

//ObjectInputStream object to read object from file

ObjectInputStream ois = new ObjectInputStream(fileIn);

ArrayList cargohold= (ArrayList) ois.readObject();

ois.close();

fileIn.close();

System.out.println("Items are read back into list");

return cargohold;

}

//Write items in file

private void saveDataInFile(ArrayList cargohold) throws IOException, ClassNotFoundException {

// FileOutputStream object to read items object from file

FileOutputStream fout = new FileOutputStream(fileName);

// ObjectOutputStream object to read object from file

ObjectOutputStream fileOutData = new ObjectOutputStream(fout);

fileOutData.writeObject(cargohold);

fileOutData.close();

fout.close();

System.out.println("Items are saved into file : "+fileName);

}

// Declaring the method.

private void addItem(ArrayList cargohold) {

// Declaring the variable of type long.

long weight = 0;

// Declaring the Scanner object and initializing it to null value.

Scanner scanner = null;

// Using if statement.

if (scanner == null) {

// Creating object of Scanner class.

scanner = new Scanner(System.in);

}

// Displays the message on console window.

System.out.println("Enter item type 1. Equipment 2. Consumable 3. Weapon :");

int type = scanner.nextInt();

// Declaring the Item class.

Item item = null;

// Using the if statement.

if (type == 1) {

// Creating an object of Equipable class.

item = new Equipable();

} else if (type == 2) {

// Creating an object of Consumable class.

item = new Consumable();

} else {

// Creating an object of Weapon class.

item = new Weapon();

}

// Using the setter method of Item class.

item.setId(cargohold.size());

// Displays the message on console window.

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

// Using the setter method of Item class.

item.setName(scanner.next());

// Displays the message on console window.

System.out.println("Enter weight in kgs :");

// Using the setter method of Item class.

item.setWeight(scanner.nextLong());

// Displays the message on console window.

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

item.setValue(scanner.nextLong());

// Displays the message on console window.

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

// Using the setter method of Item class.

item.setDurability(scanner.nextInt());

weight += item.getWeight();

if (weight < 25000) {

cargohold.add(item);

} else {

// Displays the message on console window.

System.out.println("can't add item into cargo maximum weight exceeded");

}

}

// Defining the method.

private void removeItem(ArrayList cargohold) {

Scanner scanner = null;

// Using the if-statement.

if (scanner == null) {

// Creating an object of Scanner class.

scanner = new Scanner(System.in);

}

// Displays the message on console window.

System.out.println("Enter Item name to delete");

String name = scanner.next();

Predicate condition = item -> item.getName().contains(name);

cargohold.removeIf(condition);

}

// Defining the method.

private void sortItems(ArrayList cargohold) {

// Creating an object of class.

List sitems = new ArrayList();

// Creating an object of class.

TreeSet set = new TreeSet();

// Using for each loop.

for (Item i : cargohold) {

// Using the method of the class to add elements.

set.add(i.getName());

sitems.add(set.size() - 1, i);

}

// Using for each loop.

for (Item i : sitems) {

// Displays the message on console window.

System.out.println(i.toString());

}

}

@SuppressWarnings("resource")

private void searchItems(ArrayList cargohold) {

// TODO: Search for a user specified item

Scanner scanner = null;

if (scanner == null) {

scanner = new Scanner(System.in);

}

System.out.println("Enter name for item you want to search");

String name = scanner.next();

System.out.println("Enter weight for item you want to search");

long weight = scanner.nextLong();

int flag = 0;

for (Item i : cargohold) {

if (i.getName().equalsIgnoreCase(name) && i.getWeight() == weight) {

System.out.println(i);

flag = 1;

}

}

if (flag == 0) {

System.out.println("Item not exists in the cargo");

}

}

private void displayItems(ArrayList cargohold) {

// Displays the message on console window.

System.out.println("Total number of items in cargo is : " + cargohold.size());

for (Item i : cargohold) {

System.out.println(i.toString());

}

}

private void partialSearch(ArrayList cargohold) {

// Search for an item based on a partial name

Scanner scanner = null;

if (scanner == null) {

scanner = new Scanner(System.in);

}

// Displays the message on console window.

System.out.println("Enter name for item you want to search");

String name = scanner.next();

for (Item i : cargohold) {

if (i.getName().equalsIgnoreCase(name)) {

System.out.println(i.toString());

}

}

}

}

import java.io.Serializable;

// Equipable.java class.

class Equipable extends Item implements Serializable{

// Declaring the variable of type String.

private String equipedBy="test1";

//Declaring the variable of type String.

private String equipedFor="test2";

//Declaring the variable of type String.

private String equipedWhere="test3";

// Defining the method.

public String getEquipedBy() {

// Using the return keyword.

return equipedBy;

}

//Defining the method.

public void setEquipedBy(String equipedBy) {

this.equipedBy = equipedBy;

}

//Defining the method.

public String getEquipedFor() {

// Using the return keyword.

return equipedFor;

}

//Defining the method.

public void setEquipedFor(String equipedFor) {

this.equipedFor = equipedFor;

}

//Defining the method.

public String getEquipedWhere() {

// Using the return keyword.

return equipedWhere;

}

//Defining the method.

public void setEquipedWhere(String equipedWhere) {

this.equipedWhere = equipedWhere;

}

@Override

public String toString() {

// Creating an object of StringBuilder class.

StringBuilder builder = new StringBuilder();

//Invoking the method of the class.

builder.append("Equipable [equipedBy=");

//Invoking the method of the class.

builder.append(equipedBy);

//Invoking the method of the class.

builder.append(", equipedFor=");

//Invoking the method of the class.

builder.append(equipedFor);

//Invoking the method of the class.

builder.append(", equipedWhere=");

//Invoking the method of the class.

builder.append(equipedWhere);

//Invoking the method of the class.

builder.append(", ");

//Invoking the method of the class.

builder.append(super.toString());

//Invoking the method of the class.

builder.append("]");

// Using the return keyword.

return builder.toString();

}

}

import java.io.Serializable;

// Item.java class.

class Item implements Serializable{

// Declaring the variable of type long.

private long id;

//Declaring the variable of type String.

private String name;

//Declaring the variable of type long.

private long weight;

//Declaring the variable of type long.

private long value;

//Declaring the variable of type int.

private int durability;

// Defining the method.

public long getId() {

// Using the return keyword.

return id;

}

//Defining the method.

public void setId(long id) {

this.id = id;

}

//Defining the method.

public String getName() {

// Using the return keyword.

return name;

}

//Defining the method.

public void setName(String name) {

this.name = name;

}

//Defining the method.

public long getWeight() {

// Using the return keyword.

return weight;

}

//Defining the method.

public void setWeight(long weight) {

this.weight = weight;

}

//Defining the method.

public long getValue() {

// Using the return keyword.

return value;

}

//Defining the method.

public void setValue(long value) {

this.value = value;

}

//Defining the method.

public int getDurability() {

// Using the return keyword.

return durability;

}

//Defining the method.

public void setDurability(int durability) {

this.durability = durability;

}

@Override

public String toString() {

// Creating an object of StringBuilder class.

StringBuilder builder = new StringBuilder();

// Invoking the method of the class.

builder.append("Item [id=");

//Invoking the method of the class.

builder.append(id);

//Invoking the method of the class.

builder.append(", name=");

//Invoking the method of the class.

builder.append(name);

//Invoking the method of the class.

builder.append(", weight=");

//Invoking the method of the class.

builder.append(weight);

//Invoking the method of the class.

builder.append(", value=");

//Invoking the method of the class.

builder.append(value);

//Invoking the method of the class.

builder.append(", durability=");

//Invoking the method of the class.

builder.append(durability);

//Invoking the method of the class.

builder.append("]");

// Using the return keyword.

return builder.toString();

}

}

import java.io.Serializable;

// Consumable.java class.

class Consumable extends Item implements Serializable {

// Declaring the variable of type string.

private String consumeBy="test3";

//Declaring the variable of type string.

private String consumeFor="test4";

//Declaring the variable of type string.

private String consumeWhere="test5";

// Defining the method.

public String getConsumeBy() {

// Using the return keyword.

return consumeBy;

}

//Defining the method.

public void setConsumeBy(String consumeBy) {

this.consumeBy = consumeBy;

}

//Defining the method.

public String getConsumeFor() {

// Using the return keyword.

return consumeFor;

}

//Defining the method.

public void setConsumeFor(String consumeFor) {

this.consumeFor = consumeFor;

}

//Defining the method.

public String getConsumeWhere() {

// Using the return keyword.

return consumeWhere;

}

//Defining the method.

public void setConsumeWhere(String consumeWhere) {

this.consumeWhere = consumeWhere;

}

@Override

//Defining the method.

public String toString() {

// Creating an object of StringBuilder class.

StringBuilder builder = new StringBuilder();

//Invoking the method of the class.

builder.append("Consumable [consumeBy=");

//Invoking the method of the class.

builder.append(consumeBy);

//Invoking the method of the class.

builder.append(", consumeFor=");

//Invoking the method of the class.

builder.append(consumeFor);

//Invoking the method of the class.

builder.append(", consumeWhere=");

//Invoking the method of the class.

builder.append(consumeWhere);

//Invoking the method of the class.

builder.append(",");

//Invoking the method of the class.

builder.append(super.toString());

//Invoking the method of the class.

builder.append("]");

//Using the return keyword.

return builder.toString();

}

}

import java.io.Serializable;

// Weapon.java class.

class Weapon extends Item implements Serializable{

//Declaring the variable of type string.

private String usedBy="test6";

//Declaring the variable of type string.

private String usedWhere="test7";

//Declaring the variable of type string.

private String precautions="test8";

//Defining the method.

public String getUsedBy() {

//Using the return keyword.

return usedBy;

}

//Defining the method.

public void setUsedBy(String usedBy) {

this.usedBy = usedBy;

}

//Defining the method.

public String getUsedWhere() {

//Using the return keyword.

return usedWhere;

}

//Defining the method.

public void setUsedWhere(String usedWhere) {

this.usedWhere = usedWhere;

}

//Defining the method.

public String getPrecautions() {

//Using the return keyword.

return precautions;

}

//Defining the method.

public void setPrecautions(String precautions) {

this.precautions = precautions;

}

@Override

public String toString() {

// Creating an object of StringBuilder class.

StringBuilder builder = new StringBuilder();

//Invoking the method of the class.

builder.append("Weapon [usedBy=");

//Invoking the method of the class.

builder.append(usedBy);

//Invoking the method of the class.

builder.append(", usedWhere=");

//Invoking the method of the class.

builder.append(usedWhere);

//Invoking the method of the class.

builder.append(", precautions=");

//Invoking the method of the class.

builder.append(precautions);

//Invoking the method of the class.

builder.append(", ");

//Invoking the method of the class.

builder.append(super.toString());

//Invoking the method of the class.

builder.append("]");

// Using the return keyword.

return builder.toString();

}

//Read items from file

private ArrayList readDataFromFile() throws IOException, ClassNotFoundException{

//FileOutputStream object to read items object from file

FileInputStream fileIn = new FileInputStream(fileName);

//ObjectInputStream object to read object from file

ObjectInputStream ois = new ObjectInputStream(fileIn);

ArrayList cargohold= (ArrayList) ois.readObject();

ois.close();

fileIn.close();

System.out.println("Items are read back into list");

return cargohold;

}

//Write items in file

private void saveDataInFile(ArrayList cargohold) throws IOException, ClassNotFoundException {

// FileOutputStream object to read items object from file

FileOutputStream fout = new FileOutputStream(fileName);

// ObjectOutputStream object to read object from file

ObjectOutputStream fileOutData = new ObjectOutputStream(fout);

fileOutData.writeObject(cargohold);

fileOutData.close();

fout.close();

System.out.println("Items are saved into file : "+fileName);

}

}

I keep getting errors when I complie this program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

The changing marketing landscape

Answered: 1 week ago