Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the scenario: A local coffee shop sells a variety of different items sho

ID: 3622679 • Letter: H

Question

Here is the scenario:
A 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.

Here is what I have so far. Any help will be greatly appreciated.
public class Item
{
//main method (set variables, constructor, set/get)
public static void main (String[] args)
{
String itemName;
double itemPrice;

//constructor
Item (String itemName, double itemPrice)
{
this.itemName = itemName;
this.itemPrice = itemPrice;
}

//set/get methods
public String getitemName()
{
return itemName;
}
public void setitemName (String name)
{
itemName = name;
}
public double getitemPrice()
{
return itemPrice;
}
public void setitemPrice (double price)
{
itemPrice = price;
}
}
}

import java.util.Scanner;
public class CoffeeDriver
{
//main method (create & initialize Item array, prompts for sort type, calls for sort type method)
public static void main (String[] args)
{
String[] itemName = {"Coffee, Water, Milk, Donut, Bagel"};
double[] itemPrice = {1.00, 2.00, 1.50, 0.75, 1.25};
Scanner myScanner = new Scanner(System.in);

String input;
System.out.println ("Welcome to Wings Coffee Shop");
System.out.println ("We have a great list of tasty items on our menu.");
System.out.println ("Would you like to see these items sorted by");
System.out.println ("name or by price? (n/p): ");
input = myScanner.next();

//exit program
System.exit(0);
}

//method to sort by item name and display
public static void sortName (String[] array)
{
for (int i = 0; i < array.length; i++)
{
if (input = "n")
System.out.println (itemName[i].toString());
}
}

//method to sort by item price and display
public static void sortPrice (double[] array)
{
for (int i = 0; i < array.length; i++)
{
if (input = "p")
System.out.println (itemPrice[i].toString());
}
}
}

Explanation / Answer

// save as CoffeeDriver.java and run.

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;




class Item
{
//main method (set variables, constructor, set/get)
String itemName;
double itemPrice;

//constructor
Item(String itemName, double itemPrice)
{
this.itemName = itemName;
this.itemPrice = itemPrice;
}

//set/get methods
public String getitemName()
{
return itemName;
}
public void setitemName (String name)
{
itemName = name;
}
public double getitemPrice()
{
return itemPrice;
}
public void setitemPrice (double price)
{
itemPrice = price;
}
}



public class CoffeeDriver
{
//main method (create & initialize Item array, prompts for sort type, calls for sort type method)
public static void main (String[] args)
{
String[] itemName = {"Coffee", "Water", "Milk", "Donut", "Bagel"};
double[] itemPrice = {1.00, 2.00, 1.50, 1.25, 0.75};
HashMap<String, Double> CoffeeItems = new HashMap<String, Double>();
HashMap<Double, String> PriceItems = new HashMap<Double, String>();
for(int i=0; i<itemName.length; i++)
{
     CoffeeItems.put(itemName[i], itemPrice[i]);
     PriceItems.put(itemPrice[i], itemName[i]);
}

Scanner myScanner = new Scanner(System.in);

String input;
System.out.println ("Welcome to Wings Coffee Shop");
System.out.println ("We have a great list of tasty items on our menu.");
System.out.println ("Would you like to see these items sorted by");
System.out.println ("name or by price? (n/p): ");
input = myScanner.next();
//method to sort by item name and display

if(input.equals("n"))
    Arrays.sort(itemName);

//method to sort by item price and display

else if(input.equals("p"))
    Arrays.sort(itemPrice);

for(int i=0; i<itemName.length; i++)
{
    if(input.equals("n"))
       System.out.println(itemName[i] + " " + CoffeeItems.get(itemName[i]) );
    if(input.equals("p"))
      System.out.println(PriceItems.get(itemPrice[i]) + " " + itemPrice[i] );


}


//exit program
System.exit(0);
}







}