Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ass1;
- import static org.junit.Assert.assertEquals;
- 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;
- private static final GroceryItem[] Max_num_Items = null;
- /**
- * 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;
- //
- // for(int i = 0; i < contents.length; i++) {
- // gc[increaseElement] = contents[i];
- // increaseElement ++;
- // }
- // contents = gc;
- // --------------------------------------------------------
- /**
- * construct a new ShoppingBag with an empty contents array
- * (i.e. not null, but an array of no elements)
- */
- public ShoppingBag() {
- int newShoppingBag [] = new int[0];
- // YOUR CODE HERE
- }
- /**
- * 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) {
- ShoppingBag newShoppingBag = new ShoppingBag(); // ???????????
- }
- /**
- * return the number of items in the ShoppingBag.
- * @return the number of items
- */
- public int numItems(){
- GroceryItem[] newItem = new GroceryItem[contents.length];
- return contents.length;
- // int length = contents.length;
- // return length;
- // return -1; // REPLACE THIS WITH YOUR CODE
- }
- /**
- * determine if the ShoppingBag is empty (has no contents)
- * @return true if empty
- */
- public boolean isEmpty() {
- if (contents.length == 0) {
- return true;
- }
- else {
- return false;
- }
- }
- // ------------------------------------------------------------
- /**
- * return the total weight of all items in the ShoppingBag.
- * @return total weight
- */
- public double totalWeight() {
- return (MAX_WEIGHT * MAX_NUM_ITEMS);
- // REPLACE THIS WITH YOUR CODE
- }
- /**
- * determine the total GST (tax) payable on items in this ShoppingBag
- * @return total GST
- */
- public double totalGST(){
- double totalGST = 0;
- for (int i = 0; i < contents.length; i++) {
- totalGST += contents.getClass(
- }
- }
- return totalGST;
- }
- // --------------------------------------------------------------
- /** 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(){
- if (contents.length < 6) {
- return false;
- }
- else {
- return true; // 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(int gi){
- for (int i= 0; i < contents.length; i++)
- if (contents.length <= 6 && MAX_WEIGHT < 2 )
- GroceryItem [gi] = contents [i]
- return true;
- else
- 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){
- contents[contents.length-1] = contents[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 (contents.length < 6 && MAX_WEIGHT < 2) {
- return true;
- }
- else {
- 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(contents[i].equals(gi)) {
- 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(contents[i].getName().equals(name)) {
- 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(contents[i].getName().toString().equals(name)) {
- 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(contents[i].equals(gi)) {
- 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) {
- if((ix < contents.length && ix >= 0)) {
- GroceryItem newItem = contents[ix];
- contents[ix] = contents[contents.length - 1];
- contents[contents.length - 1] = null;
- compactContents();
- return newItem;
- }
- return null;
- // REPLACE THIS WITH YOUR CODE
- }
- /**
- * 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){
- for (int i = 0; i < contents.length; i++) {
- if (contents[i].equals(gi)) {
- return contents[i];
- }
- }
- return null;
- }
- /**
- * 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){
- for(int i = 0; i < contents.length; i++) {
- if (!contents[i].equals(name)) {
- contents.IndexOfName[name] = contents[i];
- }
- }
- return null;
- }
- // ---------------------------------------------------------
- /**
- * Remove the topmost item from the ShoppingBag and return it.
- * The contents array must be left in the proper state.
- * @return
- */
- public GroceryItem removeTopItem(){
- for (int i =0; i < contents.length; i++) {
- if (contents[i] == contents[6]) {
- allah;
- }
- }
- return null; // 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(){
- return null; // REPLACE THIS WITH YOUR CODE
- }
- // ----------------------------------------------------------
- /**
- * Determine if theShoppingBag contains any hot goods.
- * @return true if any contents are HOT.
- */
- public boolean hasHotGoods(){
- GroceryItem[] HEAT
- if (contents.toString().equals(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(){
- String COOL = "COOL";
- String FROZEN = "FROZEN";
- if (contents.toString().equals(COOL) || contents.toString().equals(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 (contents.toString().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(){
- if (contents.) {
- 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(){
- if (contents.length >= 6 && MAX_WEIGHT > 2) {
- 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
- GroceryItem.compareTo(GroceryItem)
- }
- /**
- * 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) {
- // if GroceryItem[gi] ShoppingBag
- 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 newItem = contents[ix];
- contents[ix] = contents[contents.length - 1];
- 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){
- contents.length < MAX_WEIGHT
- numItems() <MAX_NUM_ITEMS
- 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