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

1. Include a File menu with the options Open and Quit. The open option is used t

ID: 3769085 • Letter: 1

Question

1.

Include a File menu with the options Open and Quit. The open option is used to open

a transaction file and run the transactions.

2.

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.

3.

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.

this is the program i have to modify

ProduceItem.java

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;
   }
}

Database.java

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;
       float weight, price, totalCost = 0;
       int totalItems = 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[0], word[1],Float.parseFloat(word[2]));
                   db.addNode(newItem);
                   break;
               case "F":
                   newItem = new Fruit(word[0], word[1],Float.parseFloat(word[2]));
                   db.addNode(newItem);
                   break;
               }
            
               totalItems++;
               line = reader.readLine();
           }
           reader.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       /*
       * 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);
               output += getName(db,code) + " " + getPrice(db,code) + " " + weight
                       + " " + weight * getPrice(db,code) + " ";
               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) {
       ProduceNode temp = db.start;
       while(temp != null)
       {
           if (temp.data.getCode().equals(code)) {
               return temp.data.getName();
           }
           else
               temp=temp.next;
       }
       return "";
   }

   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;
   }
}
class ProduceNode
{
   ProduceItem data;
   ProduceNode next;
   ProduceNode(ProduceItem data)
   {
       this.data = data;
       next = null;
   }
}

MyJFrame.java

import java.awt.*;
import java.text.DecimalFormat;

import javax.swing.*;

public class MyJFrame
{
public MyJFrame(String myLine, float myTot)
{
   DecimalFormat df = new DecimalFormat("0.00");
/* Creating and settingup window */
JFrame myFram = new JFrame("ShoppingReceipt");
myFram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFram.setSize(310,310);
/* width and height */
myFram.setLocation(210,110);
/* x and y */
myFram.setLayout(new GridLayout(2,1));
JTextArea tA = new JTextArea(30, 30);
tA.setEditable(false);
JScrollPane sP = new JScrollPane(tA);
myFram.getContentPane().add(sP);
JLabel jL = new JLabel("Shopping Total: $" + df.format(myTot));
myFram.getContentPane().add(jL);
tA.setText(myLine);
/* Displaying window */
myFram.pack();
myFram.setVisible(true);
}
}

Vegetable.java

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

Fruit.java

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

database.txt

F, 4019,APPLES,0.99
F, 4218,APRICOTS,3.49
F, 4771,AVOCADOS,2.59
F, 4011,BANANAS,.69
F, 4045,CHERRIES,4.99
F, 4263,DATES,3.99
F, 4027,GRAPEFRUIT,1.09
F, 4637,GRAPES,2.76
F, 4053,LEMONS,3.45
F, 4319,MELON,1.99
F, 4377,NECTARINE,3.69
F, 4012,ORANGES,2.49
F, 4037,PEACHES,2.99
F, 4026,PEARS,1.99
F, 4029,PINEAPPLE,2.59
F, 4041,PLUMS,3.49
V, 3332,SPINACH-BABY,2.19
V, 3335,TOMATOES,1.49
V, 4061,LETTUCE,1.89
V, 3320,CAULIFLOWER,0.99
V, 3404,MUSHROOMS,6.89
V, 4070,CELERY,1.25
V, 4072,POTATO,1.29
V, 4082,ONIONS-RED,0.69

transaction.txt

Explanation / Answer

I have done some changes in Database.java and included two new files MyJOptionPane.java and ItemNotFoundException.java. If any doubts let me know

Database.java

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;
       float weight, price, totalCost = 0;
       int totalItems = 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()));
                   db.addNode(newItem);
                   break;
               case "F":
                   newItem = new Fruit(word[1].trim(), word[2].trim(),
                           Float.parseFloat(word[3].trim()));
                   db.addNode(newItem);
                   break;
               }

               totalItems++;
               line = reader.readLine();
           }
           reader.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       /*
       * 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;
   }
}

class ProduceNode {
   ProduceItem data;
   ProduceNode next;

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

MyJOptionPane.java

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

public class ItemNotFoundException extends Exception {

}