Instructions
Requirements and Specifications
Source Code
GUI INTERFACE
package ca.cmpt213.a3.view;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import ca.cmpt213.a3.model.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class GUIInterface extends JFrame {
private JList list;
private static String JSON_FILE = "items.json";
public GUIInterface(String title)
{
super(title);
setBounds(100,100,500,500);
JPanel contentPane = new JPanel();
contentPane.setLayout(null);
ItemModel model = new ItemModel();
// top panel, which contains the all, Expired, Not Expired, etc... buttons
JPanel topPanel = new JPanel();
topPanel.setLayout(null);
topPanel.setBounds(0,0,450,50);
JButton allBtn = new JButton("All");
allBtn.setBounds(20, 20, 100, 30);
allBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
model.clearDisplayCart();
for(Item item: model.getShoppingCart().getItems())
{
model.addDisplay(item);
}
model.update();
}
});
JButton expiredBtn = new JButton("Expired");
expiredBtn.setBounds(120, 20, 100, 30);
expiredBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
model.clearDisplayCart();
for(Item item: model.getShoppingCart().getItems())
{
// chekc if the today's date is greater than expiry date
if(item.getExpiry_date().before(new Date())) {
model.addDisplay(item);
}
}
model.update();
}
});
JButton notExpiredBtn = new JButton("Not Expired");
notExpiredBtn.setBounds(220, 20, 100, 30);
notExpiredBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
model.clearDisplayCart();
for(Item item: model.getShoppingCart().getItems())
{
// chekc if the today's date is before than expiry date
if(item.getExpiry_date().after(new Date())) {
model.addDisplay(item);
}
}
model.update();
}
});
JButton expiringBtn = new JButton("Expiring 7 Days");
expiringBtn.setBounds(320, 20, 130, 30);
expiringBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
model.clearDisplayCart();
for(Item item: model.getShoppingCart().getItems())
{
// check if expiry date is after today's date and if the number of days between is less or eual to 7
Date today = new Date();
long days = item.getExpiry_date().getTime() - today.getTime();
days = TimeUnit.DAYS.convert(days, TimeUnit.MILLISECONDS);
if(item.getExpiry_date().after(today) && days <= 7) {
model.addDisplay(item);
}
}
model.update();
}
});
topPanel.add(allBtn);
topPanel.add(expiredBtn);
topPanel.add(notExpiredBtn);
topPanel.add(expiringBtn);
contentPane.add(topPanel);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(null);
bottomPanel.setBounds(0,360,450,200);
JButton removeBtn = new JButton("Remove selected");
removeBtn.setBounds(170,0,150,30);
removeBtn.setEnabled(false);
removeBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println(list.getSelectedIndex());
if(list.getSelectedIndex() != -1) {
int idx = list.getSelectedIndex();
Item item = model.getItem(idx);
if(item != null)
{
System.out.println(item.toString());
model.removeItem(idx);
}
}
}
});
bottomPanel.add(removeBtn);
JButton addBtn = new JButton("Add Item");
addBtn.setBounds(170,40,150,30);
addBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
AddItemInterface additemapp = new AddItemInterface("Add New Item", model);
}
});
bottomPanel.add(addBtn);
// List Panel
JPanel listPanel = new JPanel();
listPanel.setBounds(20,100,200,200);
listPanel.setLayout(new BorderLayout());
ItemModelPainter painter = new ItemModelPainter();
painter.setRemoveButton(removeBtn);
list = new JList();
list.setModel(model);
list.setCellRenderer(painter);
list.setVisibleRowCount(4);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// event to list
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
if(list.getSelectedIndex() != -1)
{
removeBtn.setEnabled(true);
}
else
{
removeBtn.setEnabled(false);
}
}
});
JScrollPane jcp = new JScrollPane(list);
jcp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jcp.setBounds(10,80,460,250);
contentPane.add(jcp);
// Set default closing operation
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
contentPane.add(bottomPanel);
this.setContentPane(contentPane);
// Add the closing event listener
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
// If there are items in the app, save them into a JSON file
Gson gson = new Gson();
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(JSON_FILE));
JsonArray itemsArray = new JsonArray();
for(int i = 0; i < model.getShoppingCart().getItems().size(); i++)
{
Item item = model.getShoppingCart().getItems().get(i);
itemsArray.add(item.toJSONObject());
}
gson.toJson(itemsArray, new FileWriter(JSON_FILE));
writer.write(itemsArray.toString());
writer.close();
}
catch(Exception ex)
{
System.out.println("Could not open file: " + JSON_FILE);
}
setVisible(false);
dispose();
}
});
// Now that all the components have been loaded, let' read the JSON file
try{
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader(JSON_FILE));
JsonArray itemsArray = (JsonArray)obj;
itemsArray.forEach(item -> parseItemObject((JsonObject)item, model));
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void parseItemObject(JsonObject object, ItemModel model)
{
// Get fields
int id = Integer.valueOf(object.get("Id").toString());
String name = (String)object.get("Name").getAsString();
String notes = (String)object.get("Notes").getAsString();
float price = Float.valueOf(object.get("Price").getAsString());
float volume = Float.valueOf(object.get("Volume").getAsString());
String expiryDate_str= (String)object.get("ExpiryDate").getAsString();
// parse date
Date expiryDate= null;
try
{
expiryDate = new SimpleDateFormat("yyyy-MM-dd").parse(expiryDate_str);
}
catch(Exception ex)
{
System.out.println("Invalid date format when parsing JSONObject");
return;
}
Item item = null;
if(((String)object.get("Type").getAsString()).compareTo("Food") == 0)
{
item = new FoodItem(id, name, price, volume, expiryDate, notes);
}
else
{
item = new DrinkItem(id, name, price, volume, expiryDate, notes);
}
// Add to model
model.add(item);
}
}