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

You will interact with the user to get details of BakedGood items (name and pric

ID: 3820375 • Letter: Y

Question

You will interact with the user to get details of BakedGood items (name and price) that the user wants to sell. You will use the BakedGood items and an array or ArrayList to keep track of these items. When the user wants to buy an item, you will display all of the available items (or an error if no items have been made yet). The user can make and eat as many items as they like, and when they quit you will display their full bill. Below is a basic run-through of the program, illustrating the output your program should provide: (Note: the underlined/green items are what the user will enter, your program should not print these items). A few items items to consider: You need to use the BakedGood class that I am providing to you. This class is complete except for one method: toPrettyString. This is the method you will call from the main method to output each available BakedGood to the user and that format is shown in a comment in the code. You will have to complete this method for your code to work. You may choose alter this class as much as you like as long as the input/output remains intact, but do not change the class name. 2. Once an item is made, you can assume an infinite number of portions are available, so you do not need to remove anything from the list of available BakedGoods. 3. If the user tries to eat a BakedGood before anything is made, you will need to display the error: “There is no food to eat! You have to make some first.”

This is a sample of what the output should be, the bracketed letters are what the user should enter.

Welcome to the Hungry Bakery! Would you like to (m)ake or (e)at a BakedGood, or (q)uit? [m]

What is the name of the BakedGood? [Cookie]

What is the cost of the BakedGood? [10.50]

Would you like to (m)ake or (e)at a BakedGood, or (q)uit? [m]

What is the name of the BakedGood? [pie]

What is the cost of the BakedGood? [35.00]

Would you like to (m)ake or (e)at a BakedGood, or (q)uit? [ e]

Which item do you want to eat? 0) Cookie - 10.5 1) pie - 35.0 [1]

Bill so far: 35.0 Would you like to (m)ake or (e)at a BakedGood, or (q)uit? [q]

Thank you for stopping by! Your bill is: 35.0

This is the code I have so far I'm very confused on where to put the main between the two lines of == signs and all of my code will go inbetween the two lines of == signs.

/* * The BakedGood Class contains two data elements: the name of the BakedGood and * it's price. The class has a toPrettyString method which must be written by * the student for the project to work. * * /

public class BakedGood {

private String name; private double price;

/*----------------------------------------------------------------------- *

Constructors There are two constructors for a BakedGood. The first one * takes in no parameters. It sets the name to "" and the price to $0.00. * The second one takes in two parameters and sets the name and price * accordingly. */

BakedGood() {

name = ""; price = 0.0;

}

BakedGood(String name, double price) {

this.name = name; this.price = price;

}

// ============================================ // Getters and Setters

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;

}

// ============================================ // Project 5 TODO: Write this method // This method takes in an int that needs to be printed at the start of the // line like in this example below: // 3) Cake - 1.2 // Note: The number of digits after the decimal place does not matter.

public String toPrettyString(int index) {

return "";

}

}

Explanation / Answer


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


/**
*
* @author Sam
*/
public class BakedGood {

    private String name;
    private double price;

    /*----------------------------------------------------------------------- *

     Constructors There are two constructors for a BakedGood. The first one * takes in no parameters. It sets the name to "" and the price to $0.00. * The second one takes in two parameters and sets the name and price * accordingly. */
    BakedGood() {

        name = "";
        price = 0.0;

    }

    BakedGood(String name, double price) {

        this.name = name;
        this.price = price;

    }

// ============================================ // Getters and Setters
    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;

    }

// ============================================
// Project 5 TODO: Write this method
// This method takes in an int that needs to be printed at the start of the
// line like in this example below:
// 3) Cake - 1.2
// Note: The number of digits after the decimal place does not matter.
    public String toPrettyString(int index) {
        return index + ") " + name + " - " + price;
    }

}

class BakedGoodVendor { //driver class

    private final ArrayList<BakedGood> list;
    private double bill;

    public BakedGoodVendor() { //default constructor
        list = new ArrayList<>(10);
        bill = 0;
    }

    public void addToList(String name, double price) { //add new item to list
        list.add(new BakedGood(name, price));
    }

    public double addToBill(int index) { //add item to bill
        bill += list.get(index).getPrice();
        return bill;
    }

    public String listToString() { //convert the list to string
        String out = "";
        for (int i = 0; i < list.size(); i++) {
            out = out + list.get(i).toPrettyString(i) + " ";
        }
        return out;
    }

    public static void main(String[] args) throws IOException {
        String name, price;
        double bill = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BakedGoodVendor vendor = new BakedGoodVendor();
        System.out.println("Welcome to the Hungry Bakery!");
        boolean flag = true;
        do {
            System.out.println("Would you like to (m)ake or (e)at a BakedGood, or (q)uit? ");
            switch (br.readLine()) {
                case "m":
                    System.out.print("What is the name of the BakedGood?");
                    name = br.readLine();
                    System.out.print("What is the cost of the BakedGood? ");
                    price = br.readLine();
                    vendor.addToList(name, Double.parseDouble(price));
                    break;
                case "e":
                    System.out.print("Which item do you want to eat?" + vendor.listToString());
                    bill += vendor.addToBill(Integer.parseInt(br.readLine()));
                    System.out.print("Bill so far: " + bill+" ");
                    break;
                case "q":
                    flag = false;
                default:
                    break;
            }
        } while (flag);
        System.out.println("Thank you for stopping by! Your bill is: " + bill);
    }
}

Here you go champ. I have coded
1. public String toPrettyString(int index)
2. class BakedGoodVendor //driver class

I have commented the parts that were complex to understand. I hope you like the code. In case you dont understand the code, you may comment below. I shall try my best to solve your issue.

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