Where do I change the code so that on the coffee driver file the compiler recognizes the n and p on:
if (selectSort == n)
sortPrice(arr);
if (selectSort == p)
sortName(arr);
Here are my two files Items and Coffee driver
import java.util.*;
public class Items
{
private String itemName;
private double itemPrice;
public Items (String itemName, double itemPrice)
{
this.itemPrice = itemPrice;
this.itemName = itemName;
}
public Items()
{
}
public String getName()
{
return itemName;
}
public double getPrice()
{
return itemPrice;
}
public void setItemName(String someItem)
{
itemName = someItem;
}
public void setItemPrice(double somePrice)
{
itemPrice = somePrice;
}
public String toString()
{
return "Item: " + itemName + " price:" + itemPrice;
} static final Comparator<Items> NAME_ORDER = new Comparator<Items>() {
public int compare(Items e1, Items e2) {
return e1.getName().compareTo(e2.getName());
}
};
static final Comparator<Items> PRICE_ORDER = new Comparator<Items>() {
public int compare(Items e1, Items e2) {
return Double.compare(e1.getPrice(), e2.getPrice());
}
};
}
import javax.swing.*;
import java.util.*;
import java.text.*;
public class CoffeeDriver
{
public static void sortName(Items arr[]) {
Arrays.sort(arr, Items.NAME_ORDER);
String out="";
for(Items item : arr){
out+= item.toString()+"\n";
}
JOptionPane.showMessageDialog(null, out); }
public static void sortPrice(Items arr[]){
Arrays.sort(arr, Items.PRICE_ORDER);
String out="";
for(Items item : arr){
out+= item.toString()+"\n";
}
JOptionPane.showMessageDialog(null, out);
}
public static void main(String []args)
{
String userSorted;
int x;
Items arr[] = new Items[5];
arr[0] = new Items();
arr[0].setItemName("Coffee");
arr[0].setItemPrice(1.00);
arr[1] = new Items();
arr[1].setItemName("Water");
arr[1].setItemPrice(2.00);
arr[2] = new Items();
arr[2].setItemName("Milk");
arr[2].setItemPrice(1.50);
arr[3] = new Items();
arr[3].setItemName("Bagel");
arr[3].setItemPrice(1.25);
arr[4] = new Items();
arr[4].setItemName("Donut");
arr[4].setItemPrice(0.75);
int selectSort;
JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");
userSorted = JOptionPane.showInputDialog(null, "We have a great list items on our menu. Would you like to see these items sorted by name or by price? (n / p)");
selectSort = Integer.parseInt(userSorted);
if (selectSort == n)
sortPrice(arr);
if (selectSort == p)
sortName(arr);
System.exit(0);