Locoluis

Example Java Container Class

Aug 18th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class ProductList {
  5.     List<Product> products;
  6.  
  7.     ProductList()
  8.     {
  9.         products = new ArrayList<Product>();
  10.     }
  11.     void add(Product p)
  12.     {
  13.         products.add(p);
  14.     }
  15.     void set(int index, Product p)
  16.     {
  17.         products.set(index, p);
  18.     }
  19.     void clear()
  20.     {
  21.         products.clear();
  22.     }
  23.     void remove(int index)
  24.     {
  25.         products.remove(index);
  26.     }
  27.     void remove(Product p)
  28.     {
  29.         products.remove(p);
  30.     }
  31.     Product get(int index)
  32.     {
  33.         return (Product)products.get(index);
  34.     }
  35.     int getTotalInventory()
  36.     {
  37.         int inventory = 0;
  38.         for(int i = 0; i < products.size(); i++) {
  39.             Product pp = products.get(i);
  40.             if(pp != null) {
  41.                 inventory += pp.getInventory();
  42.             }
  43.         }
  44.         return inventory;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment