Guest User

Untitled

a guest
Feb 27th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. /**
  2. * Write a description of class UI here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10. import javax.swing.JScrollPane;
  11. import java.awt.CardLayout;
  12. import java.text.*;
  13. import java.io.*;
  14. import javax.swing.table.DefaultTableModel;
  15.  
  16. public class UI implements ItemListener
  17. {
  18. JPanel cards;
  19. final static String BUTTONPANEL = "View Inventory";
  20. final static String TEXTPANEL = "Add Inventory";
  21. final static String LOGIN = "Login";
  22. final static String CASHIERMODE = "Cashier Mode";
  23. String passwordM = "manager";
  24. String passwordC = "cashier";
  25. String itemName = "";
  26. String priceNum = "";
  27. String quantityNum = "";
  28. DefaultTableModel tableSet;
  29. public void addComponentToPane(Container pane)
  30. {
  31. JPanel comboBoxPane = new JPanel();
  32. JTabbedPane tabbedPane = new JTabbedPane();
  33. JButton add = new JButton("ADD");
  34. JButton login = new JButton("LOGIN");
  35. JButton sell = new JButton("SELL");
  36.  
  37. JTextField item = new JTextField("", 20);
  38. JTextField price = new JTextField("", 20);
  39. JTextField quantity = new JTextField("", 20);
  40.  
  41. JTextField user = new JTextField("", 20);
  42. JTextField pass = new JPasswordField("", 20);
  43.  
  44. JPanel card = new JPanel();
  45.  
  46. String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL, LOGIN};
  47. JComboBox cb = new JComboBox(comboBoxItems);
  48. cb.setEditable(false);
  49. cb.addItemListener(this);
  50. comboBoxPane.add(cb);
  51.  
  52. //creating the card
  53. card.add(new Label("Login: "));
  54. card.add(user);
  55. card.add(pass);
  56. card.add(login);
  57.  
  58. JPanel card1 = new JPanel();
  59. //creating the Jtable
  60. String[] columns = new String[] {
  61. "Item", "Price", "Quantity"
  62. };
  63. //actual data for the table in a 2d array
  64. Object[][] data = new Object[][] {
  65. };
  66. //create table with dataNum
  67. tableSet = new DefaultTableModel(data, columns);
  68. JTable table = new JTable(tableSet);
  69. JTable table1 = new JTable(tableSet);
  70. //add the table to the frame
  71. card1.add(new JScrollPane(table));
  72. card1.setVisible(true);
  73.  
  74. JPanel card2 = new JPanel();
  75. card2.add(new JLabel("ITEM"));
  76. card2.add(item);
  77. card2.add(new JLabel("PRICE"));
  78. card2.add(price);
  79. card2.add(new JLabel("QUANTITY"));
  80. card2.add(quantity);
  81. card2.add(add);
  82.  
  83. JPanel card3 = new JPanel();
  84. card3.add(new JLabel("Quantity"));
  85. card3.add(new JTextField("",20));
  86. card3.add(sell);
  87.  
  88. card3.add(new JScrollPane(table1));
  89. card3.setVisible(true);
  90.  
  91. tabbedPane.addTab(LOGIN, card);
  92.  
  93. login.addActionListener(new ActionListener() {
  94. public void actionPerformed(ActionEvent e) {
  95. String username = user.getText();
  96.  
  97. String passName = pass.getText();
  98. if(passName.equals(passwordM))
  99. {
  100. card.add(new JLabel("Welcome manager!"));
  101. user.setText("");
  102. pass.setText("");
  103. tabbedPane.addTab(BUTTONPANEL, card1);
  104. tabbedPane.addTab(TEXTPANEL, card2);
  105. tabbedPane.addTab(CASHIERMODE, card3);
  106. }
  107. else if(passName.equals(passwordC))
  108. {
  109. card.add(new JLabel("Welcome cashier!"));
  110. user.setText("");
  111. pass.setText("");
  112. tabbedPane.addTab(CASHIERMODE, card3);
  113. }
  114. else
  115. {
  116. card.add(new JLabel("Username/Password is invalid."));
  117. user.setText("");
  118. pass.setText("");
  119. }
  120.  
  121. }
  122. } );
  123. add.addActionListener(new ActionListener(){
  124. public void actionPerformed(ActionEvent e){
  125. itemName = item.getText();
  126. priceNum = price.getText();
  127. quantityNum= quantity.getText();
  128. tableSet.addRow(new String[] {itemName, priceNum, quantityNum});
  129. item.setText("");
  130. price.setText("");
  131. quantity.setText("");
  132. Double.parseDouble(priceNum);
  133. Integer.parseInt(quantityNum);
  134. inventoryUpdate(itemName, priceNum, quantityNum);
  135.  
  136. }
  137. } );
  138.  
  139. sell.addActionListener(new ActionListener() {
  140. public void actionPerformed(ActionEvent e) {
  141.  
  142. }
  143. } );
  144. pane.add(tabbedPane, BorderLayout.CENTER);
  145. }
  146.  
  147. public void itemStateChanged(ItemEvent evt) {
  148. CardLayout cl = (CardLayout)(cards.getLayout());
  149. cl.show(cards, (String)evt.getItem());
  150. }
  151.  
  152. public void inventoryUpdate(String name, double price, int quantity)
  153. {
  154. try{
  155. File file = new File("inventory.txt");
  156. //FileWriter writer = new FileWriter(file);
  157. // writer.append(name + ""+ price + "" + quantity);}
  158. catch(Exception e)
  159. {}
  160. }
  161.  
  162. private static void createAndShowGUI() {
  163. //Create and set up the window.
  164. JFrame frame = new JFrame("Retail");
  165. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  166.  
  167. //Create and set up the content pane.
  168. UI demo = new UI();
  169.  
  170. //Display the window.
  171. frame.pack();
  172. frame.setSize(500,500);
  173. frame.setVisible(true);
  174.  
  175. demo.addComponentToPane(frame.getContentPane());
  176. //display
  177. frame.pack();
  178. frame.setVisible(true);
  179. }
  180.  
  181. public static void main(String[] args)
  182. {
  183.  
  184. try {
  185.  
  186. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  187. } catch (UnsupportedLookAndFeelException ex) {
  188. ex.printStackTrace();
  189. } catch (IllegalAccessException ex) {
  190. ex.printStackTrace();
  191. } catch (InstantiationException ex) {
  192. ex.printStackTrace();
  193. } catch (ClassNotFoundException ex) {
  194. ex.printStackTrace();
  195. }
  196. /* Turn off metal's use of bold fonts */
  197. UIManager.put("swing.boldMetal", Boolean.FALSE);
  198.  
  199. //Schedule a job for the event dispatch thread:
  200. //creating and showing this application's GUI.
  201. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  202. public void run() {
  203. createAndShowGUI();
  204. }
  205. });
  206. }
  207.  
  208. }
Add Comment
Please, Sign In to add comment