SHARE
TWEET

Untitled

mauriciojavacode Feb 18th, 2020 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Shopping;
  2.  
  3. import DataStructures.*;
  4. import java.io.FileNotFoundException;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  * @version Fall 2019
  10.  * @author ITCS 2214
  11.  */
  12. public class ShoppingListArrayList implements ShoppingListADT {
  13.  
  14.     private ArrayList<Grocery> shoppingList;
  15.  
  16.     /**
  17.      * Default constructor of ShoppingArray object.
  18.      */
  19.     public ShoppingListArrayList() {
  20.         this.shoppingList = new ArrayList<>();
  21.     }
  22.  
  23.     /**
  24.      * Constructor of ShoppingArray object that parses from a file.
  25.      *
  26.      * @param filename the name of the file to parse
  27.      * @throws FileNotFoundException if an error occurs when parsing the file
  28.      */
  29.     public ShoppingListArrayList(String filename) throws FileNotFoundException {
  30.         this.shoppingList = new ArrayList<>();
  31.         scanFile(filename);
  32.     }
  33.  
  34.     /**
  35.      * Method to add a new entry. Only new entries can be added. Combines
  36.      * quantities if entry already exists.
  37.      *
  38.      * @param entry the entry to be added
  39.      */
  40.     @Override
  41.     public void add(Grocery entry) {
  42.         if (entry == null) {
  43.             return;
  44.         }
  45.        
  46.         // Check if this item already exists
  47.         if (this.contains(entry)) {
  48.             //Merge the quantity of new entry into existing entry
  49.             combineQuantity(entry);
  50.             return;
  51.         }
  52.  
  53.         shoppingList.add(entry);
  54.     }
  55.  
  56.     /**
  57.      * Method to remove an entry.
  58.      *
  59.      * @param ent to be removed
  60.      * @return true when entry was removed
  61.      * @throws DataStructures.ElementNotFoundException
  62.      */
  63.     @Override
  64.     public boolean remove(Grocery ent) {
  65.        
  66.        
  67.         // the boolean found describes whether or not we find the
  68.         // entry to remove
  69.        
  70.         boolean found = false;
  71.  
  72.         // TODO Search the parameter variable ent in the shoppingList; if it is found, please remove it and set the value of `found`. It is okay to directly use methods from the Java Build-in ArrayList class.
  73.        
  74.         for (int i = 0; i < shoppingList.size(); i++) {
  75.             if(shoppingList.get(i) == ent) {
  76.                 shoppingList.remove(i);
  77.                 found = true;
  78.             }
  79.           }
  80.         // Return false if not found        
  81.         return found;
  82.     }
  83.  
  84.     /**
  85.      * Method to find an entry.
  86.      *
  87.      * @param index to find
  88.      * @return the entry if found
  89.      * @throws Exceptions.EmptyCollectionException
  90.      */
  91.     @Override
  92.     public Grocery find(int index) throws IndexOutOfBoundsException,
  93.             EmptyCollectionException {
  94.         if (this.isEmpty()) {
  95.             throw new EmptyCollectionException("ECE - find");
  96.         }
  97.        
  98.         // TODO check whether or not the given index is legal
  99.         // for example, the given index is less than 0 or falls outside of the size.
  100.         // If it is not legal, throw an IndexOutOfBoundsException
  101.        
  102.         if (index >= shoppingList.size()) {
  103.             throw new IndexOutOfBoundsException("IOOBE - find");
  104.         }
  105.        
  106.         if (index < 0) {
  107.             throw new IndexOutOfBoundsException("IOOBE - find");
  108.         }        
  109.          
  110.         // return the corresponding entry in the shoppingList
  111.         // need to change the return value
  112.         // return null;
  113.         return (Grocery)this.shoppingList.get(index);
  114.     }
  115.  
  116.     /**
  117.      * Method to locate the index of an entry.
  118.      *
  119.      * @param ent to find the index
  120.      * @return the index of the entry
  121.      * @throws ElementNotFoundException if no entry was found
  122.      */
  123.     @Override
  124.     public int indexOf(Grocery ent) throws ElementNotFoundException {
  125.         if (ent != null) {
  126.             for (int i = 0; i < shoppingList.size(); i++) {
  127.                 if (shoppingList.get(i).compareTo(ent) == 0) {
  128.                     return i;
  129.                 }
  130.             }
  131.         }
  132.         throw new ElementNotFoundException("indexOf");
  133.     }
  134.  
  135.     /**
  136.      * Method to determine whether the object contains an entry.
  137.      *
  138.      * @param ent to find
  139.      * @return true if and only if the entry is found
  140.      */
  141.     @Override
  142.     public boolean contains(Grocery ent) {
  143.         boolean hasItem = false;
  144.        
  145.         if (ent != null) {
  146.             // TODO go through the shoppingList and try to find the
  147.             // given item, named ent, in the list. If found, return true. Either using methods from Java build-in ArrayList method or writing your own loop is fine.
  148.             for (int i = 0; i < shoppingList.size(); i++) {
  149.                 if (shoppingList.get(i) != null && shoppingList.get(i).compareTo(ent) == 0) {
  150.                     hasItem = true;
  151.                     break;
  152.                 }
  153.             }
  154.  
  155.         }
  156.         return hasItem;
  157.     }
  158.  
  159.     /**
  160.      * Gets the size of the collection.
  161.      *
  162.      * @return the size of the collection
  163.      */
  164.     @Override
  165.     public int size() {
  166.         return shoppingList.size();
  167.     }
  168.  
  169.     /**
  170.      * Gets whether the collection is empty.
  171.      *
  172.      * @return true if and only if the collection is empty
  173.      */
  174.     @Override
  175.     public boolean isEmpty() {
  176.         return shoppingList.isEmpty();
  177.     }
  178.  
  179.     /**
  180.      * Returns a string representing this object.
  181.      *
  182.      * @return a string representation of this object
  183.      */
  184.     @Override
  185.     public String toString() {
  186.         StringBuilder s = new StringBuilder();
  187.         s.append(String.format("%-25s", "NAME"));
  188.         s.append(String.format("%-18s", "CATEGORY"));
  189.         s.append(String.format("%-10s", "AISLE"));
  190.         s.append(String.format("%-10s", "QUANTITY"));
  191.         s.append(String.format("%-10s", "PRICE"));
  192.         s.append('\n');
  193.         s.append("------------------------------------------------------------"
  194.                 + "-------------");
  195.         s.append('\n');
  196.         for (int i = 0; i < shoppingList.size(); i++) {
  197.             s.append(String.format("%-25s", shoppingList.get(i).getName()));
  198.             s.append(String.format("%-18s", shoppingList.get(i).getCategory()));
  199.             s.append(String.format("%-10s", shoppingList.get(i).getAisle()));
  200.             s.append(String.format("%-10s", shoppingList.get(i).getQuantity()));
  201.             s.append(String.format("%-10s", shoppingList.get(i).getPrice()));
  202.             s.append('\n');
  203.             s.append("--------------------------------------------------------"
  204.                     + "-----------------");
  205.             s.append('\n');
  206.         }
  207.  
  208.         return s.toString();
  209.     }
  210.  
  211.     /**
  212.      * Add the quantity of a duplicate entry into the existing
  213.      *
  214.      * @param entry duplicate
  215.      */
  216.     private void combineQuantity(Grocery entry) {
  217.         try {
  218.             int index = this.indexOf(entry);
  219.             shoppingList.get(index).setQuantity(
  220.                     shoppingList.get(index).getQuantity()
  221.                     + entry.getQuantity());
  222.         } catch (ElementNotFoundException e) {
  223.             System.out.println("combineQuantity - ECE");
  224.         }
  225.  
  226.     }
  227.  
  228.     /**
  229.      * Scans the specified file to add items to the collection.
  230.      *
  231.      * @param filename the name of the file to scan
  232.      * @throws FileNotFoundException if the file is not found
  233.      */
  234.     private void scanFile(String filename) throws FileNotFoundException {
  235.         Scanner scanner = new Scanner(getClass().getResourceAsStream(filename))
  236.                 .useDelimiter("(,|\r\n)");
  237.  
  238.         while (scanner.hasNext()) {
  239.             Grocery temp = new Grocery(scanner.next(), scanner.next(),
  240.                     Integer.parseInt(scanner.next()),
  241.                     Float.parseFloat(scanner.next()),
  242.                     Integer.parseInt(scanner.next()));
  243.            
  244.             add(temp);
  245.         }
  246.     }
  247.  
  248. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top