I am trying to sort the array by name or price depending on input. It is saying that the method sort is not applicable and that it cannot be cast. I have looked and can not figure it out or find information on it that is helpful.
This is for a homework assignment, so any type of brief help is welcome.
This is for a homework assignment, so any type of brief help is welcome.
/*
*/
public class Item {
public static String name;
public static double price;
public void setName(String someItem){
name = someItem;
}
public void setPrice(double somePrice){
price = somePrice;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
/*
*/
import java.util.*;
public class CoffeeDriver {
public static Item[] arr = new Item[5];
public static void sortName(){
Arrays.sort(arr, Item.name);
}
public static void sortPrice(){
Arrays.sort(arr, Item.price);
}
public static void main (String[] args){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
arr[0] = new Item();
arr[0].setName("Coffee");
arr[0].setPrice(1.00);
arr[1] = new Item();
arr[1].setName("Water");
arr[1].setPrice(2.00);
arr[2] = new Item();
arr[2].setName("Milk");
arr[2].setPrice(1.50);
arr[3] = new Item();
arr[3].setName("Bagel");
arr[3].setPrice(1.25);
arr[4] = new Item();
arr[4].setName("Donut");
arr[4].setPrice(0.75);
boolean exit = false;
do{
System.out.println("Welcome to Wings Coffee Shop. We have a great list of items on our menu.\n Would you like to see these items sorted by name or by price?\n 1 for name, 2 for price, 3 to exit ");
int choice = input.nextInt();
switch(choice) {
case 1:
sortName();
break;
case 2:
sortPrice();
break;
case 3:
exit = true;
break;
default:
System.out.println("Invalid");
}
}
while(!exit);
}
}