We will write a simulation of someone shopping in a grocery store for produce (f
ID: 3770014 • 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
The required solution is as below,
1) ProduceItem.java
package simulation.groceryStore;
public class ProduceItem {
private String code;
private String name;
private float price;
public ProduceItem(String code, String name, float price){
this.code = code;
this.name= name;
this.price = price;
}
public String getCode(){
return this.code;
}
public void setCode(String code){
this.code = code;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public float getPrice(){
return this.price;
}
public void setPrice(float price){
this.price = price;
}
}
2) Database.java
package simulation.groceryStore;
public class Database {
private ProduceItem[] items;
public Database(ProduceItem[] items){
this.items = items;
}
public String getName(String code){
for(int i =0; i<this.items.length; i++){
String tempCode = this.items[i].getCode();
if(tempCode == code){
return this.items[i].getName();
}
}
return null;
}
public float getPrice(String code){
for(int i =0; i<this.items.length; i++){
String tempCode = this.items[i].getCode();
if(tempCode == code){
return this.items[i].getPrice();
}
}
return (float) 0.0;
}
}
3) The database class is being changed, as now Linkedlist is being used here.
Database.java
package simulation.groceryStore;
import java.util.LinkedList;
public class Database {
private LinkedList<ProduceItem> items;
public Database(LinkedList<ProduceItem> items){
this.items = items;
}
public String getName(String code){
for(int i =0; i<this.items.size(); i++){
ProduceItem item = (ProduceItem)this.items.get(i);
String tempCode = item.getCode();
if(tempCode == code){
return item.getName();
}
}
return null;
}
public float getPrice(String code){
for(int i =0; i<this.items.size(); i++){
ProduceItem item = (ProduceItem)this.items.get(i);
String tempCode = item.getCode();
if(tempCode == code){
return item.getPrice();
}
}
return (float) 0.0;
}
}
4) Fruit.java
package simulation.groceryStore;
public class Fruit extends ProduceItem {
public Fruit(String code, String name, float price) {
super(code, name, price);
// TODO Auto-generated constructor stub
}
}
////Vegetable.java
package simulation.groceryStore;
public class Vegetable extends ProduceItem {
public Vegetable(String code, String name, float price) {
super(code, name, price);
// TODO Auto-generated constructor stub
}
}
5) The required menu bar is created having two menu items.
///UIManager.java
package simulation.groceryStore;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class UIManager {
private static JMenuBar menuBar;
private static JMenu fileMenu;
private static JMenuItem open;
private static JMenuItem quit;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
open = new JMenuItem("Open");
fileMenu.add(open);
quit = new JMenuItem("Quit");
fileMenu.add(quit);
JFrame frame = new JFrame();
frame.setLayout(new GridLayout());
frame.add(menuBar);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.