In java Need to construct a test that passes only one of them. On the other
Question:
In java
Need to construct a test that passes only one of them. On the other words, need to find an edge test that passes only Basket 6 or 7. They cannot have same test result.
====================================================================================== Basket6 =======================================================================================
package cse12pa1student;
import java.util.ArrayList;
public class Basket6 implements Basket {
public Basket6() { this.items = new ArrayList(); }
ArrayList items;
@Override public int count() { return this.items.size(); }
@Override public int countItem(Item i) { int countItem = 0; for (int j = 0; j < this.items.size(); j += 1) { if (this.items.get(j).equals(i)) { countItem += 1; } } return countItem; }
@Override public int totalCost() { int price = 0; for (int j = 0; j < this.items.size(); j += 1) { price += this.items.get(j).priceInCents; } return price; }
@Override public void addToBasket(Item i) { this.items.add(i); }
@Override public boolean removeFromBasket(Item i) { if(i == null) { return false; } return this.items.remove(i); }
@Override public boolean removeAllFromBasket(Item i) { if(i == null) { return false; } boolean removed = false; while (this.items.contains(i)) { removed = true; this.items.remove(i); } return removed; } @Override public void empty() { this.items.clear(); }
}
================================================================================================= Basket7 =================================================================================================
package cse12pa1student;
import java.util.ArrayList; import java.util.Arrays;
public class Basket7 implements Basket {
public Basket7() { this.items = new ArrayList>(); }
ArrayList> items;
@Override public int count() { int count = 0; for (int i = 1; i < this.items.size(); i++) { count += this.items.get(i).size() * i; } return count; }
@Override public int countItem(Item i) { for (int j = 0; j < this.items.size(); j++) { if (this.items.get(j).contains(i)) return j; } return 0; }
@Override public int totalCost() { int totalCost = 0; for (int i = 1; i < this.items.size(); i++) { for (Item item : this.items.get(i)) { totalCost += item.priceInCents * i; } } return totalCost; }
@Override public void addToBasket(Item i) { for (int j = 1; j < this.items.size(); j++) { if (this.items.get(j).contains(i)) { this.items.get(j).remove(i); if (j + 1 == this.items.size()) { ArrayList temp = new ArrayList(); temp.add(i); this.items.add(temp); } else{ this.items.get(j + 1).add(i); } return; } } while(this.items.size() <= 1) this.items.add(new ArrayList()); this.items.get(1).add(i); }
@Override public boolean removeFromBasket(Item i) { for (int j = 1; j < this.items.size(); j++) { if (this.items.get(j).contains(i)) { this.items.get(j).remove(i); this.items.get(j - 1).add(i); return true; } } return false; }
@Override public boolean removeAllFromBasket(Item i) { for (int j = 1; j < this.items.size(); j++) { if (this.items.get(j).contains(i)) return this.items.get(j).remove(i); } return false; }
@Override public void empty() { this.items.clear(); } }
================================================================================================= Basket.java =================================================================================================
package cse12pa1student;
public interface Basket { /* * @return the total count of all items, counting duplicates, in the basket. */ int count();
/* * @param i The item to count * * @return The number of the provided Item that are in the basket */ int countItem(Item i);
/* * @return the total cost in cents of all items in the basket, counting duplicates */ int totalCost();
/* * @param i The item to add */ void addToBasket(Item t);
/* * Remove a single copy of an item from the basket * * @param i The Item to remove * * @return false if the item was not in the basket, true otherwise */ boolean removeFromBasket(Item i);
/* * Remove all copies of an item from the basket * * @param i The Item to remove * * @return false if the item was not in the basket, true otherwise */ boolean removeAllFromBasket(Item i);
/* * Remove all items from the basket */ void empty();
}
================================================================================================= Item.java =================================================================================================
package cse12pa1student;
public class Item { String name; int priceInCents; public Item(String name, int priceInCents) { this.name = name; this.priceInCents = priceInCents; } @Override public boolean equals(Object other) { if(!(other instanceof Item)) { return false; } Item i = (Item)other; return i.name.equals(this.name) && i.priceInCents == this.priceInCents; } }
Cost management a strategic approach
ISBN: 978-0073526942
5th edition
Authors: Edward J. Blocher, David E. Stout, Gary Cokins