Question
This is StockManger Class: import java.util.ArrayList; public class StockManager { // A list of the products. private ArrayList stock; /** * Initialise the stock manager.
This is StockManger Class:
import java.util.ArrayList;
public class StockManager { // A list of the products. private ArrayList stock;
/** * Initialise the stock manager. */ public StockManager() { stock = new ArrayList(); }
/** * Add a product to the list. * @param item The item to be added. */ public void addProduct(Product item) { stock.add(item); } /** * Receive a delivery of a particular product. * Increase the quantity of the product by the given amount. * @param id The ID of the product. * @param amount The amount to increase the quantity by. */ public void delivery(int id, int amount) { } /** * Try to find a product in the stock with the given id. * @return The identified product, or null if there is none * with a matching ID. */ public Product findProduct(int id) { return null; } /** * Locate a product with the given ID, and return how * many of this item are in stock. If the ID does not * match any product, return zero. * @param id The ID of the product. * @return The quantity of the given product in stock. */ public int numberInStock(int id) { return 0; }
/** * Print details of all the products. */ public void printProductDetails() { } }
This is Product Class:
public class Product { // An identifying number for this product. private int id; // The name of this product. private String name; // The quantity of this product in stock. private int quantity;
/** * Constructor for objects of class Product. * The initial stock quantity is zero. * @param id The product's identifying number. * @param name The product's name. */ public Product(int id, String name) { this.id = id; this.name = name; quantity = 0; }
/** * @return The product's id. */ public int getID() { return id; }
/** * @return The product's name. */ public String getName() { return name; }
/** * @return The quantity in stock. */ public int getQuantity() { return quantity; }
/** * @return The id, name and quantity in stock. */ public String toString() { return id + ": " + name + " stock level: " + quantity; }
/** * Restock with the given amount of this product. * The current quantity is incremented by the given amount. * @param amount The number of new items added to the stock. * This must be greater than zero. */ public void increaseQuantity(int amount) { if(amount > 0) { quantity += amount; } else { System.out.println("Attempt to restock " + name + " with a non-positive amount: " + amount); } }
/** * Sell one of these products. * An error is reported if there appears to be no stock. */ public void sellOne() { if(quantity > 0) { quantity--; } else { System.out.println( "Attempt to sell an out of stock item: " + name); } } }
This is StockDemo Class:
/** * Demonstrate the StockManager and Product classes. * The demonstration becomes properly functional as * the StockManager class is completed. * * @author David J. Barnes and Michael Klling. * @version 2016.02.29 */ public class StockDemo { // The stock manager. private StockManager manager;
/** * Create a StockManager and populate it with a few * sample products. */ public StockDemo() { manager = new StockManager(); manager.addProduct(new Product(132, "Clock Radio")); manager.addProduct(new Product(37, "Mobile Phone")); manager.addProduct(new Product(23, "Microwave Oven")); } /** * Provide a very simple demonstration of how a StockManager * might be used. Details of one product are shown, the * product is restocked, and then the details are shown again. */ public void demo() { // Show details of all of the products. manager.printProductDetails(); // Take delivery of 5 items of one of the products. manager.delivery(132, 5); manager.printProductDetails(); } /** * Show details of the given product. If found, * its name and stock quantity will be shown. * @param id The ID of the product to look for. */ public void showDetails(int id) { Product product = getProduct(id); if(product != null) { System.out.println(product.toString()); } } /** * Sell one of the given item. * Show the before and after status of the product. * @param id The ID of the product being sold. */ public void sellProduct(int id) { Product product = getProduct(id); if(product != null) { showDetails(id); product.sellOne(); showDetails(id); } } /** * Get the product with the given id from the manager. * An error message is printed if there is no match. * @param id The ID of the product. * @return The Product, or null if no matching one is found. */ public Product getProduct(int id) { Product product = manager.findProduct(id); if(product == null) { System.out.println("Product with ID: " + id + " is not recognised."); } return product; }
/** * @return The stock manager. */ public StockManager getManager() { return manager; } }
Copy BlueJ project "products" to a new folder named products_yourname_ID. Submit Assign2-products-yourname-ID.zip by February 26, 11:59PM. A company records stock levels of the products it sells. A StockManager object maintains an arbitrary-length list of Product objects. Your task is to complete the outline implementation of the Stock Manger class. The StockDemo class has been provided to help demonstrate ways in which StockManager and Product objects might be used. You can create a StockDemo object on the object bench and call its demo method. As you develop the Stock Manager class, this demo should demonstrate increasing functionality. The Stock Manager Class The Stock Manager class uses a LinkedList object to store zero or more Product items. Its addProduct method adds a new product to the collection. The following methods need completing: delivery, find Product, printProduct Details, and numberInStock. The delivery method should find the Product with the given ID in the list of products and then call its increase Quantity method. The find Product method should look through the collection for a product whose id field matches the id argument of this method. If a matching product is found, that Product should be returned as the method's result. If no matching product is found, return null from the method. The printProductDetails method should iterate over the list of products and print the result of calling the toString() method on each. The numberInStock method should locate a product in the collection with a matching ID, and return the current quantity of that product as a method result. If no product with a matching ID is found, return zero. The Product Class This class has been provided for you, and you should not need to make any alterations to it. Each product sold by the company is represented by an instance of the Product class, which records a product's ID, name and how many of that product are currently in stock. The Product class defines the increase Quantity method to record increases in the stock level of that product. The sellOne method records that one item of that product has been sold by reducing the quantity field level by one. Staged Implementation The overall task has been broken down into suggested separate stages to help you create the finished version in small steps. You are recommended to compile and run the program after each stage to check that the changes you have made are correct. 1. Implement the printProductDetails method to ensure that you are able to iterate over the collection of Products. Just print out each product using System.out. Using an Iterator is the preferred approach, but use an integer index variable if you find that easier to understand. 2. Implement the findProduct method. This differs from the printProduct Details method in that it will not necessarily have to examine every product in the collection before a match is found. For instance, if the first product in the collection matches the product name, iteration can finish and that first Product returned. On the other hand, it is possible that there might be no match for the name in the collection. In that case, the whole collection will be examined, without finding a product to return. In this case the null value should be returned. 3. Implement the numberInStock method. This is relatively simple to implement once the find Product method has been completed. For instance, numberInStock can call the findProduct method to do the searching, and then call the getQuantity method on the result. Watch out for products that cannot be found, though. 4. Implement the delivery method using a similar approach to that used in numberInStock. 5 Implement a method in Stock Manager to print details of all products with stock levels below a given value (passed as a parameter to the method). MUST be implemented in functional style (using lambdas and streams). 6 Modify the addProduct method so that a new product cannot be added to the product list with the same ID as an existing one. 7 Add to StockManager a method that finds a product from its name rather than its ID: public Product findProduct(String name) In order to do this, you need to know that two String objects sl and s2 can be tested for equality with the Boolean expression: s1.equals(s2)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started