1. Include a File menu with the options Open and Quit. The open option is used t
ID: 3768729 • 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
package net.codejava.swing;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
/**
* JTree basic tutorial and example
* @author wwww.codejava.net
*/
public class TreeExample extends JFrame
{
private JTree tree;
private JLabel selectedLabel;
public TreeExample()
{
//create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
//create the child nodes
DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode("Vegetables");
vegetableNode.add(new DefaultMutableTreeNode("Capsicum"));
vegetableNode.add(new DefaultMutableTreeNode("Carrot"));
vegetableNode.add(new DefaultMutableTreeNode("Tomato"));
vegetableNode.add(new DefaultMutableTreeNode("Potato"));
DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode("Fruits");
fruitNode.add(new DefaultMutableTreeNode("Banana"));
fruitNode.add(new DefaultMutableTreeNode("Mango"));
fruitNode.add(new DefaultMutableTreeNode("Apple"));
fruitNode.add(new DefaultMutableTreeNode("Grapes"));
fruitNode.add(new DefaultMutableTreeNode("Orange"));
//add the child nodes to the root node
root.add(vegetableNode);
root.add(fruitNode);
//create the tree by passing in the root node
tree = new JTree(root);
ImageIcon imageIcon = new ImageIcon(TreeExample.class.getResource("/leaf.jpg"));
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(imageIcon);
tree.setCellRenderer(renderer);
tree.setShowsRootHandles(true);
tree.setRootVisible(false);
add(new JScrollPane(tree));
selectedLabel = new JLabel();
add(selectedLabel, BorderLayout.SOUTH);
tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
selectedLabel.setText(selectedNode.getUserObject().toString());
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTree Example");
this.setSize(200, 200);
this.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TreeExample();
}
});
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.