Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.60 KB | None | 0 0
  1. package ass1;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import java.util.Arrays;
  6.  
  7. import ass1.GroceryItem;
  8. import ass1.GroceryItem.HEAT;
  9.  
  10. /* NOTES
  11.  *
  12.  * Modelling the contents with GroceryItem[] (an array of GroceryItems) is not
  13.  * the most efficient method.  In practice we might use a different class
  14.  * like ArrayList, which works like an array but can grow/shrink as required,
  15.  * and has members for manipulation of its elements.
  16.  *
  17.  * A plain array is used here in order to teach us how to work with arrays.
  18.  * Please don't use library methods for array processing (like "Arrays" methods);
  19.  * work out how to do it yourself.
  20.  * The sortItems() method supplied for you uses Arrays.sort().  You can use this
  21.  * as-is with your own methods, however for full marks you must replace the call
  22.  * to Arrays.sort() with your own sort routine.
  23.  *
  24.  */
  25.  
  26. public class ShoppingBag {
  27.  
  28.     /**
  29.      * put your student ID here   (replace "bondj007" with yours).
  30.      */
  31.     public static final String STUDENT_ID = "kimdy031";
  32.    
  33.     /**
  34.      * maximum # items a bag should hold  (if it holds more, it will be overfull)
  35.      */
  36.     public static final int MAX_NUM_ITEMS = 6;
  37.     /**
  38.      * maximum total weight a bag should hold  (if it holds more, it will be overloaded)
  39.      */
  40.     public static final double MAX_WEIGHT = 2.0;
  41.  
  42.     private static final GroceryItem[] Max_num_Items = null;
  43.    
  44.     /**
  45.      * Array holding the GroceryItems in this bag.
  46.      * This array is to be maintained at exactly the right length to hold the number of items.
  47.      * i.e. if the bag has 4 items, this array will be length 4.  There will be no null elements.
  48.      * To this end it will be necessary to grow the array to add new items, and to shrink the array
  49.      * when items are deleted.
  50.      * The methods compactContents() and growContents() will be used for this.
  51.      *
  52.      * Elements are ordered from BOTTOM to TOP.
  53.      * The first element, contents[0], is considered to be at the BOTTOM of the bag.
  54.      * The final element, contents[length-1], is at the TOP of the bag.
  55.      */
  56.     private GroceryItem[] contents;
  57.    
  58.  
  59.     // -------------------------------------------------------------------
  60.    
  61.     /**
  62.      * remove any nulls from the contents array, shrinking it if necessary.
  63.      * Example:  if the contents array prior to call is [A null B C null D] (length 6)
  64.      *  then it will be [A B C D] (length 4) after call.
  65.      *  
  66.      * Use this routine in your other methods to remove nulls after/during a deletion operation.
  67.      *
  68.      * BE AWARE there is no JUnit test case supplied for this method.
  69.      */
  70.     private void compactContents() {
  71.        
  72.         int nullCount = 0;  // make a new variable to count
  73.        
  74.         //this loop  is to count the number of null elements in the original list
  75.         for (int i = 0; i < contents.length; i++) {
  76.             if (contents[i].equals(null)) { // if elements in contents array has null
  77.                 nullCount++; // increase the nullCount
  78.             }
  79.         GroceryItem[] newItem = new GroceryItem[contents.length - nullCount]; // create a newlist called newItem,
  80.         // which counts the elements of contents which does not include null
  81.        
  82.        
  83.         int indexNewItem = 0;  
  84.        
  85.         // copy the whole arraylist
  86.         for (int index = 0; index < contents.length; index++) {
  87.             if (!contents[index].equals(null)) { // if the element in the array does not have null  
  88.                 newItem[indexNewItem] = contents[index];  // just copy them into the newItem list.
  89.                 indexNewItem ++;
  90.             }
  91.         }
  92.         contents = newItem; // copy back.
  93.     }
  94.  
  95.     /**
  96.      * grow the contents array by one element, such that the existing elements are not changed,
  97.      * and the new (final) element is null.
  98.      * Example:  If the contents array prior to call is [A B C D] (length 4)
  99.      * then contents after call is [A B C D null]
  100.      *
  101.      * Use this routine in your other methods to make space so new items can be added.
  102.      *
  103.      * BE AWARE there is no JUnit test case supplied for this method.
  104.      */
  105.    
  106.         private void growContents() {
  107.        
  108.     }
  109.    
  110. //  GroceryItem[] gc = new GroceryItem[contents.length+1]; // create a new array list which is increased by one element.
  111. // 
  112. //  int increaseElement = 0;
  113. // 
  114. //  for(int i = 0; i < contents.length; i++) {
  115. //      gc[increaseElement] = contents[i];
  116. //      increaseElement ++;
  117. //  }
  118. //  contents = gc;
  119.     // --------------------------------------------------------
  120.    
  121.     /**
  122.      * construct a new ShoppingBag with an empty contents array
  123.      * (i.e. not null, but an array of no elements)
  124.      */
  125.    
  126.     public ShoppingBag() {
  127.        
  128.         int newShoppingBag [] = new int[0];
  129.  
  130.         // YOUR CODE HERE
  131.     }
  132.    
  133.     /**
  134.      * construct a new ShoppingBag with contents initialised from args[] array.
  135.      * contents of the ShoppingBag must be ordered in the same way as the args are supplied
  136.      * i.e. args[0] on the bottom of the bag, args[length-1] on the top.
  137.      * @param args
  138.      */
  139.     public ShoppingBag(GroceryItem... args) {
  140.         ShoppingBag newShoppingBag = new ShoppingBag(); // ???????????  
  141.        
  142.         }
  143.    
  144.     /**
  145.      * return the number of items in the ShoppingBag.
  146.      * @return the number of items
  147.      */
  148.     public int numItems(){
  149.        
  150.         GroceryItem[] newItem = new GroceryItem[contents.length];
  151.        
  152.         return contents.length;
  153.        
  154. //      int length = contents.length;  
  155. //      return length;
  156. //      return -1;  // REPLACE THIS WITH YOUR CODE
  157.     }
  158.    
  159.     /**
  160.      * determine if the ShoppingBag is empty (has no contents)
  161.      * @return true if empty
  162.      */
  163.     public boolean isEmpty() {
  164.        
  165.         if (contents.length == 0) {
  166.             return true;
  167.         }
  168.         else {  
  169.             return false;
  170.         }
  171.     }
  172.    
  173.     // ------------------------------------------------------------
  174.    
  175.     /**
  176.      * return the total weight of all items in the ShoppingBag.
  177.      * @return total weight
  178.      */
  179.     public double totalWeight() {
  180.        
  181.         return (MAX_WEIGHT * MAX_NUM_ITEMS);    
  182.            // REPLACE THIS WITH YOUR CODE
  183.     }
  184.    
  185.     /**
  186.      * determine the total GST (tax) payable on items in this ShoppingBag
  187.      * @return total GST
  188.      */
  189.     public double totalGST(){
  190.         double totalGST = 0;
  191.         for (int i = 0; i < contents.length; i++) {
  192.             totalGST += contents.getClass(
  193.             }
  194.         }
  195.         return totalGST;
  196.     }
  197.    
  198.     // --------------------------------------------------------------
  199.  
  200.     /** determine if the ShoppingBag's contents exceed the maximum weight
  201.      *  or exceeds the maximum number of items.
  202.      * @return true if overloaded/overfull.
  203.      */
  204.     public boolean isOverloaded(){
  205.         if (contents.length < 6) {
  206.             return false;
  207.            
  208.         }
  209.         else {
  210.             return true; // REPLACE THIS WITH YOUR CODE
  211.         }
  212.         }
  213.    
  214.     /**
  215.      * Determine if a specific item can be placed into this ShoppingBag without
  216.      * exceeding maximum weight or exceeding maximum number of items.
  217.      * @param gi  the GroceryItem to consider
  218.      * @return  true if item will fit without exceeding weight/number limits.
  219.      */
  220.     public boolean canFitItem(int gi){
  221.         for (int i= 0; i < contents.length; i++)
  222.         if (contents.length <= 6 && MAX_WEIGHT < 2 )
  223.             GroceryItem [gi] = contents [i]
  224.             return true;
  225.         else
  226.             return false;  // REPLACE THIS WITH YOUR CODE
  227.     }
  228.    
  229.     /**
  230.      * add an item to the top of the ShoppingBag, regardless of existing contents.
  231.      * @param item the GroceryItem to add.
  232.      */
  233.     public void forceIntoBag(GroceryItem item){  
  234.        
  235.           contents[contents.length-1] = contents[item];  
  236.        
  237.         }
  238.    
  239.     /**
  240.      * add an item to the top of the ShoppingBag ONLY IF it will fit.
  241.      * @param item the GroceryItem to add.
  242.      * @return true if added, false if not.
  243.      */
  244.     public boolean addToBag(GroceryItem item){
  245.         if (contents.length < 6 && MAX_WEIGHT < 2) {
  246.          
  247.             return true;
  248.         }
  249.         else {
  250.         return false;  // REPLACE THIS WITH YOUR CODE
  251.     }
  252.     }
  253.    
  254.     // ------------------------------------------------------------
  255.  
  256.     /**
  257.      * Determine if an item equal to the parameter item occurs in the
  258.      * ShoppingBag's contents and return its index, or -1 if the item
  259.      * is not present.  Use GroceryItem.equals() to compare items.
  260.      * If the designated item occurs more than once in the contents then
  261.      * return the index of the topmost item.
  262.      *
  263.      * NOTE this is private because it refers to an implementation detail:
  264.      * the array index.  We don't want to expose our implementation to the
  265.      * user of the class.
  266.      * However certain other methods in this class could and should use this
  267.      * routine!
  268.      *
  269.      * @param gi  GroceryItem to search for in contents.
  270.      * @return
  271.      */
  272.     private int findIndexOfItem (GroceryItem gi){
  273.         for (int i = 0; i < contents.length; i++) {
  274.             if(contents[i].equals(gi)) {
  275.                 return i;
  276.             }
  277.         }
  278.         return -1;  // REPLACE THIS WITH YOUR CODE
  279.     }
  280.    
  281.     /**
  282.      * Determine if an item having the supplied name occurs in the contents
  283.      * and return its index, or -1 if not present.
  284.      * If several items have the same name then return the topmost.
  285.      *
  286.      * NOTE private for same reasons as above.
  287.      *
  288.      * @param name  name of GroceryItem to search for in contents.
  289.      * @return
  290.      */
  291.     private int findIndexOfName (String name) {
  292.         for (int i = 0; i< contents.length; i++) {
  293.             if(contents[i].getName().equals(name)) {
  294.                 return i;
  295.             }
  296.         }
  297.         return -1;  // REPLACE THIS WITH YOUR CODE
  298.     }
  299.    
  300.     /**
  301.      * Determine if ShoppingBag contains a GroceryItem with same name as supplied parameter
  302.      * @param name the name of GroceryItem to look for
  303.      * @return true if item present, false if not
  304.      */
  305.     public boolean hasItem(String name) {
  306.         for(int i = 0; i < contents.length; i++) {
  307.             if(contents[i].getName().toString().equals(name)) {
  308.                 return true;
  309.             }
  310.         }
  311.         return false;  // REPLACE THIS WITH YOUR CODE
  312.     }
  313.    
  314.     /**
  315.      * Determine if ShoppingBag contains a GroceryItem equal to the supplied parameter
  316.      * @param gi the GroceryItem to search for
  317.      * @return true if item present, false if not.
  318.      */
  319.     public boolean hasItem(GroceryItem gi) {
  320.         for(int i = 0; i< contents.length; i++) {
  321.             if(contents[i].equals(gi)) {
  322.                 return true;
  323.             }
  324.         }
  325.        
  326.         return false;  // REPLACE THIS WITH YOUR CODE
  327.     }
  328.    
  329.     /**
  330.      * Remove the GroceryItem at supplied index from the contents array
  331.      * and return it, or return null if the index is outside array limits.
  332.      * The contents array must be left in the proper state,
  333.      * i.e. correct length and containing no nulls.
  334.      *
  335.      * private because it refers to underlying implementation (array)
  336.      *
  337.      * @param ix  index of item to return
  338.      * @return  the item which was at position ix.
  339.      */
  340.     private GroceryItem removeFromBag(int ix) {
  341.         if((ix < contents.length && ix >= 0)) {
  342.             GroceryItem newItem = contents[ix];
  343.             contents[ix] = contents[contents.length - 1];
  344.             contents[contents.length - 1] = null;
  345.             compactContents();
  346.             return newItem;
  347.         }
  348.         return null;
  349.           // REPLACE THIS WITH YOUR CODE
  350.     }
  351.    
  352.     /**
  353.      * Remove a GroceryItem from the bag which is the same as the supplied
  354.      * parameter, and return it.  Return null if no matching item was found
  355.      * in the ShoppingBag. 
  356.      * The contents array must be maintained in the proper state.
  357.      * @param gi  the GroceryItem to search for in contents
  358.      * @return  the found GroceryItem, or null
  359.      */
  360.     public GroceryItem removeFromBag(GroceryItem gi){
  361.         for (int i = 0; i < contents.length; i++) {
  362.         if (contents[i].equals(gi)) {
  363.             return contents[i];
  364.         }
  365.         }
  366.         return null;
  367.     }
  368.    
  369.     /**
  370.      * Remove a GroceryItem from the bag which has the same name as the
  371.      * supplied String.  Return null if no such item was found.
  372.      * The contents array must be maintained in the proper state.
  373.      * @param name  The name of the item to remove.
  374.      * @return  the removed item.
  375.      */
  376.     public GroceryItem removeFromBag(String name){
  377.         for(int i = 0; i < contents.length; i++) {
  378.             if (!contents[i].equals(name)) {
  379.                 contents.IndexOfName[name] = contents[i];
  380.             }
  381.        
  382.         }
  383.     return null;
  384.     }
  385.    
  386.     // ---------------------------------------------------------
  387.    
  388.     /**
  389.      * Remove the topmost item from the ShoppingBag and return it.
  390.      * The contents array must be left in the proper state.
  391.      * @return
  392.      */
  393.     public GroceryItem removeTopItem(){
  394.        
  395.         for (int i =0; i < contents.length; i++) {
  396.             if (contents[i] == contents[6]) {
  397.                 allah;
  398.             }
  399.         }
  400.         return null;  // REPLACE THIS WITH YOUR CODE
  401.     }
  402.    
  403.     /**
  404.      * report the item on top of the ShoppingBag's contents.
  405.      * @return the top GroceryItem, or null if empty
  406.      */
  407.     public GroceryItem getTopItem(){
  408.         return null;  // REPLACE THIS WITH YOUR CODE
  409.     }
  410.    
  411.     // ----------------------------------------------------------
  412.    
  413.     /**
  414.      * Determine if theShoppingBag contains any hot goods.
  415.      * @return true if any contents are HOT.
  416.      */
  417.     public boolean hasHotGoods(){
  418.        
  419.         GroceryItem[] HEAT
  420.        
  421.        
  422.         if (contents.toString().equals(HOT)) {
  423.             return true;
  424.         }
  425.  
  426.         return false;  // REPLACE THIS WITH YOUR CODE
  427.     }
  428.    
  429.    
  430.    
  431.     /**
  432.      * Determine if ShoppingBag contains any cold goods.
  433.      * @return  true if any contents are COOL or FROZEN
  434.      */
  435.     public boolean hasColdGoods(){
  436.        
  437.         String COOL = "COOL";
  438.         String FROZEN = "FROZEN";
  439.         if (contents.toString().equals(COOL) || contents.toString().equals(FROZEN)) {
  440.             return true;
  441.         }
  442.         return false;  // REPLACE THIS WITH YOUR CODE
  443.     }
  444.    
  445.     /**
  446.      * Determine if contents in this ShoppingBag are liable to spoil,
  447.      *  i.e. contains both Hot and Cold goods.
  448.      * @return true if Hot and Cold goods present.
  449.      */
  450.     public boolean hasSpoilageRisk(){
  451.        
  452.     if  (contents.toString().hasColdGoods()) {
  453.         return true;
  454.     }
  455.        
  456.         return false;  // REPLACE THIS WITH YOUR CODE
  457.     }
  458.    
  459.     /**
  460.      * Determine if any contents of this ShoppingBag are  fragile.
  461.      * @return  true if any contents are fragile.
  462.      */
  463.     public boolean hasFragileGoods(){
  464.        
  465.     if (contents.) {
  466.         return true;
  467.     }
  468.         return false;  // REPLACE THIS WITH YOUR CODE
  469.     }
  470.    
  471.     /**
  472.      * Determine if this ShoppingBag is at risk of causing breakage
  473.      * because fragile items are stored underneath heavy items.
  474.      * @return
  475.      */
  476.     public boolean hasBreakageRisk(){
  477.        
  478.     if (contents.length >= 6 && MAX_WEIGHT > 2) {
  479.         return true;
  480.     }
  481.         return false;  // REPLACE THIS WITH YOUR CODE
  482.     }
  483.    
  484.     /**
  485.      * Sort the items in Shopping Bag so heavier items are below lighter items,
  486.      *
  487.      * *** FOR FULL MARKS ***
  488.      * Remove the call to Arrays.sort()
  489.      * and replace it with your own sort routine.
  490.      * HINT:  use GroceryItem.compareTo(GroceryItem) to determine the ordering.
  491.      */
  492.     void sortItems(){
  493.        
  494.         Arrays.sort(contents);  // REPLACE THIS WITH YOUR OWN SORT ROUTINE
  495.         GroceryItem.compareTo(GroceryItem)
  496.  
  497.     }
  498.    
  499.     /**
  500.      * Remove the item equal to supplied GroceryItem gi from the ShoppingBag,
  501.      * Add it to the contents of ShoppingBag other,
  502.      * regardless of the state of other  (force it even if the bag is full).
  503.      * If there are multiple items equivalent to gi in the contents then
  504.      * remove the topmost, and do nothing if there are no equivalent items
  505.      * on the contents of gi.
  506.      *
  507.      * @param gi GroceryItem to transfer
  508.      * @param other Bag to which item is transferred.
  509.      * @return true if transfer was successful
  510.      */
  511.     public boolean transferItem(GroceryItem gi, ShoppingBag other) {
  512.        
  513. //  if  GroceryItem[gi] ShoppingBag
  514.    
  515.        
  516.        
  517.        
  518.        
  519.         return false;  // REPLACE THIS WITH YOUR CODE
  520.     }
  521.    
  522.     /**
  523.      * Remove the indexed item from ShoppingBag,
  524.      * Add it to contents of ShoppingBag other, regardless of state.
  525.      * Do nothing if there is no such element in the contents array.
  526.      *
  527.      * @param ix  index of item in contents
  528.      * @param other  bag to which item is transferred.
  529.      * @return true if successful.
  530.      */
  531.     private boolean transferItem(int ix, ShoppingBag other) {
  532.            
  533.            
  534.  
  535.    
  536.                 GroceryItem newItem = contents[ix];
  537.                 contents[ix] = contents[contents.length - 1];
  538.    
  539.        
  540.        
  541.        
  542.         return false;  // REPLACE THIS WITH YOUR CODE
  543.    
  544.     }
  545.    
  546.     /**
  547.      * Remove all fragile items from Bag and transfer them to a newly-created bag.
  548.      * @return  the new ShoppingBag holding the fragile items removed from this bag.
  549.      */
  550.     public ShoppingBag separateFragileItems(){
  551.         return null;
  552.     }
  553.    
  554.     /**
  555.      * Attempt to transfer items between this bag and other
  556.      * such that the weight of each bag is below the maximum weight.
  557.      * AND the maximum number of items per bag is not exceeded.
  558.      *
  559.      * ASSUME that neither bag has too many items to start with
  560.      * (i.e. numItems()<MAX_NUM_ITEMS for each bag)
  561.      * but make no assumptions on initial weight of the bags.
  562.      *
  563.      * This method is DIFFICULT and there may be several ways to go about it.
  564.      * Be aware that there is no JUnit test case supplied for this method,
  565.      * you'll have to write your own test code.
  566.      * Do your best!
  567.      *
  568.      * @param other
  569.      * @return true on success, false if failed to do it.
  570.      */
  571.     boolean balanceWeight(ShoppingBag other){
  572.        
  573.              contents.length < MAX_WEIGHT
  574.              
  575.               numItems() <MAX_NUM_ITEMS
  576.              
  577.              
  578.              
  579.              
  580.              
  581.        
  582.         return false;  // REPLACE THIS WITH YOUR CODE
  583.     }
  584.    
  585.     /**
  586.      * Take a ShoppingBag and return an array comprised of the weights of all
  587.      * of the items in that ShoppingBag, in same order.
  588.      *
  589.      * You might find this useful for doing balanceWeight() (above)
  590.      * but don't feel obliged to use it!
  591.      *
  592.      * @param bag a ShoppingBag
  593.      * @return  array of weights of GroceryItems in bag
  594.      */
  595.     private static double[] getWeightArray(ShoppingBag bag) {
  596.         double[] arr = new double[bag.numItems()];
  597.         int ix=0;
  598.         for (GroceryItem gi: bag.contents){
  599.             arr[ix++] = gi.getWeightKGs();
  600.         }
  601.         return arr;
  602.     }
  603.     /**
  604.      * Take an array of GroceryItem[] and pack the elements one-by-one into ShoppingBags,
  605.      * Creating a new ShoppingBag each time one is filled.
  606.      * Return the created ShoppingBags as an array of ShoppingBags[].
  607.      *
  608.      * This method would be significantly easier if you were allowed to use
  609.      * ArrayList.  But you aren't.  To make this a little bit easier you
  610.      * can assume that no more than MAXNBAG ShoppingBags will be required.
  611.      * You can also assume that the goods array is not null, nor is it full
  612.      * of null elements.  i.e. there is at least one good to pack.
  613.      *
  614.      * @param goods
  615.      * @return
  616.      */
  617.     private static final int MAXNBAG = 12; // assume this to make it easier
  618.     public static ShoppingBag[] packIntoBags(GroceryItem[] goods){
  619.        
  620.         return new ShoppingBag[] {null};  // REPLACE THIS WITH YOUR CODE
  621.     }
  622.     ////////
  623.    
  624.     /* (non-Javadoc)
  625.      * @see java.lang.Object#toString()
  626.      */
  627.     public String toString(){
  628.         String str = "";
  629.         for(GroceryItem gi: contents)
  630.             if (gi!=null) str = gi.toString() + "\n" + str ;
  631.         str += String.format("   %2d items,  total weight  %6.2f kg\n",numItems(),totalWeight());
  632.         System.out.printf("total cost   $%6.2f   \n");
  633.         return str;
  634.     }
  635.    
  636.     //////////////////////////////////////////////
  637.     // a test driver.
  638.    
  639.     public static void main(String[] args) {
  640.  
  641.         ShoppingBag[] bags = packIntoBags(GroceryItem.testArray());
  642.         int ix = 0;
  643.         while(bags[ix]!=null){
  644.             System.out.println("Bag " + (ix+1) + ": ");
  645.             System.out.println(bags[ix++]);
  646.         }
  647.     }
  648.  
  649. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement