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

Project We will write a simulation of someone shopping in a grocery store for pr

ID: 3770006 • Letter: P

Question

Project

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.

Explanation / Answer

Store.java

import java.util.HashSet;

public class Store {

private HashSet<Register> regs;

public Store(){
this.regs = new HashSet<Register>();

for(int i = 0;i<=10; i++){
regs.add(new Register());
}
}

private Register leastFullRegister() {

int minLineSize = Integer.MAX_VALUE;
Register leastFullRegister = null;

for ( Register r : regs ) {
if ( r.lineSize() < minLineSize ) {
minLineSize = r.lineSize();
leastFullRegister = r;
}
}
return leastFullRegister;
}

public String toString(){

String str = "";

for(Register r: regs){

str += r.toString();
}
return str;
}

}

Register.java

import java.util.LinkedList;
import java.util.Queue;

public class Register {

private boolean open; // line open or not?
private Queue<Customer> line; //the line of customers


public Register(){
open = true;
line = new LinkedList<Customer>();
}

public double getTotalEnqueue(){

double totalTime = 0;

for(Customer cust: line ){
totalTime += cust.getServiceTime();  
}

return totalTime;
}

public void addCustomer(Customer c){
line.add(c);
}

public Customer pollCustomer(){
return line.poll();
}

public boolean isOpen(){

return open;
}

public void setOpen(boolean open){

this.open = open;
}

public int lineSize(){
return line.size();
}

public String toString(){

String str = "";

for(Customer cust: line )
{
str += " " + cust.toString();  
}

return str;
}
}

Customer.java

import java.util.Random;


public class Customer {

private double serviceTime;

Customer(){
Random r = new Random();
serviceTime = r.nextInt(4);
}

public double getServiceTime(){
return serviceTime;
}

public void setServiceTime(double serviceTime){

this.serviceTime = serviceTime;

return;
}

public String toString(){
return "Service Time: " + serviceTime;
}

}

StoreView.java

import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class StoreView extends JFrame{

private JPanel topPanel;
private JPanel checkoutPanel;

private JButton createCustomer;

private JTextField[] totalServiceTime = new JTextField[10];

private ImageIcon registerIcon;

private JLabel[] registers = new JLabel[10];

private ArrayList<Customer> customers = new ArrayList<Customer>();

private StoreModel model;
private Register register = new Register();

public StoreView(StoreModel model){
this.model = model;

setTitle("Checkout Simulator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(11,10));

buildTopPanel();

add(topPanel);
createRegisters();
getServiceTime();

pack();
setVisible(true);
}

public void createRegisters(){

registerIcon = new ImageIcon("C:\Users\Dev\Desktop\GUI Project\GuiProject\src egister.png");

for(int i = 0; i < registers.length; i++){

registers[i] = new JLabel();
registers[i].setIcon(registerIcon);
add(registers[i]);
}
}

public void getServiceTime()
{

for(int i = 0; i < totalServiceTime.length; i++)
{
totalServiceTime[i].setText("" + register.getTotalEnqueue());
add(totalServiceTime[i]);
}
}


public void buildTopPanel(){
topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
createCustomer = new JButton("CreateCustomer");
topPanel.add(createCustomer);
}

public void setCustomer(){

}
}

StoreModel.java

public class StoreModel {

private Customer cust = new Customer();
private Store store = new Store();
private Register reg = new Register();

public StoreModel(){
setCustomer(new Customer());
setStore(new Store());
setRegister(new Register());
}

public StoreModel(Customer customer, Store store, Register register){
setCustomer(customer);
setStore(store);
setRegister(register);
}


public Customer getCustomer(){
return cust;
}

public void setCustomer(Customer customer){
cust = customer;
}

public Store getStore(){
return store;
}

public void setStore(Store store){
this.store = store;
}

public Register getRegister(){
return reg;
}

public void setRegister(Register reg){
this.reg = reg;
}
}

StoreController.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class StoreController {

private StoreModel model;
private StoreView view;

public StoreController(StoreModel model, StoreView view){
this.model = model;
this.view = view;
}

class AddCustomer implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
model.getCustomer().getServiceTime();

}

}
}