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

/** * This program will act as a grocery store app. * * The purpose of this proj

ID: 3706315 • Letter: #

Question

/**

* This program will act as a grocery store app.

*

* The purpose of this project is to have you create a linked list, add items to it, add computations to

* fields within the nodes, remove a node by traversing it to find a match, then eliminate the node,

* traverse the linked list to add up items stored within the objects' instance variables (how much

* is owed for the items and for tax), traverse the list to print out a neat receipt.

*

* This project requires that the grocery information be stored in a singly linked list

*  and that you use an inner class to define the LinkedListNode

* It also requires:

*  That you begin with an empty linked list

*  That you have the user enter data as many times as they wish to fill in the linked list

*  See the exact wording in the sample output for these features:

*      The method invocation to add the items is included in main

*      The additions are performed within a method and not in main

*      The linked list will be displayed between each addition of data

*      That you allow the user to remove items from the linked list

*      The method invocation to remove items is included in main

*      The removal of items is performed in a method and not in main

*      The linked list will be displayed between each addition of data

*      A message stating that the item is not found is included when appropriate

*  The display of data is done by a display method, not as the values are read in or removed

*

* The program will request items that you will purchase: Item name, Price, taxable (y or n).

* You will enter at least 7 items. (Keep adding in more items until the user wishes to check out

* as in: Enter the item name ( or "!" to Exit)

*

* Before you are done, the app will ask if you wish to remove items from your list. You will be able to remove

* at least one middle item and the LAST item from the list.

* You are not required to remove the first item from the list (but I highly recommend that you be able to do this)

* You are not required to empty out the list entirely (but I highly recommend that you be able to do this)

*

* At least match this output. Yours can be nicer. But a formatted receipt is required: lining up the items

* and the Subtotal, Total Tax, and Total Owed IS A MUST.

*

* Your program will print out a neat formatted receipt with

* A header

* Each item, the price for each item, and the tax for each item (USE a formatted toString)

* The subtotal of the prices,

* The tax owed.

* The total owed.

*

* You may start a new class or use this one. This is just a skeleton to get you started!

*

* @author ( Your name)

* @version (Project 8, DUE DATE: Thursday, April 12 by 11:59 pm.)

*/

import java.util.Scanner;

public class Project8_Lname

{

   /****************************************************************

     * intro()

     * The introduction will be displayed to the user

     */

    public static void intro()

    {

       System.out.println("Welcome to the grocer app.");

        System.out.println("As you go through the store, enter your item details.");

       System.out.println("Your total won't be a shock when you get to the checkout.");

       System.out.println(" For each item: Enter the item name, the price, and y if it is taxed or n if it is not.");

       System.out.println("You will also get a chance to see the subtotal while you shop.");

    }

    public static String getString(String str)

    {

        Scanner keyboard = new Scanner(System.in);

       

        System.out.println(" Enter the item name ( or "!" to Exit)");

       System.out.print(str);

        return keyboard.nextLine();

    }

   

    public static double getDouble(String str)

    {

        // Do FIX this so that it can't read in erroneous input

        Scanner keyboard = new Scanner(System.in);

       

       System.out.print(str);

        return keyboard.nextDouble();       

    }

   

    public static boolean getTaxable()

    {

        // Read in the y or n ...  Make that true or false!

        return false;

    }  

   /****************************************************************

     * main

     */                   

    public static void main(String [] args)

    {      

        boolean done = false;

        LinkedListNode head = null;

       

        intro();

       

        // Fill first node:

        String temp = getString("Item: ");

        if (!temp.equals("!"))

            head = new LinkedListNode(temp, getDouble("Price: "), getTaxable(), null);

       

        // What's in the first node?

       System.out.println("The first node has as contents: "+head);

       

        // Add more nodes to the Linked List (Until the user wishes to end the program with a "!")

       

       

        // Remove items section begins here

       

        // Print out the final receipt

       

             

       System.out.println("Thank you for using <Your name>'s app.");

      

    }

}

/**

Welcome to the grocer app.

As you go through the store, enter your item details.

Your total won't be a shock when you get to the checkout.

For each item: Enter the item name, the price, and y if it is taxed or n if it is not.

You will also get a chance to see the subtotal while you shop.

Enter the item name ( or "!" to Exit)

Item: Milk

Price: 3.99

Taxable? (Y for yes or N for no) n

The first node has as contents:

           Milk     3.99   0.00

Enter the item name ( or "!" to Exit)

Item: Bread

Price: 2.79

Taxable? (Y for yes or N for no) n

           Item    Price    Tax

          Bread     2.79   0.00

           Milk     3.99   0.00

             Subtotal:$    6.78

             Total tax:$    0.00

            Total Owed:$    6.78

Enter the item name ( or "!" to Exit)

Item: Kleenex

Price: 3.89

Taxable? (Y for yes or N for no) y

           Item   Price     Tax

        Kleenex     3.89   0.23

          Bread     2.79   0.00

           Milk     3.99   0.00

             Subtotal:$   10.67

             Total tax:$    0.23

            Total Owed:$   10.90

Enter the item name ( or "!" to Exit)

Item: Chicken Breasts

Price: 7.99

Taxable? (Y for yes or N for no) n

           Item    Price    Tax

Chicken Breasts    7.99    0.00

        Kleenex     3.89   0.23

          Bread     2.79   0.00

           Milk     3.99   0.00

              Subtotal:$   18.66

             Total tax:$    0.23

            Total Owed:$   18.89

Enter the item name ( or "!" to Exit)

Item: Ink pens

Price: 5.79

Taxable? (Y for yes or N for no) y

           Item    Price    Tax

       Ink pens     5.79   0.35

Chicken Breasts    7.99    0.00

        Kleenex     3.89   0.23

          Bread     2.79   0.00

           Milk     3.99   0.00

             Subtotal:$   24.45

             Total tax:$    0.58

            Total Owed:$   25.03

Enter the item name ( or "!" to Exit)

Item: !

           Item    Price    Tax

       Ink pens     5.79   0.35

Chicken Breasts    7.99    0.00

        Kleenex     3.89   0.23

          Bread     2.79   0.00

           Milk     3.99   0.00

             Subtotal:$   24.45

             Total tax:$    0.58

            Total Owed:$   25.03

=================================================================

        Removal of items module

=================================================================

Enter the item name to remove ( or "!" to Exit)

Remove Item: Kleenex

Removing Kleenex

           Item    Price    Tax

       Ink pens     5.79   0.35

Chicken Breasts    7.99    0.00

          Bread     2.79   0.00

           Milk     3.99   0.00

              Subtotal:$   20.56

             Total tax:$    0.35

            Total Owed:$   20.91

Enter the item name to remove ( or "!" to Exit)

Remove Item: Milk

Removing Milk

           Item    Price    Tax

       Ink pens     5.79   0.35

Chicken Breasts    7.99    0.00

          Bread     2.79   0.00

             Subtotal:$   16.57

             Total tax:$    0.35

            Total Owed:$   16.92

Enter the item name to remove ( or "!" to Exit)

Remove Item: Aspirin

           Item    Price    Tax

       Ink pens     5.79   0.35

Chicken Breasts    7.99    0.00

          Bread     2.79   0.00

             Subtotal:$   16.57

             Total tax:$    0.35

            Total Owed:$   16.92

Enter the item name to remove ( or "!" to Exit)

Remove Item: !

The final receipt:

           Item    Price    Tax

       Ink pens     5.79   0.35

Chicken Breasts    7.99    0.00

          Bread     2.79   0.00

             Subtotal:$   16.57

             Total tax:$    0.35

            Total Owed:$   16.92

Thank you for using <Your name>'s app.

*/

Explanation / Answer

CODING:

import java.math.RoundingMode;

import java.text.DecimalFormat;

import java.util.Scanner;

public class Project8_Lname {

   /****************************************************************

   * intro()

   *

   * The introduction will be displayed to the user

   */

   private static DecimalFormat decimalFormat = new DecimalFormat("#.00");

   public static void intro() {

       System.out.println("Welcome to the grocer app.");

       System.out.println("As you go through the store, enter your item details.");

       System.out.println("Your total won't be a shock when you get to the checkout.");

       System.out.println(" For each item: Enter the item name, the price, and y if it is taxed or n if it is not.");

       System.out.println("You will also get a chance to see the subtotal while you shop.");

   }

   public static String getString(Scanner keyboard, String str) {

       System.out.println(" Enter the item name ( or "!" to Exit)");

       System.out.print(str);

       return keyboard.nextLine();

   }

   public static double getDouble(Scanner keyboard, String str) {

       System.out.print(str);

       double dbl = 0.0d;

       try {

           dbl = Double.parseDouble(keyboard.nextLine());

       } catch (NumberFormatException e) {

           System.out.println("Wrong Input ! Please enter appropriate number.");

           getDouble(keyboard, str);

       }

       return dbl;

   }

   public static boolean getTaxable(Scanner keyboard, String str) {

       System.out.print(str);

       String input = keyboard.nextLine();

       return input.equalsIgnoreCase("y");

   }

   /****************************************************************

   * main

   */

   public static void main(String[] args) {

       Scanner keyboard = new Scanner(System.in);

  

       LinkedListNode head = null;

       LinkedListNode curPointer = null;

       intro();

       decimalFormat.setRoundingMode(RoundingMode.UP);

      

       // Fill first node:

       String temp = getString(keyboard, "Item: ");

       double price = 0.0d;

       boolean isTaxable = false;

       if (!temp.equals("!")) {

           price = getDouble(keyboard, "Price: ");

           isTaxable = getTaxable(keyboard, "Taxable? (Y for yes or N for no): ");

           head = new LinkedListNode(temp, price, isTaxable, null);

           if(isTaxable) {

               head.setTax(head.getPrice() * 0.05);

           }

           curPointer = head;

       }

      

       // What's in the first node?

       System.out.println("The first node has as contents: ");

       System.out.print(head.getName() + " " + head.getPrice() + " " + head.getTax());

       // Add more nodes to the Linked List (Until the user wishes to end the

       // program with a "!")

       String item = "";

       while(!(item = getString(keyboard, "Item: ")).equals("!")){

           //item = getString(keyboard, "Item: ");

           LinkedListNode next = readItem(keyboard, item, curPointer);

           displayReceipt(head);

           curPointer = next;

       };

       item = "";

      

       // Remove items section

       while(!(item = getString(keyboard, "Item: ")).equals("!")) {

           System.out.println(" Enter the item name to remove ( or "!" to Exit)");

           item = getString(keyboard, "Remove Item: ");

           if(item.equalsIgnoreCase(head.getName())) {

               LinkedListNode newHead = head.getNext();

               head = newHead;

           } else {

               removeItem(keyboard, head, item);  

           }

          

           displayReceipt(head);

       };

      

       // Print out the final receipt

       displayReceipt(head);

      

       System.out.println("Thank you for using JJ's Grocery Store app.");

       keyboard.close();

   }

  

   private static LinkedListNode readItem(Scanner keyboard, String item, LinkedListNode curPointer) {

       double price = getDouble(keyboard, "Price: ");

       boolean isTaxable = getTaxable(keyboard, "Taxable? (Y for yes or N for no): ");

       LinkedListNode node = new LinkedListNode(item, price, isTaxable, null);

       if(isTaxable) {

           node.setTax(node.getPrice() * 0.05);

       }

       curPointer.setNext(node);

       return node;

   }

  

   private static void removeItem(Scanner keyboard, LinkedListNode head, String item) {

       System.out.println("Removing "+item);

       head.removeItem(item);  

   }

  

   private static void displayReceipt(LinkedListNode head) {

       LinkedListNode current = head;

      

       double subtotal = 0.0d;

       double totalTax = 0.0d;

       double totalOwed = 0.0d;

       System.out.println("Item Price Tax");

       do {

           System.out.print(current.getName() + " ");

           System.out.print(decimalFormat.format(current.getPrice()) + " ");

           subtotal += current.getPrice();

           System.out.println(decimalFormat.format(current.tax));

           totalTax += current.getTax();

          

           current = current.getNext();

       }while (current!= null);

      

       totalOwed = subtotal + totalTax;

      

       System.out.println("Subtotal: $"+decimalFormat.format(subtotal));

       System.out.println("Total Tax: $"+decimalFormat.format(totalTax));

       System.out.println("Total Owed: $"+decimalFormat.format(totalOwed));

   }

  

   private static class LinkedListNode {

       private String name;

       private double price;

       private boolean isTaxable;

       private double tax;

       private LinkedListNode next;

      

       public LinkedListNode() {  

       }

       public LinkedListNode(String name, double price, boolean isTaxable, LinkedListNode next) {

           this.name = name;

           this.price = price;

           this.isTaxable = isTaxable;

           this.next = next;          

       }

       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;

       }

       public boolean isTaxable() {

           return isTaxable;

       }

       public void setTaxable(boolean isTaxable) {

           this.isTaxable = isTaxable;

       }

       public double getTax() {

           return tax;

       }

       public void setTax(double tax) {

           this.tax = tax;

       }

       public LinkedListNode getNext() {

           return next;

       }

       public void setNext(LinkedListNode next) {

           this.next = next;

       }

      

       public void removeItem(String item) {

           LinkedListNode current = this;

           LinkedListNode prev = null;

           while(!current.getName().equalsIgnoreCase(item)) {

               prev = current;

               current = current.next;

           }

           if(prev != null) {

               prev.next = current.next;

           }

       }

   }

}