Advertisement
Guest User

GroceryLinked & GLink classes

a guest
Apr 6th, 2020
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.28 KB | None | 0 0
  1. _______________________________________________________________________________________________________________________________________
  2. GroceryLinked Class
  3. _______________________________________________________________________________________________________________________________________
  4. package stacksQueuesLinkedLists;
  5.  
  6. import java.io.*; //for serialization
  7. import javax.swing.*;
  8. import java.util.*; //for ListIterator
  9.  
  10. import javax.swing.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13.  
  14.  
  15. public class GroceryLinked extends JFrame implements ActionListener{
  16.    
  17.  
  18.     int[] filled = new int[5];
  19.  
  20.     static JFrame mainFrame = new JFrame();
  21.     JPanel main = new JPanel(new GridLayout(3,1));
  22.     JPanel titlePanel = new JPanel();
  23.     JPanel customerQueuePanel = new JPanel(new GridLayout(1,9));
  24.     JPanel buttonPanel = new JPanel(new GridLayout(1,3));
  25.    
  26.     JLabel title = new JLabel("Customer Queue Manager");
  27.     JLabel[] customers = new JLabel[9];
  28.    
  29.     JButton processC = new JButton("Process Next Customer");
  30.     JButton info = new JButton("Info");
  31.     JButton addC = new JButton("Add Customer");
  32.    
  33.    
  34.     JPanel infoPanel = new JPanel(new BorderLayout());
  35.     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!");
  36.     JButton back = new JButton("back");
  37.     JTextArea margin1 = new JTextArea("    ");
  38.     JTextArea margin2 = new JTextArea("    ");
  39.     JTextArea margin3 = new JTextArea("    ");
  40.    
  41.    
  42.     GLink linked = new GLink();
  43.    
  44.     public GroceryLinked()
  45.     {
  46.  
  47.        
  48.         for(int x = 0; x < 5; ++x) {
  49.             filled[x] = 0;
  50.         }
  51.        
  52.         for(int x = 0; x < 9; ++x) {
  53.             customers[x] = new JLabel();
  54.             customerQueuePanel.add(customers[x]);
  55.         }
  56.        
  57.        
  58.         add(main);
  59.         main.add(titlePanel);
  60.         main.add(customerQueuePanel);
  61.         main.add(buttonPanel);
  62.        
  63.         titlePanel.add(title);
  64.  
  65.         buttonPanel.add(addC);
  66.         buttonPanel.add(info);
  67.         buttonPanel.add(processC);
  68.  
  69.         infoPanel.add(infoArea, BorderLayout.CENTER);
  70.         infoPanel.add(back, BorderLayout.SOUTH);
  71.         infoPanel.add(margin1, BorderLayout.NORTH);
  72.         infoPanel.add(margin2, BorderLayout.EAST);
  73.         infoPanel.add(margin3, BorderLayout.WEST);
  74.  
  75.         processC.addActionListener(this);
  76.         info.addActionListener(this);
  77.         addC.addActionListener(this);
  78.         back.addActionListener(this);
  79.  
  80.         infoArea.setLineWrap(true);
  81.         infoArea.setEditable(false);
  82.         margin1.setEditable(false);
  83.         margin2.setEditable(false);
  84.         margin3.setEditable(false);
  85.        
  86.     }
  87.  
  88.    
  89.     public static void main(String[] args)
  90.     {
  91.  
  92.         mainFrame = new GroceryLinked();
  93.         mainFrame.setBounds(500,500,998,300);
  94.         mainFrame.setTitle("Customer Queue Manager");
  95.         mainFrame.setVisible(true);
  96.  
  97.  
  98.     }
  99.     @Override
  100.     public void actionPerformed(ActionEvent temp) {
  101.         Object e = temp.getSource();
  102.  
  103.         String cName = "";
  104.         String cCost = "";
  105.        
  106.        
  107.        
  108.  
  109.         if(e.equals(processC)) {
  110.             linked.remove(0);
  111.             linked.remove(0);
  112.             for(int x = 0; x < 9; ++x) {
  113.                 customers[x].setText("" + linked.get(x));
  114.             }
  115.         }
  116.        
  117.        
  118.         if(e.equals(info)) {
  119.             getContentPane().invalidate();
  120.             getContentPane().removeAll();
  121.             add(infoPanel);
  122.  
  123.             getContentPane().repaint();
  124.             getContentPane().revalidate();
  125.         }
  126.        
  127.        
  128.         if(e.equals(addC)) {
  129.             if(filled[0] == 0) {
  130.                 cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
  131.                 cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
  132.                 if(cName != null || cCost != null) {
  133.                     linked.set(0, "" + cName + ", $" + cCost);
  134.                     filled[0] = 1;
  135.                 }
  136.             }
  137.             else {
  138.                 if(filled[1] == 0) {
  139.                     cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
  140.                     cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
  141.                     if(cName != null || cCost != null) {
  142.                         linked.set(2, "" + cName + ", $" + cCost);
  143.                         filled[1] = 1;
  144.                     }
  145.                 }
  146.                 else {
  147.  
  148.                     if(filled[2] == 0) {
  149.                         cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
  150.                         cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
  151.                         if(cName != null || cCost != null) {
  152.                             linked.set(4, "" + cName + ", $" + cCost);
  153.                             filled[2] = 1;
  154.                         }
  155.                     }
  156.                     else {
  157.  
  158.                         if(filled[3] == 0) {
  159.                             cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
  160.                             cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
  161.                             if(cName != null || cCost != null) {
  162.                                 linked.set(6, "" + cName + ", $" + cCost);
  163.                                 filled[3] = 1;
  164.                             }
  165.                         }
  166.                         else {
  167.                             if(filled[4] == 0) {
  168.                                 cName = JOptionPane.showInputDialog(null, "Please input the customers name.");
  169.                                 cCost = (JOptionPane.showInputDialog(null, "Please input cost of the customers order."));
  170.                                 if(cName != null || cCost != null) {
  171.                                     linked.set(8, "" + cName + ", $" + cCost);
  172.                                     filled[4] = 1;
  173.                                 }
  174.                             }
  175.                             else {
  176.                                 JOptionPane.showMessageDialog(null, "The customer queue is full! process customers to make space.");
  177.                             }
  178.                         }
  179.                     }
  180.                 }
  181.             }
  182.             for(int x = 0; x < 9; ++x) {
  183.                 customers[x].setText("" + linked.get(x));
  184.             }
  185.         }
  186.        
  187.         if(e.equals(back)) {
  188.             getContentPane().invalidate();
  189.             getContentPane().removeAll();
  190.             add(main);
  191.  
  192.             getContentPane().repaint();
  193.             getContentPane().revalidate();
  194.         }
  195.        
  196.        
  197.        
  198.     }
  199.    
  200. }
  201.  
  202.  
  203.  
  204. _______________________________________________________________________________________________________________________________________
  205. GLink Class
  206. _______________________________________________________________________________________________________________________________________
  207.  
  208. package stacksQueuesLinkedLists;
  209.  
  210. import java.io.*; //for serialization
  211. import javax.swing.*;
  212. import java.util.*; //for ListIterator
  213.  
  214. public class GLink implements IndexedList, Serializable {
  215.    
  216.     private Node head; // pointer to first node
  217.     private static int listSize; // list size
  218.     private Node nodeAtIndex; // The node at index position or null if past the end of the list
  219.     private Node nodeBefore; // The node before index position or null if before the beginning of the list
  220.     private Node pointer;
  221.  
  222.     public GLink() {
  223.         head = new Node("no customer", null);
  224.         head = new Node("SPACE", head);
  225.         head = new Node("no customer", head);
  226.         head = new Node("SPACE", head);
  227.         head = new Node("no customer", head);
  228.         head = new Node("SPACE", head);
  229.         head = new Node("no customer", head);
  230.         head = new Node("SPACE", head);
  231.         head = new Node("no customer", head);
  232.         listSize = 9;
  233.     }
  234.    
  235.    
  236.     public void add(int index, Object obj)
  237.     {
  238.         if(index < 0 || index >= listSize)
  239.         {
  240.             throw new RuntimeException("Index = " + index + " is out of the list bounds");
  241.         }
  242.         if(index == 0)
  243.         {
  244.             head = new Node(obj, head);
  245.         }
  246.         else
  247.         {
  248.             locateNode(index);
  249.             nodeBefore.next = new Node(obj, nodeAtIndex);
  250.         }
  251.         listSize++;
  252.     }
  253.    
  254.     public boolean isEmpty()
  255.     {
  256.         return listSize == 0;
  257.     }
  258.    
  259.     public boolean isFull()
  260.     {
  261.         return false;
  262.     }
  263.    
  264.     public Object get(int index)
  265.     {
  266.         if(index < 0 || index >= listSize)
  267.         {
  268.             throw new RuntimeException("Index = " + index + " is out of the list bounds");
  269.         }
  270.        
  271.         locateNode(index);
  272.         return nodeAtIndex.value;
  273.     }
  274.    
  275.     public Object remove(int index)
  276.     {
  277.         if(index < 0 || index >= listSize)
  278.         {
  279.             throw new RuntimeException("Index = " + index + " is out of the list bounds");
  280.         }
  281.        
  282.         Object removedObj = null;
  283.        
  284.         if(index == 0)
  285.         {
  286.             removedObj = head.value;
  287.             head = head.next;
  288.         }
  289.        
  290.         else
  291.         {
  292.             locateNode(index);
  293.             nodeBefore.next = nodeAtIndex.next;
  294.             removedObj = nodeAtIndex.value;
  295.         }
  296.        
  297.         listSize--;
  298.         return removedObj;
  299.     }
  300.    
  301.     public Object set(int index, Object obj)
  302.     {
  303.         Object replacedObj = null;
  304.        
  305.         try
  306.         {
  307.             if(index == 0)
  308.             {
  309.                 replacedObj = head.value;
  310.                 head.value = obj;
  311.             }
  312.             else
  313.             {
  314.                 locateNode(index);
  315.                 replacedObj = nodeAtIndex.value;
  316.                 nodeAtIndex.value = obj;
  317.             }
  318.         }
  319.        
  320.         catch(RuntimeException e)  //8
  321.         {
  322.             JOptionPane.showMessageDialog(null, "Index = " + index + " is out of the list bounds",
  323.             "Out of Bounds Error", JOptionPane.ERROR_MESSAGE);
  324.             System.out.println("The list size is only" + size());
  325.         }
  326.        
  327.         return replacedObj;
  328.     }
  329.    
  330.     public int size()
  331.     {
  332.         return listSize;
  333.     }
  334.    
  335.     public String toString()
  336.     {
  337.         String message = "";
  338.        
  339.         for(Node pointer = head; pointer != null; pointer = pointer.next)
  340.         {
  341.             message += pointer.value + " ";
  342.         }
  343.         return message;
  344.     }
  345.    
  346.     private void locateNode(int index)
  347.     {
  348.         nodeBefore = null;
  349.         nodeAtIndex = head;
  350.        
  351.         for(int i = 1; i < listSize && i <= index; i++)
  352.         {
  353.             nodeBefore = nodeAtIndex;
  354.             nodeAtIndex = nodeAtIndex.next;
  355.         }
  356.        
  357.         if(index == listSize)
  358.         {
  359.             nodeBefore = nodeAtIndex;
  360.             nodeAtIndex = null;
  361.         }
  362.     }
  363.    
  364.     //private inner class for Node
  365.     private class Node
  366.     {
  367.         private Object value;
  368.         private Node next;
  369.         private Node()
  370.         {
  371.             value = null;
  372.             next = null;
  373.         }
  374.         private Node(Object value, Node next)
  375.         {
  376.             this.value = value;
  377.             this.next = next;
  378.         }
  379.     }
  380.    
  381.    
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement