public class Items
{
// * A String instance variable to hold the item name
private String itemName;
// * A double instance variable to hold the price
private double itemPrice;
// * A constructor that takes a String and double to initialize the instance variables
public Items (String itemName, double itemPrice)
{
this.itemPrice = itemPrice;
this.itemName = itemName;
}
public Items()
{
}
// * A get and set method for each instance variable
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;
}
}
This is my tester class but it doesn't display the output correctly. Any help would be appreciated. I am getting confused and I know it has to be something I overlooked and can't find a good answer for on Google. I hope you guys can help me out.
// Once you have this class created, you write a second class named CoffeeDriver. This class has the following methods:
public class CoffeeDriver
{
// sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen
public static void sortName(Items arr[])
{
// sorts array by name and displays the items and prices
int in, out;
for(out=1; out< arr.length; out++) // out is dividing line
{ Items temp = arr[out]; // remove marked person
in = out; // start shifting at out
while(in>0 && // until smaller one found,
arr[in - 1].getName().compareTo(temp.getName())>0)
{ arr[in] = arr[in-1]; // shift item to the right
--in; // go left one position
}
arr[in] = temp; // insert marked item
JOptionPane.showMessageDialog(null,(arr[in]));
} // end for
}
// sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen
public static void sortPrice(Items arr[])
{
// sorts the array by price and displays the list
int a,b;
Items temp;
for (a=0; a < arr.length;++a)
for (b=0; b< arr.length;++b)
if (arr[b].getPrice() > arr[b +1].getPrice())
{
temp = arr[b];
arr[b] = arr[b +1];
arr[b+1] = temp;
JOptionPane.showMessageDialog(null, (arr[b]));
}
}
// main - It creates an array of Item objects using the data above to set each Item's information.
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, "Please indicate how you would like to sort the menu. To sort by price, press 1. To sort by name, press 2. To exit the program, press 3.");
selectSort = Integer.parseInt(userSorted);
// insert do while loop here
// if (selectSort == 1);
// sortPrice(arr);
if (selectSort == 2);
sortName(arr);
if (selectSort == 3);
JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!");
System.exit(0);
}
}
Thanks


Sign In
Create Account

Back to top









