My project is to make a program for A local coffee shop that sells a variety of
ID: 3623498 • Letter: M
Question
My project is to make a program for A local coffee shop that sells a variety of different items shown below to their customers. You are asked to write a Java application that can be used to keep track of these items. Additionally, this program provides a way to print out a listing of the items.Item Name Price
Coffee $1.00
Water $2.00
Milk $1.50
Bagel $1.25
Donut $0.75
Your program will create a class, named Item. This class has the following:
A String instance variable to hold the item name
A double instance variable to hold the price
A constructor that takes a String and double to initialize the instance variables
A get and set method for each instance variable
Once you have this class created, you write a second class named CoffeeDriver. This class has the following methods:
• sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen
• sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen
• main - It creates an array of Item objects using the data above to set each Item's information.
Be sure your program demonstrates good programming style. //<------"(appropriate comments, identifier names, indenting, etc within the program code).
Explanation / Answer
import java.util.*;
public class CoffeeDriver
{
//This method will sort the array of items by name
//and then print them to the screen
public void sortName(Item[] itemArray){
Arrays.sort(itemArray, new ItemNameComparator());
printItems(itemArray);
}
//This method will sort the array of items by price
//and then print them to the screen
public void sortPrice(Item[] itemArray){
Arrays.sort(itemArray, new ItemPriceComparator());
printItems(itemArray);
}
//This method prints the array of items to the screen
private void printItems(Item[] itemArray){
for (int x = 0; x < itemArray.length; x++){
System.out.println(itemArray[x]);
}
}
//Comparators used to do the perform the sorting
class ItemNameComparator implements Comparator<Item>
{
public int compare(Item a, Item b){
return a.getItemName().compareTo(b.getItemName());
}
}
class ItemPriceComparator implements Comparator<Item>
{
public int compare(Item a, Item b){
return ((new Double(a.getPrice())).compareTo(new Double(b.getPrice())));
}
}
public static void main(String[] args)
{
int numItems = 5;
//This array will hold the items
Item[] itemArray = new Item[numItems];
//Initialize the items array
itemArray[0] = new Item("Coffee", 1.0);
itemArray[1] = new Item("Water", 2.0);
itemArray[2] = new Item("Milk", 1.5);
itemArray[3] = new Item("Bagel", 1.25);
itemArray[4] = new Item("Donut", 0.75);
CoffeeDriver cf = new CoffeeDriver();
cf.sortName(itemArray);
System.out.println();
cf.sortPrice(itemArray);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.