Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _______________________________________________________________________________________________________________________________________
- GroceryLinked Class
- _______________________________________________________________________________________________________________________________________
- package stacksQueuesLinkedLists;
- import java.io.*; //for serialization
- import javax.swing.*;
- import java.util.*; //for ListIterator
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class GroceryLinked extends JFrame implements ActionListener{
- int[] filled = new int[5];
- static JFrame mainFrame = new JFrame();
- JPanel main = new JPanel(new GridLayout(3,1));
- JPanel titlePanel = new JPanel();
- JPanel customerQueuePanel = new JPanel(new GridLayout(1,9));
- JPanel buttonPanel = new JPanel(new GridLayout(1,3));
- JLabel title = new JLabel("Customer Queue Manager");
- JLabel[] customers = new JLabel[9];
- JButton processC = new JButton("Process Next Customer");
- JButton info = new JButton("Info");
- JButton addC = new JButton("Add Customer");
- JPanel infoPanel = new JPanel(new BorderLayout());
- JTextArea infoArea = new JTextArea("In an effort to remind employees and enforce policies around distancing customers in cashier queues, spaces will be added after each customer has been added. As queues only have 9 spaces, there can only be a maximum of 5 customers in each queue, after which you will not be able to add any more without processing them. Stay healthy!");
- JButton back = new JButton("back");
- JTextArea margin1 = new JTextArea(" ");
- JTextArea margin2 = new JTextArea(" ");
- JTextArea margin3 = new JTextArea(" ");
- GLink linked = new GLink();
- public GroceryLinked()
- {
- for(int x = 0; x < 5; ++x) {
- filled[x] = 0;
- }
- for(int x = 0; x < 9; ++x) {
- customers[x] = new JLabel();
- customerQueuePanel.add(customers[x]);
- }
- add(main);
- main.add(titlePanel);
- main.add(customerQueuePanel);
- main.add(buttonPanel);
- titlePanel.add(title);
- buttonPanel.add(addC);
- buttonPanel.add(info);
- buttonPanel.add(processC);
- infoPanel.add(infoArea, BorderLayout.CENTER);
- infoPanel.add(back, BorderLayout.SOUTH);
- infoPanel.add(margin1, BorderLayout.NORTH);
- infoPanel.add(margin2, BorderLayout.EAST);
- infoPanel.add(margin3, BorderLayout.WEST);
- processC.addActionListener(this);
- info.addActionListener(this);
- addC.addActionListener(this);
- back.addActionListener(this);
- infoArea.setLineWrap(true);
- infoArea.setEditable(false);
- margin1.setEditable(false);
- margin2.setEditable(false);
- margin3.setEditable(false);
- }
- public static void main(String[] args)
- {
- mainFrame = new GroceryLinked();
- mainFrame.setBounds(500,500,998,300);
- mainFrame.setTitle("Customer Queue Manager");
- mainFrame.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent temp) {
- Object e = temp.getSource();
- String cName = "";
- String cCost = "";
- if(e.equals(processC)) {
- linked.remove(0);
- linked.remove(0);
- for(int x = 0; x < 9; ++x) {
- customers[x].setText("" + linked.get(x));
- }
- }
- if(e.equals(info)) {
- getContentPane().invalidate();
- getContentPane().removeAll();
- add(infoPanel);
- getContentPane().repaint();
- getContentPane().revalidate();
- }
- if(e.equals(addC)) {
- if(filled[0] == 0) {
- cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
- cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
- if(cName != null || cCost != null) {
- linked.set(0, "" + cName + ", $" + cCost);
- filled[0] = 1;
- }
- }
- else {
- if(filled[1] == 0) {
- cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
- cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
- if(cName != null || cCost != null) {
- linked.set(2, "" + cName + ", $" + cCost);
- filled[1] = 1;
- }
- }
- else {
- if(filled[2] == 0) {
- cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
- cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
- if(cName != null || cCost != null) {
- linked.set(4, "" + cName + ", $" + cCost);
- filled[2] = 1;
- }
- }
- else {
- if(filled[3] == 0) {
- cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
- cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
- if(cName != null || cCost != null) {
- linked.set(6, "" + cName + ", $" + cCost);
- filled[3] = 1;
- }
- }
- else {
- if(filled[4] == 0) {
- cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
- cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
- if(cName != null || cCost != null) {
- linked.set(8, "" + cName + ", $" + cCost);
- filled[4] = 1;
- }
- }
- else {
- JOptionPane.showMessageDialog(null, "The customer queue is full! process customers to make space.");
- }
- }
- }
- }
- }
- for(int x = 0; x < 9; ++x) {
- customers[x].setText("" + linked.get(x));
- }
- }
- if(e.equals(back)) {
- getContentPane().invalidate();
- getContentPane().removeAll();
- add(main);
- getContentPane().repaint();
- getContentPane().revalidate();
- }
- }
- }
- _______________________________________________________________________________________________________________________________________
- GLink Class
- _______________________________________________________________________________________________________________________________________
- package stacksQueuesLinkedLists;
- import java.io.*; //for serialization
- import javax.swing.*;
- import java.util.*; //for ListIterator
- public class GLink implements IndexedList, Serializable {
- private Node head; // pointer to first node
- private static int listSize; // list size
- private Node nodeAtIndex; // The node at index position or null if past the end of the list
- private Node nodeBefore; // The node before index position or null if before the beginning of the list
- private Node pointer;
- public GLink() {
- head = new Node("no customer", null);
- head = new Node("SPACE", head);
- head = new Node("no customer", head);
- head = new Node("SPACE", head);
- head = new Node("no customer", head);
- head = new Node("SPACE", head);
- head = new Node("no customer", head);
- head = new Node("SPACE", head);
- head = new Node("no customer", head);
- listSize = 9;
- }
- public void add(int index, Object obj)
- {
- if(index < 0 || index >= listSize)
- {
- throw new RuntimeException("Index = " + index + " is out of the list bounds");
- }
- if(index == 0)
- {
- head = new Node(obj, head);
- }
- else
- {
- locateNode(index);
- nodeBefore.next = new Node(obj, nodeAtIndex);
- }
- listSize++;
- }
- public boolean isEmpty()
- {
- return listSize == 0;
- }
- public boolean isFull()
- {
- return false;
- }
- public Object get(int index)
- {
- if(index < 0 || index >= listSize)
- {
- throw new RuntimeException("Index = " + index + " is out of the list bounds");
- }
- locateNode(index);
- return nodeAtIndex.value;
- }
- public Object remove(int index)
- {
- if(index < 0 || index >= listSize)
- {
- throw new RuntimeException("Index = " + index + " is out of the list bounds");
- }
- Object removedObj = null;
- if(index == 0)
- {
- removedObj = head.value;
- head = head.next;
- }
- else
- {
- locateNode(index);
- nodeBefore.next = nodeAtIndex.next;
- removedObj = nodeAtIndex.value;
- }
- listSize--;
- return removedObj;
- }
- public Object set(int index, Object obj)
- {
- Object replacedObj = null;
- try
- {
- if(index == 0)
- {
- replacedObj = head.value;
- head.value = obj;
- }
- else
- {
- locateNode(index);
- replacedObj = nodeAtIndex.value;
- nodeAtIndex.value = obj;
- }
- }
- catch(RuntimeException e) //8
- {
- JOptionPane.showMessageDialog(null, "Index = " + index + " is out of the list bounds",
- "Out of Bounds Error", JOptionPane.ERROR_MESSAGE);
- System.out.println("The list size is only" + size());
- }
- return replacedObj;
- }
- public int size()
- {
- return listSize;
- }
- public String toString()
- {
- String message = "";
- for(Node pointer = head; pointer != null; pointer = pointer.next)
- {
- message += pointer.value + " ";
- }
- return message;
- }
- private void locateNode(int index)
- {
- nodeBefore = null;
- nodeAtIndex = head;
- for(int i = 1; i < listSize && i <= index; i++)
- {
- nodeBefore = nodeAtIndex;
- nodeAtIndex = nodeAtIndex.next;
- }
- if(index == listSize)
- {
- nodeBefore = nodeAtIndex;
- nodeAtIndex = null;
- }
- }
- //private inner class for Node
- private class Node
- {
- private Object value;
- private Node next;
- private Node()
- {
- value = null;
- next = null;
- }
- private Node(Object value, Node next)
- {
- this.value = value;
- this.next = next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement