Advertisement
Guest User

Untitled

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