Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- * Manage the stock in a business.
- * The stock is described by zero or more Products.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class StockManager
- {
- // A list of the products.
- private ArrayList<Product> stock;
- /**
- * Initialise the stock manager.
- */
- public StockManager()
- {
- stock = new ArrayList<Product>();
- }
- /**
- * Add a product to the list.
- * @param item The item to be added.
- */
- public void addProduct(Product item)
- { int id = item.getID();
- int stockSize = stock.size();
- System.out.println("item.getID " + id);
- Product product = findProduct(id);
- if(stockSize==0 || product == null){
- stock.add(item);
- }
- else if(product.getID() != item.getID()){
- stock.add(item);
- }
- else
- System.out.println("there is a product with this ID or something else gone wrong");
- }
- /**
- * 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)
- {
- if(findProduct(id)!=null){
- Product product = findProduct(id);
- product.increaseQuantity(amount);
- System.out.println("Quantity increased by " +amount);
- }
- else{
- System.out.println("Something is wrong?");
- }
- }
- /**
- * 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)
- {
- for(Product product : stock) {
- if(product.getID()==id){
- System.out.println(product.toString());
- return product;
- }
- }
- return null;
- }
- public Product findProduct(String name)
- {
- int index = 0;
- boolean found = false;
- int stockSize = stock.size();
- if(stockSize==0){
- //System.out.println("There is nothing to find here");
- return null;
- }
- Product stockItem = stock.get(index);
- for(; index < stockSize && !found; index++) {
- stockItem = stock.get(index);
- if(stockItem.getName().equals(name)) {
- found = true;
- //System.out.println("Stock found: " + index + " " + stockItem.getName());
- }
- }
- if(!found){
- return null;
- }
- return stockItem;
- }
- /**
- * 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)
- {
- Product product = null;
- int quantity = 0;
- if(findProduct(id)!=null){
- product = findProduct(id);
- quantity = product.getQuantity();
- System.out.println(quantity);
- }
- else{
- return 0;
- }
- return quantity;
- }
- /**
- * Print details of all the products.
- */
- public void printProductDetails()
- {
- for(Product product : stock) {
- System.out.println(product.toString());
- }
- }
- public void printLowStockProducts(int upperLimit){
- for(Product product : stock) {
- if(product.getQuantity()<upperLimit){
- System.out.println(product.toString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement