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

A local coffee shop sells a variety of different items shown below to their cust

ID: 3624304 • Letter: A

Question

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. After initializing the array, prompt the user for how they want to see the list – sorted by name or price. Then call the appropriate method.

Be sure your program demonstrates good programming style (appropriate comments, identifier names, indenting, etc).

Explanation / Answer

import java.util.Scanner;

public class CoffeeDriver {
   
    // takes an array of Items and prints them, ordered by name
    public static void sortName(Item[] items) {
        // sort the array by name
        for (int i=0; i<items.length; i++) {
            for (int j=i; j<items.length; j++) {
                if (items[i].getName().compareTo(items[j].getName()) > 0) {
                    Item temp = items[i];
                    items[i] = items[j];
                    items[j] = temp;
                }
            }
        }
        // print the items in order
        for (Item item : items) {
            // calls the item's toString method to print an appropriate String
            System.out.println(item);
        }
    }
   
    // takes an array of Items and prints them, ordered by price
    public static void sortPrice(Item[] items) {
        // sort the array by price
        for (int i=0; i<items.length; i++) {
            for (int j=i; j<items.length; j++) {
                if (items[i].getPrice() > items[j].getPrice()) {
                    Item temp = items[i];
                    items[i] = items[j];
                    items[j] = temp;
                }
            }
        }
        // print the items in order
        for (Item item : items) {
            // calls the item's toString method to print an appropriate String
            System.out.println(item);
        }
    }
   
   
    public static void main(String[] args) {
        Item[] items = new Item [] {
                new Item("Coffee",1.0),
                new Item("Water",2.0),
                new Item("Milk",1.5),
                new Item("Bagel",1.25),
                new Item("Donut",0.75),
        };
        Scanner in = new Scanner(System.in);
        System.out.print("Sort by name or by price? Enter n or p: ");
        String response = in.nextLine();
        if (response.equals("n")) {
            sortName(items);
        }
        if (response.equals("p")) {
            sortPrice(items);
        }
    }

}

-----------------------------------------------------

import java.text.NumberFormat;

public class Item {
    private String name;
    private double price;
   
    public Item(String name, double price) {
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
   
    // returns a String with the name and price, for printing
    public String toString() {
        return this.name + " " + NumberFormat.getCurrencyInstance().format(this.price);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote