Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have posted the problem with the class files, about the Item class, I have filled it but I'm not sure if it's right or

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

I have posted the problem with the class files, about the Item class, I have filled it but I'm not sure if it's right or not.. There are 3 files, Item, Inventory and InventoryManager

please use java.

Thank you

import java.util.Random; public class Item {

/* * The Random instance to be used to acquire random values. You must use * this to get Random values if you want any credit! */ private static Random random = new Random();

// A String instance variable called "name" private String name;

double price; // TODO : Add a double instance variable called "price"

double weight; // TODO : Add a double instance variable called "weight"

// Create an Item called "Random item" with a random price and weight. public Item() { name = "Random item"; double price = 10;// TODO : Use one call to random to set price to a random double double weight = 100; // TODO : Use one call to random to set weight to a random double // Both these doubles should have a value in the range [10, 100) }

// Create an Item with the given name, price and weight. public Item(String name, double price, double weight) { this.name = name; this.price = price; this.weight = weight; }

// Get the item name. public String getName() { return name; }

// Get the item weight. public double getWeight() { return weight; }

// Get the item price. public double getPrice() { return price; }

}

Problem Description You have decided you want to create an Inventory management system, which can keep track of items, their weights, and their prices. Solution Descriptionn We have provided you with the skeletons of three classes: Item, Inventory and Inventory Manager. Your task is to fill them in so that they meet the following requirements: em You will create two instance fields in Item: - A double field named "weight A double field named "price". .In addition, you will fill in the first constructor. Just fill in the TODO lines provided. Inventory You will create an Item[] field in Inventory called contents You will write the merge method. This works as follows: - You are given another inventory. You can access its contents with other.contents You need to first figure out how big the merged array should be. You must add together the lengths of contents and other.contents You need to create a new Item whose size is the sum we computed above. -You need to copy all of contents into that Item created above - You need to copy all of other.contents into that Item created above. You need to write the getItem method. This wor ks as follows: First, check if the given index is valid. (Think about when an array index is valid versus invalid.) If it is invalid, return null Otherwise, if it is valid, return the value in contents at the given index. You need to write the putItem method. This works as follows: First, check if the given index is valid. (Think about when an array index is valid versus invalid.) If it is invalid, return false Otherwise, if it is valid, set the value in contents at the given index to item and return true. .You need to write the getContentsLength method, which just returns the length of contents InventoryManager You will fill in a mergeInventories method, which just takes in two Inventorys and merges them using the met hods you wrote above. You will fill in a addItem method, which just takes in an Inventory to put the item into, an Item, to put in the Inventory, and an index for where in the Inventory to put the Item. If it doesn't work print "Can't add there.". You will fill in a printItem method, which takes in an Item and prints (name, price, weight), where name, price and weight are filled in with the proper values from Item. price and weight should be printed to two decimal points. If the Item is null, print (empty, 0, 0). Do not print a new-line after is You will fill in a showInventory method. If the Inventory is null, or if getContentsLength returns a value less than or equal to zero, just print . Otherwise, print a comma separated list of the Items in the Inventory's contents array. An example is given below: (A, 0.0, 0.0), (B, 1.0, 0.0), (empty, o, o), (empty, 0, 0), Your formatting (spaces, no extra commas, etc.) must match this exactly. Print a new-line after your list or after the "O". Allowed Imports To prevent trivialization of the assignment, you are only allowed to import the following classes: java.util.Scanner java.util.Random If you would like to import anything else, ask on Piazza. Based on what we have seen in the past, we'll go ahead and say you cannot import the following classes: java.util.Arrays java.util.ArrayList Or any List Feature Restrictions There are a few feat ures and methods in Java that overly simplify the concepts we are trying to teach. For that reason, do not use any of the following in your final submission: var (the reserved keyword) System.arraycopy In addition, you may not use System.exit as it will break the autograder. import java.util.Random; public class Item The Random instance to be used to acquire random values. You must use * this to get Random values if you want any credit! private static Random randomnew Random(); // A String instance variable called "name" private String name; double price; /I TODO Add a double instance variable called "price" double weight; // TODO : Add a double instance variable called "weight" // Create an Item called "Random item" with a random price and weight. public Item() name"Random item"; double price 10;// TODOUse one call to random to set price to a random double double weight -100; // TODO Use one call to random to set weight to a random double // Both these doubles should have a value in the range [10, 100) // Create an Item with the given name, price and weight. public Item(String name, double price, double weight) f this.name name; this.price -price; this.weight - weight; // Get the item name. public String getName) return name; // Get the item weight. public double getweight) return weight; // Get the item price. public double getPrice) return price; public class Inventory public class Inventory . 1 public class Inventory 2 // TODO :Add an Item[] instance variable called contents. // Initialize it at declaration; give it a default length of 4. 4 // Merge two inventorys, merging other into this inventory. public void merge(Inventory other) t 7 // TODO :Compute contents.length other.contents.length // TODO :Create a new Item[] of the size above // TODO Copy all the elements from contents into the new Item [ // TODO: Copy all the elements from other.contents into the new Item[l // TODO : Set contents to be the new Item[] we just filled 10 12 13 15 // Get the i-th item, where the 0th item is the first. // Return null if the index is invalid. public Item getItem(int i) - inventories.length) Inventory [] inventories new Inventory[inventoriesSize 10]; for (int = 0; inventoriesSize; // jd inventories2 [iinventories [i]) inventories [inventoriesSize++] new Inventory () break; case 2: int c- 0; for (Inventory i inventories) if (i - null) continue; System.out.println("Inventory[" + c++ "]"+" with" + i.getContentsLength) " items"); System.out.println) break; case 3: System.out.print("Enter which inventory to show: "); choice Integer.parseInt (scanner, nextLine()); inventory((choice Il choice inventories. length) ? null inventories [choice]); if (inventorynull) System.out.println("That doesn't exist"); System.out.println); break; showInventory(inventory); break: case 4: System.out.print("Enter the reciever inventory number: "); choiceInteger.parseInt(scanner.nextLine()) Inventory inventory1((choice - inventories.length) showInventory(inventory); System.out.print("Select a slot: "); choice Integer. parseInt ( scanner. nextLine()); System.out.println(); System.out.print("Enter a name: "); String name -scanner.nextLine(); System.out.println); System.out.print ("Enter a price: "); double price - Double.valueOf (scanner.nextLine()); System.out.println(); System.out.print ("Enter a weight: "); double weight- Double.valueOf (scanner.nextLine()); System.out.println); Item itemnew Item(name, price, weight); addItem(inventory, choice, item); break; case 6: System.out.println("Goodbye") return; default: System.out.println("That's not an option: try again."); break

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

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions