Wings Coffee Shop A local coffee shop sells a variety of different items shown b
ID: 3641440 • Letter: W
Question
Wings Coffee ShopA local coffee shop 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. After initializing the array, prompt the user for how they want to see the list – sorted by name or price. Then call the appropriate method.
Explanation / Answer
public class CoffeeDriver {
public static java.util.Comparator<Item> NameComparator = new java.util.Comparator<Item>() {
public int compare(Item item1, Item item2) {
String itemName1 = item1.getName();
String itemName2 = item2.getName();
//ascending order
return itemName1.compareTo(itemName2);
}
};
public static java.util.Comparator<Item> PriceComparator = new java.util.Comparator<Item>() {
public int compare(Item item1, Item item2) {
double price1 = item1.getPrice();
double price2 = item2.getPrice();
//ascending order
return Double.compare(price1, price2);
}
};
static void sortName(Item[] items) {
java.util.Arrays.sort(items, CoffeeDriver.NameComparator);
printArray(items);
}
static void sortPrice(Item[] items) {
java.util.Arrays.sort(items, CoffeeDriver.PriceComparator);
printArray(items);
}
static void printArray(Item[] items) {
for (int i = 0; i < items.length; i++) {
Item item = items[i];
System.out.println(item.toString());
}
}
public static void main(String[] args) {
Item[] items = new Item[5];
items[0] = new Item("Water", 1.00);
items[1] = new Item("Milk", 2.00);
items[2] = new Item("Bagel", 1.50);
items[3] = new Item("Donnut", 1.20);
items[4] = new Item(" ", 0.75);
CoffeeDriver.sortName(items);
CoffeeDriver.sortPrice(items);
}
}
public class CoffeeDriver {
public static java.util.Comparator<Item> NameComparator = new java.util.Comparator<Item>() {
public int compare(Item item1, Item item2) {
String itemName1 = item1.getName();
String itemName2 = item2.getName();
//ascending order
return itemName1.compareTo(itemName2);
}
};
public static java.util.Comparator<Item> PriceComparator = new java.util.Comparator<Item>() {
public int compare(Item item1, Item item2) {
double price1 = item1.getPrice();
double price2 = item2.getPrice();
//ascending order
return Double.compare(price1, price2);
}
};
static void sortName(Item[] items) {
java.util.Arrays.sort(items, CoffeeDriver.NameComparator);
printArray(items);
}
static void sortPrice(Item[] items) {
java.util.Arrays.sort(items, CoffeeDriver.PriceComparator);
printArray(items);
}
static void printArray(Item[] items) {
for (int i = 0; i < items.length; i++) {
Item item = items[i];
System.out.println(item.toString());
}
}
public static void main(String[] args) {
Item[] items = new Item[5];
items[0] = new Item("Water", 1.00);
items[1] = new Item("Milk", 2.00);
items[2] = new Item("Bagel", 1.50);
items[3] = new Item("Donnut", 1.20);
items[4] = new Item(" ", 0.75);
CoffeeDriver.sortName(items);
CoffeeDriver.sortPrice(items);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.