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

We will write a simulation of someone shopping in a grocery store for produce (f

ID: 3770189 • Letter: W

Question

We will write a simulation of someone shopping in a grocery store for produce (fruits and vegetables). An input file will contain the code number and the weight of the item being purchased. A database will contain the code number, the name of the item (such as “Pineapple”) and the price per pound.

1- Create a class called ProduceItem with three instance variables for the code (String), name (String) and price (float). Include appropriate constructors, get/set methods and overrides of class Object.

2- Create a class for the database. It should contain an array which will store all the ProcudeItems (that will be read in from a file). This class should contain, among others, methods that will return the name and the price of an item with a given code:

String getName(String code);

float getPrice(String code);

The output of the program should be a JFrame (a separate GUI class) that serves as a receipt for the customer. It should contain, one per line, the name, price, weight and total cost of each item, and at the end the total cost of all purchases.

3- Change the representation for the ProduceItems in the database from an array to a linked list of ProduceItems. The linked list should have a first and last pointer and a length as shown in lecture. There shouldn’t be a need to modify how your program interacts with the database. That is, the method signatures remain the same; only the internal representation for storing the ProduceItems changes.

4- Create a class hierarchy for ProduceItem where Fruit and Vegetable are subclasses of ProduceItem. Make ProduceItem abstract. The input file will now indicate if the code number is for a Fruit or a Vegetable by having a ‘F’ or ‘V’ as the first field:

F,4088,Apple,1.69

V,3023,Carrot,0.99

5- Include a File menu with the options Open and Quit. The open option is used to open a transaction file and run the transactions.

6- Include a Database menu with three menu items: Open (let the user choose a file to initialize the database), Display Fruits (use a JFrame to display all the fruits in the database), and Display Vegetables. Write a DatabaseMenuHandler for this menu item.

The transaction file for this project will include PLU codes that are not in the database. Have the database throw an ItemNotFoundException in this case, and include a try/catch block in your program that will catch this exception and pop up a JOptionPane input dialog to get the price for the missing item.

NB: 13 files need to be submited!!!!!!

Explanation / Answer

MyFrame.java

package chegg;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;

import javax.swing.*;

public class MyJFrame {
   public MyJFrame(String myLine, float myTot) {
      
       JMenuBar menuBar = new JMenuBar();
       JMenu file, database;
       file = new JMenu("File");
       database = new JMenu("Database");
       file.setMnemonic(KeyEvent.VK_F);

JMenuItem open = new JMenuItem("Open");
JMenuItem quit = new JMenuItem("Quit");
  
open.setMnemonic(KeyEvent.VK_O);
open.setToolTipText("Open transaction file");
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
   // TODO
}
});
  
quit.setMnemonic(KeyEvent.VK_E);
quit.setToolTipText("Quit application");
quit.addActionListener(new ActionListener() {
          
           @Override
           public void actionPerformed(ActionEvent e) {
               System.exit(0);
              
           }
       });

file.add(open);
file.add(quit);
       menuBar.add(file);
       menuBar.add(database);
      
       DecimalFormat df = new DecimalFormat("0.00");
      
       /* Creating and setting up window */
       JFrame myFrame = new JFrame("ShoppingReceipt");
       myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
       /* Add menu bar */
       myFrame.setJMenuBar(menuBar);
      
       /* width and height */
       myFrame.setSize(310, 310);
      
       /* x and y */
       myFrame.setLocation(210, 110);
      
       myFrame.setLayout(new GridLayout(2, 1));
      
       /* Displaying window */
       JTextArea tA = new JTextArea(30, 30);
       tA.setEditable(false);
      
       JScrollPane sP = new JScrollPane(tA);
       myFrame.getContentPane().add(sP);
       JLabel jL = new JLabel("Shopping Total: $" + df.format(myTot));
       myFrame.getContentPane().add(jL);
       tA.setText(myLine);
      
       myFrame.pack();
       myFrame.setVisible(true);
   }
}

ProduceNode.java

package chegg;

public class ProduceNode {
   ProduceItem data;
   ProduceNode next;

   ProduceNode(ProduceItem data) {
       this.data = data;
       next = null;
   }
}

MyJOptionPane.java

package chegg;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MyJOptionPane {

   public float getPrice(String code) {
       JFrame frame = new JFrame("Price Input");
       String price = JOptionPane.showInputDialog(frame,
               "What's the price of item " + code);
       return Float.parseFloat(price);
   }

}

ItemNotFoundException.java

package chegg;

public class ItemNotFoundException extends Exception {

}

Fruit.java

package chegg;

public class Fruit extends ProduceItem
{
public Fruit(String code,String name,float price)
{
super(code,name,price);
}
}

Database.java

package chegg;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Database {

   ProduceNode start, end;
   int length;

   void addNode(ProduceItem data) {
       if (length == 0) {
           start = new ProduceNode(data);
           end = start;
       } else {
           ProduceNode newNode = new ProduceNode(data);
           newNode.next = start;
           start = newNode;
       }
       length++;
   }

   public static void main(String args[]) throws IOException {
       Database db = new Database();
       String line;
       String word[], code, output = "";
       ProduceItem newItem, arrayOfItems[] = new ProduceItem[100]; // Assuming there are max 100 items
       float weight, totalCost = 0;
       int numberOfItems = 0;
       BufferedReader reader = null;
       /*
       * Create a database from "database.txt" file. Read all items make
       * object of items and store in items[] array
       */
       try {
           reader = new BufferedReader(new FileReader("database.txt"));
           line = reader.readLine();
           while (line != null) {
               word = line.split(",");

               switch (word[0]) {
               case "V":
                   newItem = new Vegetable(word[1].trim(), word[2].trim(),
                           Float.parseFloat(word[3].trim()));
                   arrayOfItems[numberOfItems++] = newItem;
                   break;
               case "F":
                   newItem = new Fruit(word[1].trim(), word[2].trim(),
                           Float.parseFloat(word[3].trim()));
                   arrayOfItems[numberOfItems++] = newItem;
                   break;
               }

               line = reader.readLine();
           }
           reader.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
      
       /*
       * Change representation of ProduceItems from array to linked list
       */
       for (int i = 0; i < numberOfItems; i++) {
           db.addNode(arrayOfItems[i]);
       }
      
       /*
       * Read items purchased by the customer from file "purchase.txt"
       * Calculate total cost of all purchases
       */
       output += "Name Price Weight Cost ";
       try {
           reader = new BufferedReader(new FileReader("transaction.txt"));
           line = reader.readLine();
           while (line != null) {
               word = line.split(",");
               code = word[0];
               weight = Float.parseFloat(word[1]);
               totalCost = totalCost + weight * getPrice(db, code);
               try {
                   output += getName(db, code) + " " + getPrice(db, code) + " "
                           + weight + " " + weight * getPrice(db, code) + " ";
               } catch (ItemNotFoundException e) {
                   MyJOptionPane pane = new MyJOptionPane();
                   float newPrice = pane.getPrice(code);
                   output += "Not defined " + newPrice + " "
                           + weight + " " + weight * newPrice + " ";
               }
               line = reader.readLine();
           }
           reader.close();
           output += " Total Cost: " + totalCost;
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       new MyJFrame(output, totalCost);
   }

   static String getName(Database db, String code) throws ItemNotFoundException {
       ProduceNode temp = db.start;
       while (temp != null) {
           if (temp.data.getCode().equals(code)) {
               return temp.data.getName();
           } else
               temp = temp.next;
       }
       throw new ItemNotFoundException();
   }

   static float getPrice(Database db, String code) {
       ProduceNode temp = db.start;
       while (temp != null) {
           if (temp.data.getCode().equals(code)) {
               return temp.data.getPrice();
           } else
               temp = temp.next;
       }
       return 0;
   }
}

ProduceItem.java

package chegg;

public abstract class ProduceItem {
   private String code;
   private String name;
   private float price;

   ProduceItem() {
   }

   ProduceItem(String code, String name, float price) {
       this.code = code;
       this.name = name;
       this.price = price;
   }

   public String getCode() {
       return code;
   }

   public void setCode(String code) {
       this.code = code;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public float getPrice() {
       return price;
   }

   public void setPrice(float price) {
       this.price = price;
   }
}

Vegetable.java

package chegg;

public class Vegetable extends ProduceItem
{
public Vegetable(String code,String name,float price)
{
super(code,name,price);
}
}

DUE TO LACK OF TIME, I COULD NOT DO THE DATABASE MENU

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