public class GroceryItem { // instance variables private String name; private double cost; // could be float or int (if in cents) // Could also have quantity // (e.g., you might have 1 "item" object for 5 apples). // Constructor public GroceryItem(String itemName, double itemCost) { this.name = itemName; this.cost = itemCost; } // Observer methods (one for each instance variable) public String getName() { return this.name; } public double getCost() { return this.cost; } // Should also have getQuantity if you have a quantity instance variable. }
public class GroceryList { // instance variables private ArrayList<GroceryItem> cart; // Constructor public GroceryList() { this.cart = new ArrayList<GroceryItem>(); } // Methods public void addItem(GroceryItem item) // Add item to the cart { this.cart.add(item); } public void printList() // Print items, one per line { for ( GroceryItem item: this.cart ) { System.out.println(item.getName() + ": " + item.getCost()); } /* OR for ( int i = 0; i < this.cart.size(); i++ ) { GroceryItem item = this.cart.get(i); System.out.println(item.getName() + ": " + item.getCost()); } */ } public int numItems() { return this.cart.size(); } public int totalCost() // Cost of buying all items in cart { double total = 0.0; for ( GroceryItem item: this.cart ) { total = total + item.getCost(); // OR total += item.getCost(); } /* OR for ( int i = 0; i < this.cart.size(); i++ ) { GroceryItem item = this.cart.get(i); total = total + item.getCost(); } */ } public String cheapestItem() // Returns name of cheapest item in list { if ( this.cart.isEmpty() == true ) return ""; // Assume the first item is the cheapest. GroceryItem cheapest = this.cart.get(0); // Look through all others to see if any are cheaper. for ( GroceryItem item: this.cart ) { if ( item.getCost() < cheapest.getCost() ) cheapest = item; } return cheapest.getName(); /* OR // Assume the first item is the cheapest. int cheapIndex = 0; // Starting at the 2nd item, see if any others are cheaper. for ( int i = 1; i < this.cart.size(); i++ ) { GroceryItem item = this.cart.get(i); if ( item.getCost() < this.cart.get(cheapIndex).getCost() ) cheapIndex = i; } return this.cart.get(cheapIndex).getName(); */ } public int indexOfPriciestItem() { if ( this.cart.isEmpty() == true ) return -1; // Assume the first item is the priciest. int priceyIndex = 0; // Starting at the 2nd item, see if any others are pricier. for ( int i = 1; i < this.cart.size(); i++ ) { GroceryItem item = this.cart.get(i); if ( item.getCost() > this.cart.get(priceyIndex).getCost() ) priceyIndex = i; } return priceyIndex; } /** Prints number of items in list, cheapest item, most expensive item * (and its price), and the total cost of all the items. */ public void printSummaryStats() { System.out.println("Number of items in list: " + this.numItems()); GroceryItem cheapest = this.cart.get(this.cheapestItem); GroceryItem priciest = this.cart.get(this.indexOfPriciestItem()); System.out.println("Cheapest item is: " + cheapest.getName()); System.out.println("Priciest item is: " + priciest.getName() + " (" + priciest.getCost() + ")"); System.out.println("Total cost of all items is: " + this.totalCost()); } // Additional methods one could write: // - A method that returns the index of the alphbetically first item // - A method that returns the index of the alphbetically first item // starting from a certain point, e.g., // alphaFirst(0) returns the first from the whole list // alphaFirst(5) returns the first from the range (5 .. n-1) // - A method that swaps two items in the list, e.g., // swap(0, 5) swaps the items at indices 0 and 5 // - A method that finds the alphbetically first item in a range // (i .. n-1) and moves that item to the beginning of the range, e.g., // sortHelper(5) finds the alphabetically smallest item // from index 5 to the end of the list and // rearranges that part of the list in some // way so that the smallest item will be at // index 5. // - A method that sorts the items in the grocery list alphabetically. }