Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ass1;
- import java.util.Arrays;
- import ass1.GroceryItem;
- import ass1.GroceryItem.HEAT;
- /* NOTES
- *
- * Modelling the contents with GroceryItem[] (an array of GroceryItems) is not
- * the most efficient method. In practice we might use a different class
- * like ArrayList, which works like an array but can grow/shrink as required,
- * and has members for manipulation of its elements.
- *
- * A plain array is used here in order to teach us how to work with arrays.
- * Please don't use library methods for array processing (like "Arrays" methods);
- * work out how to do it yourself.
- * The sortItems() method supplied for you uses Arrays.sort(). You can use this
- * as-is with your own methods, however for full marks you must replace the call
- * to Arrays.sort() with your own sort routine.
- *
- */
- public class ShoppingBag {
- /**
- * put your student ID here (replace "bondj007" with yours).
- */
- public static final String STUDENT_ID = "kimdy031";
- /**
- * maximum # items a bag should hold (if it holds more, it will be overfull)
- */
- public static final int MAX_NUM_ITEMS = 6;
- /**
- * maximum total weight a bag should hold (if it holds more, it will be overloaded)
- */
- public static final double MAX_WEIGHT = 2.0;
- /**
- * Array holding the GroceryItems in this bag.
- * This array is to be maintained at exactly the right length to hold the number of items.
- * i.e. if the bag has 4 items, this array will be length 4. There will be no null elements.
- * To this end it will be necessary to grow the array to add new items, and to shrink the array
- * when items are deleted.
- * The methods compactContents() and growContents() will be used for this.
- *
- * Elements are ordered from BOTTOM to TOP.
- * The first element, contents[0], is considered to be at the BOTTOM of the bag.
- * The final element, contents[length-1], is at the TOP of the bag.
- */
- private GroceryItem[] contents;
- // -------------------------------------------------------------------
- /**
- * remove any nulls from the contents array, shrinking it if necessary.
- * Example: if the contents array prior to call is [A null B C null D] (length 6)
- * then it will be [A B C D] (length 4) after call.
- *
- * Use this routine in your other methods to remove nulls after/during a deletion operation.
- *
- * BE AWARE there is no JUnit test case supplied for this method.
- */
- private void compactContents(){
- int nullCount = 0; // make a new variable to count
- //this loop is to count the number of null elements in the original list
- for (int i = 0; i < contents.length; i++) {
- if (contents[i].equals(null)) { // if elements in contents array has null
- nullCount++; // increase the nullCount
- }
- GroceryItem[] newItem = new GroceryItem[contents.length - nullCount]; // create a newlist called newItem,
- // which counts the elements of contents which does not include null
- int indexNewItem = 0;
- // copy the whole arraylist
- for (int index = 0; index < contents.length; index++) {
- if (!contents[index].equals(null)) { // if the element in the array does not have null
- newItem[indexNewItem] = contents[index]; // just copy them into the newItem list.
- indexNewItem ++;
- }
- }
- contents = newItem; // copy back.
- }
- }
- /**
- * grow the contents array by one element, such that the existing elements are not changed,
- * and the new (final) element is null.
- * Example: If the contents array prior to call is [A B C D] (length 4)
- * then contents after call is [A B C D null]
- *
- * Use this routine in your other methods to make space so new items can be added.
- *
- * BE AWARE there is no JUnit test case supplied for this method.
- */
- private void growContents(){
- GroceryItem[] gc = new GroceryItem[contents.length+1]; // create a new array list which is increased by one element.
- int increaseElement = 0; // make a new element for the new array
- for(int i = 0; i < contents.length; i++) {
- gc[increaseElement] = contents[i]; // copy the contents array into the new aray.
- increaseElement ++;
- }
- contents = gc;// copy elements of contents into grow contents array.
- }
- // --------------------------------------------------------
- /**
- * construct a new ShoppingBag with an empty contents array
- * (i.e. not null, but an array of no elements)
- */
- public ShoppingBag() {
- this.contents = new GroceryItem[ShoppingBag.MAX_NUM_ITEMS]; // Make a new Shopping Bag with empty contents array.
- // The new shopping bag can store maximum 6 items (Max_NUM_iTMES is used)
- }
- /**
- * construct a new ShoppingBag with contents initialised from args[] array.
- * contents of the ShoppingBag must be ordered in the same way as the args are supplied
- * i.e. args[0] on the bottom of the bag, args[length-1] on the top.
- * @param args
- */
- public ShoppingBag(GroceryItem... args) {
- }
- /**
- * return the number of items in the ShoppingBag.
- * @return the number of items
- */
- public int numItems() {
- int countNum = 0;
- for(int i = 0; i < contents.length; i++) {
- GroceryItem temp = contents[i];
- if (temp != null) {
- countNum++;
- }
- }
- return countNum;
- }
- /**
- * determine if the ShoppingBag is empty (has no contents)
- * @return true if empty
- */
- public boolean isEmpty(){
- if (this.numItems() == 0) {
- return true;
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- // ------------------------------------------------------------
- /**
- * return the total weight of all items in the ShoppingBag.
- * @return total weight
- */
- public double totalWeight() {
- double total = 0; // initialize the total weight
- for (int i = 0; i < this.numItems(); i++) { // for loop which is less than number
- // of items in the shopping bag.
- GroceryItem tempItem = contents[i]; // new variable to copy the grocery Item
- total += tempItem.getWeightKGs(); // get the totalweight by using the variable
- // created and using the funcction.
- }
- return total; // copy back.
- }
- // contents[0].getWeightKGs()
- /**
- * determine the total GST (tax) payable on items in this ShoppingBag
- * @return total GST
- */
- public double totalGST(){
- double totalTax;
- for (int i = 0; i < this.numItems(); i++) {
- totalTax = (price * gstFactor);
- }
- return totalTax;
- // double tax = 0;
- //
- // for(int i =0; i < this.numItems(); i++) {
- // GroceryItem tempItem = contents[i];
- // if (tempItem.isTaxable()) {
- // tax++;
- // }
- // }
- // return tax;
- }
- // --------------------------------------------------------------
- /** determine if the ShoppingBag's contents exceed the maximum weight
- * or exceeds the maximum number of items.
- * @return true if overloaded/overfull.
- */
- public boolean isOverloaded(){
- // this if statement checks whether the array is overloaded or overfull.
- if(this.numItems() > this.MAX_NUM_ITEMS || this.totalWeight() > this.MAX_WEIGHT) {
- return true;
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if a specific item can be placed into this ShoppingBag without
- * exceeding maximum weight or exceeding maximum number of items.
- * @param gi the GroceryItem to consider
- * @return true if item will fit without exceeding weight/number limits.
- */
- public boolean canFitItem(GroceryItem gi){
- if (this.numItems() < this.MAX_NUM_ITEMS && this.totalWeight()+ gi.getWeightKGs() <= this.MAX_WEIGHT) {
- return true; // check whether the number of items is sless than 6
- // and less than 2 (max weight) so that we can add a new specific item
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * add an item to the top of the ShoppingBag, regardless of existing contents.
- * @param item the GroceryItem to add.
- */
- public void forceIntoBag(GroceryItem item){
- if (this.numItems() >= 6) {
- GroceryItem [] newArray = new GroceryItem [this.contents.length+1];
- for (int i = 0; i < contents.length; i++) {
- newArray[i] = contents[i];
- }
- newArray[newArray.length-1] = item;
- contents = newArray;
- }
- else {
- contents[this.numItems()] = item;
- }
- }
- /**
- * add an item to the top of the ShoppingBag ONLY IF it will fit.
- * @param item the GroceryItem to add.
- * @return true if added, false if not.
- */
- public boolean addToBag(GroceryItem item){
- if (this.canFitItem(item)) {
- this.forceIntoBag(item);
- return true;
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- // ------------------------------------------------------------
- /**
- * Determine if an item equal to the parameter item occurs in the
- * ShoppingBag's contents and return its index, or -1 if the item
- * is not present. Use GroceryItem.equals() to compare items.
- * If the designated item occurs more than once in the contents then
- * return the index of the topmost item.
- *
- * NOTE this is private because it refers to an implementation detail:
- * the array index. We don't want to expose our implementation to the
- * user of the class.
- * However certain other methods in this class could and should use this
- * routine!
- *
- * @param gi GroceryItem to search for in contents.
- * @return
- */
- private int findIndexOfItem(GroceryItem gi){
- for (int i = 0; i < contents.length; i++) {
- if (gi == contents[i]) {
- return i;
- }
- }
- return -1; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if an item having the supplied name occurs in the contents
- * and return its index, or -1 if not present.
- * If several items have the same name then return the topmost.
- *
- * NOTE private for same reasons as above.
- *
- * @param name name of GroceryItem to search for in contents.
- * @return
- */
- private int findIndexOfName(String name){
- for(int i = 0; i < contents.length; i++) {
- if (name == contents[i].toString()) {
- return i;
- }
- }
- return -1; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if ShoppingBag contains a GroceryItem with same name as supplied parameter
- * @param name the name of GroceryItem to look for
- * @return true if item present, false if not
- */
- public boolean hasItem(String name){
- for(int i = 0; i < contents.length; i++) {
- if(name == contents[i].getName()) {
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if ShoppingBag contains a GroceryItem equal to the supplied parameter
- * @param gi the GroceryItem to search for
- * @return true if item present, false if not.
- */
- public boolean hasItem(GroceryItem gi){
- for(int i = 0; i < contents.length; i++) {
- if(gi.equals(contents[i])) {
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Remove the GroceryItem at supplied index from the contents array
- * and return it, or return null if the index is outside array limits.
- * The contents array must be left in the proper state,
- * i.e. correct length and containing no nulls.
- *
- * private because it refers to underlying implementation (array)
- *
- * @param ix index of item to return
- * @return the item which was at position ix.
- */
- private GroceryItem removeFromBag(int ix){
- GroceryItem[] removeList = new GroceryItem[contents.length]; // copy the whole list.// copy the whole list.
- int indexCheck = 0;
- if (ix < 0 || ix > 5) {
- return null;
- }
- GroceryItem newItem = contents[ix];
- for (int i = 0; i < contents.length; i++) {
- if (i != ix) {
- removeList[indexCheck] = contents[i];
- indexCheck ++;
- // REPLACE THIS WITH YOUR CODE
- }
- }
- contents = removeList;
- return newItem;
- }
- /**
- * Remove a GroceryItem from the bag which is the same as the supplied
- * parameter, and return it. Return null if no matching item was found
- * in the ShoppingBag.
- * The contents array must be maintained in the proper state.
- * @param gi the GroceryItem to search for in contents
- * @return the found GroceryItem, or null
- */
- public GroceryItem removeFromBag(GroceryItem gi){
- GroceryItem[] removeItem = new GroceryItem[contents.length];
- int checkIndex = 0 ;
- GroceryItem newItem = null;
- for (int i = 0; i < contents.length; i++) {
- if (contents[i] != gi) {
- removeItem[checkIndex] = contents[i];
- checkIndex ++;
- }
- else
- {
- newItem = contents[i];
- }
- }
- contents = removeItem;
- return newItem;
- }
- // for (int i = 0; i < contents.length; i++) {
- // if (this.findIndexOfItem(gi) == i) {
- // return this.removeFromBag(i);
- // }
- // }
- // return null;
- // return this.removeFromBag(this.findIndexOfItem(gi));
- // return null; // REPLACE THIS WITH YOUR CODE
- /**
- * Remove a GroceryItem from the bag which has the same name as the
- * supplied String. Return null if no such item was found.
- * The contents array must be maintained in the proper state.
- * @param name The name of the item to remove.
- * @return the removed item.
- */
- public GroceryItem removeFromBag(String name){
- return this.removeFromBag(this.findIndexOfName(name));
- }
- // ---------------------------------------------------------
- /**
- * Remove the topmost item from the ShoppingBag and return it.
- * The contents array must be left in the proper state.
- * @return
- */
- public GroceryItem removeTopItem(){
- return this.removeFromBag(getTopItem()); // REPLACE THIS WITH YOUR CODE
- }
- /**
- * report the item on top of the ShoppingBag's contents.
- * @return the top GroceryItem, or null if empty
- */
- public GroceryItem getTopItem(){
- if (this.numItems() != 0) {
- return this.contents[this.numItems()-1];
- }
- return null; // REPLACE THIS WITH YOUR CODE
- }
- // ----------------------------------------------------------
- /**
- * Determine if theShoppingBag contains any hot goods.
- * @return true if any contents are HOT.
- */
- public boolean hasHotGoods(){
- for (int i = 0; i < contents.length; i++) {
- if (contents[i].getName("HOT")) {
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if ShoppingBag contains any cold goods.
- * @return true if any contents are COOL or FROZEN
- */
- public boolean hasColdGoods() {
- for (int i = 0; i < contents.length; i++) {
- if (contents[i].toString().matches("COOL") || contents[i].toString().matches("FROZEN")) {
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if contents in this ShoppingBag are liable to spoil,
- * i.e. contains both Hot and Cold goods.
- * @return true if Hot and Cold goods present.
- */
- public boolean hasSpoilageRisk(){
- if (this.hasHotGoods() && this.hasColdGoods()) {
- return true;
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if any contents of this ShoppingBag are fragile.
- * @return true if any contents are fragile.
- */
- public boolean hasFragileGoods(){
- for (int i = 0; i < contents.length; i++) {
- if (contents[i].isFragile()) {
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Determine if this ShoppingBag is at risk of causing breakage
- * because fragile items are stored underneath heavy items.
- * @return
- */
- public boolean hasBreakageRisk(){
- for (int i = 0; i < contents.length; i++) {
- if (contents[i].getWeightKGs() < contents[i].isFragile()) {
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Sort the items in Shopping Bag so heavier items are below lighter items,
- *
- * *** FOR FULL MARKS ***
- * Remove the call to Arrays.sort()
- * and replace it with your own sort routine.
- * HINT: use GroceryItem.compareTo(GroceryItem) to determine the ordering.
- */
- void sortItems(){
- Arrays.sort(contents); // REPLACE THIS WITH YOUR OWN SORT ROUTINE
- }
- /**
- * Remove the item equal to supplied GroceryItem gi from the ShoppingBag,
- * Add it to the contents of ShoppingBag other,
- * regardless of the state of other (force it even if the bag is full).
- * If there are multiple items equivalent to gi in the contents then
- * remove the topmost, and do nothing if there are no equivalent items
- * on the contents of gi.
- *
- * @param gi GroceryItem to transfer
- * @param other Bag to which item is transferred.
- * @return true if transfer was successful
- */
- public boolean transferItem(GroceryItem gi, ShoppingBag other){
- for (int i = 0; i < contents.length; i++) {
- if (contents[i] == gi) {
- this.forceIntoBag(gi) ;
- if (this.numItems() > 1)
- this.removeTopItem();
- return true;
- }
- }
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Remove the indexed item from ShoppingBag,
- * Add it to contents of ShoppingBag other, regardless of state.
- * Do nothing if there is no such element in the contents array.
- *
- * @param ix index of item in contents
- * @param other bag to which item is transferred.
- * @return true if successful.
- */
- private boolean transferItem(int ix, ShoppingBag other){
- GroceryItem[] other = new GroceryItem [contents.length-1];
- int checkIndexItem = 0;
- GroceryItem newItem = contents[ix];
- for (int i = 0; i < contents.length; i++) {
- if (i == ix) {
- other[checkIndexItem] = contents[i];
- checkIndexItem ++;
- return true;
- }
- }
- contents = other;
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Remove all fragile items from Bag and transfer them to a newly-created bag.
- * @return the new ShoppingBag holding the fragile items removed from this bag.
- */
- public ShoppingBag separateFragileItems(){
- return null;
- }
- /**
- * Attempt to transfer items between this bag and other
- * such that the weight of each bag is below the maximum weight.
- * AND the maximum number of items per bag is not exceeded.
- *
- * ASSUME that neither bag has too many items to start with
- * (i.e. numItems()<MAX_NUM_ITEMS for each bag)
- * but make no assumptions on initial weight of the bags.
- *
- * This method is DIFFICULT and there may be several ways to go about it.
- * Be aware that there is no JUnit test case supplied for this method,
- * you'll have to write your own test code.
- * Do your best!
- *
- * @param other
- * @return true on success, false if failed to do it.
- */
- boolean balanceWeight(ShoppingBag other){
- return false; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * Take a ShoppingBag and return an array comprised of the weights of all
- * of the items in that ShoppingBag, in same order.
- *
- * You might find this useful for doing balanceWeight() (above)
- * but don't feel obliged to use it!
- *
- * @param bag a ShoppingBag
- * @return array of weights of GroceryItems in bag
- */
- private static double[] getWeightArray(ShoppingBag bag){
- double[] arr = new double[bag.numItems()];
- int ix=0;
- for (GroceryItem gi: bag.contents){
- arr[ix++] = gi.getWeightKGs();
- }
- return arr;
- }
- /**
- * Take an array of GroceryItem[] and pack the elements one-by-one into ShoppingBags,
- * Creating a new ShoppingBag each time one is filled.
- * Return the created ShoppingBags as an array of ShoppingBags[].
- *
- * This method would be significantly easier if you were allowed to use
- * ArrayList. But you aren't. To make this a little bit easier you
- * can assume that no more than MAXNBAG ShoppingBags will be required.
- * You can also assume that the goods array is not null, nor is it full
- * of null elements. i.e. there is at least one good to pack.
- *
- * @param goods
- * @return
- */
- private static final int MAXNBAG = 12; // assume this to make it easier
- public static ShoppingBag[] packIntoBags(GroceryItem[] goods){
- return new ShoppingBag[] {null}; // REPLACE THIS WITH YOUR CODE
- }
- ////////
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- public String toString(){
- String str = "";
- for(GroceryItem gi: contents)
- if (gi!=null) str = gi.toString() + "\n" + str ;
- str += String.format(" %2d items, total weight %6.2f kg\n",numItems(),totalWeight());
- //System.out.printf("total cost $%6.2f \n");
- return str;
- }
- //////////////////////////////////////////////
- // a test driver.
- public static void main(String[] args) {
- ShoppingBag[] bags = packIntoBags(GroceryItem.testArray());
- int ix = 0;
- while(bags[ix]!=null){
- System.out.println("Bag " + (ix+1) + ": ");
- System.out.println(bags[ix++]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement