Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.  * Manage the stock in a business.
  5.  * The stock is described by zero or more Products.
  6.  *
  7.  * @author (your name)
  8.  * @version (a version number or a date)
  9.  */
  10. public class StockManager
  11. {
  12.     // A list of the products.
  13.     private ArrayList<Product> stock;
  14.  
  15.     /**
  16.      * Initialise the stock manager.
  17.      */
  18.     public StockManager()
  19.     {
  20.         stock = new ArrayList<Product>();
  21.     }
  22.  
  23.     /**
  24.      * Add a product to the list.
  25.      * @param item The item to be added.
  26.      */
  27.     public void addProduct(Product item)
  28.     {   int id = item.getID();
  29.         int stockSize = stock.size();
  30.        
  31.         System.out.println("item.getID " + id);
  32.         Product product = findProduct(id);
  33.        
  34.         if(stockSize==0 || product == null){
  35.             stock.add(item);
  36.         }
  37.        
  38.        else if(product.getID() != item.getID()){
  39.         stock.add(item);
  40.     }
  41.     else
  42.         System.out.println("there is a product with this ID or something else gone wrong");
  43.     }
  44.    
  45.     /**
  46.      * Receive a delivery of a particular product.
  47.      * Increase the quantity of the product by the given amount.
  48.      * @param id The ID of the product.
  49.      * @param amount The amount to increase the quantity by.
  50.      */
  51.     public void delivery(int id, int amount)
  52.     {
  53.         if(findProduct(id)!=null){
  54.            Product product = findProduct(id);
  55.            product.increaseQuantity(amount);
  56.            System.out.println("Quantity increased by " +amount);
  57.         }
  58.         else{
  59.             System.out.println("Something is wrong?");
  60.         }
  61.     }
  62.    
  63.     /**
  64.      * Try to find a product in the stock with the given id.
  65.      * @return The identified product, or null if there is none
  66.      *         with a matching ID.
  67.      */
  68.     public Product findProduct(int id)
  69.     {
  70.        for(Product product : stock) {
  71.              
  72.              if(product.getID()==id){
  73.                  System.out.println(product.toString());
  74.                  return product;
  75.         }
  76.         }    
  77.     return null;
  78.     }
  79.    
  80.    
  81.     public Product findProduct(String name)
  82.     {
  83.         int index = 0;
  84.         boolean found = false;
  85.         int stockSize = stock.size();
  86.        
  87.         if(stockSize==0){
  88.             //System.out.println("There is nothing to find here");
  89.             return null;
  90.         }
  91.        
  92.        
  93.         Product stockItem = stock.get(index);
  94.        
  95.         for(; index < stockSize && !found; index++) {
  96.             stockItem = stock.get(index);
  97.             if(stockItem.getName().equals(name)) {
  98.             found = true;
  99.             //System.out.println("Stock found: " + index + " " + stockItem.getName());
  100.         }
  101.     }
  102.    
  103.     if(!found){
  104.         return null;
  105.     }
  106.     return stockItem;
  107.     }
  108.    
  109.     /**
  110.      * Locate a product with the given ID, and return how
  111.      * many of this item are in stock. If the ID does not
  112.      * match any product, return zero.
  113.      * @param id The ID of the product.
  114.      * @return The quantity of the given product in stock.
  115.      */
  116.     public int numberInStock(int id)
  117.     {  
  118.         Product product = null;
  119.         int quantity = 0;
  120.        
  121.          if(findProduct(id)!=null){
  122.             product  = findProduct(id);
  123.             quantity = product.getQuantity();
  124.             System.out.println(quantity);
  125.         }
  126.         else{
  127.             return 0;
  128.         }
  129.         return quantity;
  130.     }
  131.  
  132.     /**
  133.      * Print details of all the products.
  134.      */
  135.     public void printProductDetails()
  136.     {      
  137.             for(Product product : stock) {
  138.             System.out.println(product.toString());
  139.         }
  140.     }
  141.    
  142.     public void printLowStockProducts(int upperLimit){
  143.          for(Product product : stock) {
  144.              
  145.              if(product.getQuantity()<upperLimit){
  146.                  System.out.println(product.toString());
  147.         }
  148.         }        
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement