you are asked to write a Java application that can be used to keep track of thes
ID: 3623621 • Letter: Y
Question
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.
Be sure your program demonstrates good programming style (appropriate comments, identifier names, indenting, etc).
here are my codes it does not work properly does it have to do with the file path?
public class Item
{
//private instance variables
private String itemName;
private double price;
public String getItemName(){
return this.itemName;
}
public void setItemName(String itemName){
this.itemName = itemName;
}
public double getPrice(){
return this.price;
}
public void setPrice(double itemName){
this.price = price;
}
public String toString(){
return this.getItemName() + " $" + this.getPrice();
}
//constructor
public Item(String itemName, double price){
this.itemName = itemName;
this.price = price;
}
}//<--------------------------end of program.
_____________________________________________________________________________
import java.util.*;
public class CoffeeDriver
{
//<----------------------------------------------------------This method will sort the array of items by name
//<----------------------------------------------------------and then print them to the screen
public void sortName(Item[] itemArray){
Arrays.sort(itemArray, new ItemNameComparator());
printItems(itemArray);
}
//<---------------------------------------------------This method will sort the array of items by price
//<---------------------------------------------------------Also then print them to the screen
public void sortPrice(Item[] itemArray){
Arrays.sort(itemArray, new ItemPriceComparator());
printItems(itemArray);
}
//<-----------------------------------------------This method prints the array of items to the screen
private void printItems(Item[] itemArray){
for (int x = 0; x < itemArray.length; x++){
System.out.println(itemArray[x]);
}
}
//<-----------------------------------------------Will be the comparators used to do the perform the sorting
class ItemNameComparator implements Comparator<Item>
{
public int compare(Item a, Item b){
return a.getItemName().compareTo(b.getItemName());
}
}
class ItemPriceComparator implements Comparator<Item>
{
public int compare(Item a, Item b){
return ((new Double(a.getPrice())).compareTo(new Double(b.getPrice())));
}
}
public static void main(String[] args)
{
int numItems = 5;
//<-------------------------------------------This array will hold the items
Item[] itemArray = new Item[numItems];
//<---------------------------------------------This will Initialize the items array
itemArray[0] = new Item("The price for: Coffee", 1.00);
itemArray[1] = new Item("The price for: Water", 2.00);
itemArray[2] = new Item("The price for: Milk", 1.50);
itemArray[3] = new Item("The price for: Bagel", 1.25);
itemArray[4] = new Item("The price for: Donut", 0.75);
CoffeeDriver cf = new CoffeeDriver();
cf.sortName(itemArray);
System.out.println();
cf.sortPrice(itemArray);
}
}//<----------------------------------End of program.
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.