For my program I had to create two classes one named Item.java and another one called CoffeeDriver.java. I need to display and sort the following:
Item Name Price
Coffee $1.00
Water $2.00
Milk $1.50
Bagel $1.25
Donut $0.75
I have the class and array created to display this but I am currently have trouble with sorting this array. Any help will be greatly appreciated.
Heres my code:
package wings.coffee.shop;
//Class named Item
public class Item {
private String name; //String instance to hold name
private double price; //Double instance to hold price
//Constructor to return values
public Item(String itemName, double itemPrice)
{
this.name = itemName;
this.price = itemPrice;
}
//Get method for name
public String getName()
{
return this.name;
}
//Set method for name
public void setName(String Itemname)
{
this.name = Itemname;
}
//Get method for price
public double getPrice()
{
return this.price;
}
//Set method for price
public void setPrice(double Itemprice)
{
this.price = Itemprice;
}
}
package wings.coffee.shop;
import java.util.Scanner; //Needed for user input
//Class named CoffeeDriver
public class CoffeeDriver {
//Default main class
public static void main(String[] args) {
String ans;
Scanner keyBoard = new Scanner(System.in); //Allows user input
//Array to hold all of the items and prices
Item Item[] = new Item[5];
Item[0] = new Item("Coffee", 1.00);
Item[0].setName("Coffee");
Item[0].setPrice(1.00);
Item[1] = new Item("Water ", 2.00);
Item[1].setName("Water ");
Item[1].setPrice(2.00);
Item[2] = new Item("Milk ", 1.50);
Item[1].setName("Milk ");
Item[1].setPrice(1.50);
Item[3] = new Item("Bagel ", 2.00);
Item[1].setName("Bagel ");
Item[1].setPrice(2.00);
Item[4] = new Item("Donut ", 0.75);
Item[1].setName("Donut ");
Item[1].setPrice(0.75);
//Welcome message and directions
System.out.println("Welcome to Wings Coffee Shop! \n"
+ "Heres a unsorted list of our menu: ");
System.out.println("Item Name" + " Price");
//For loop to print the unsorted values of the array
for (int i = 0; i < Item.length; ++i)
{
System.out.print( Item[i].getName());
System.out.print( " ");
System.out.print(Item[i].getPrice());
System.out.print('\n');
}
//Statement to ask the use r how they would like to sort
System.out.println ("Would you like to see these items sorted by "
+ "name or price? (n/p): ");
ans = keyBoard.nextLine(); //Input from user on which sort to view
for (int i = 0; i < Item.length; ++i)
{
sortPrice(Item[i].getPrice(), Item.length);
}
}
//Method to sort by price
//public static void sortPrice(double Item[] )
//{
public static void sortPrice(double item[], int size){
double temp;
for(int i=0;i<size;i++)
for(int j=0;j<size-i-1;j++)
if(item[j]>item[j+1])
{
temp = item[j];
item[j]=item[j+1];
item[j+1]=temp;
}}}
The error that I am recieving is in the call to my sort function in my main method. it is saying double cannot be converted to double[].
MODERATOR: code block fixed: indent total block of your code one extra tab by selecting it after paste and clicking TAB key