Guest User

Untitled

a guest
Feb 27th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 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 java.util.*;
  15. import javax.swing.table.DefaultTableModel;
  16. import javax.swing.table.*;
  17. import javax.swing.event.*;
  18.  
  19. public class UI implements ItemListener
  20. {
  21. JPanel cards;
  22. final static String BUTTONPANEL = "View Inventory";
  23. final static String TEXTPANEL = "Add Inventory";
  24. final static String LOGIN = "Login";
  25. final static String CASHIERMODE = "Cashier Mode";
  26. String passwordM = "manager";
  27. String passwordC = "cashier";
  28. String itemName = "";
  29. String priceNum = "";
  30. String quantityNum = "";
  31. String tot = "";
  32. double priceSell = 0.0;
  33. double total = 0.0;
  34. int quantityS = 0;
  35.  
  36. int cheeseQ = 5;
  37. int PBQ = 10;
  38. int saladQ = 5;
  39. int butterQ = 15;
  40. int tomatoQ = 25;
  41. double temp = 0;
  42. DefaultTableModel tableSet;
  43.  
  44. public void addComponentToPane(Container pane)
  45. {
  46. JPanel comboBoxPane = new JPanel();
  47. JTabbedPane tabbedPane = new JTabbedPane();
  48. JButton add = new JButton("ADD");
  49. JButton login = new JButton("LOGIN");
  50. JButton sell = new JButton("SELL");
  51. JTextField sellquant = new JTextField("",20);
  52. JLabel totalval = new JLabel("TOTAL: ");
  53. JLabel totalprice = new JLabel();
  54. JLabel ce = new JLabel();
  55.  
  56. JTextField item = new JTextField("", 20);
  57. JTextField price = new JTextField("", 20);
  58. JTextField quantity = new JTextField("", 20);
  59.  
  60. JTextField user = new JTextField("", 20);
  61. JTextField pass = new JPasswordField("", 20);
  62.  
  63. JPanel card = new JPanel();
  64.  
  65. String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL, LOGIN};
  66. JComboBox cb = new JComboBox(comboBoxItems);
  67. cb.setEditable(false);
  68. cb.addItemListener(this);
  69. comboBoxPane.add(cb);
  70.  
  71. //creating the card
  72. card.add(new Label("Login: "));
  73. card.add(user);
  74. card.add(pass);
  75. card.add(login);
  76.  
  77. JPanel card1 = new JPanel();
  78.  
  79. add.addActionListener(new ActionListener(){
  80. public void actionPerformed(ActionEvent e){
  81. try{
  82. itemName = item.getText();
  83. priceNum = price.getText();
  84. quantityNum= quantity.getText();
  85. tableSet.addRow(new String[] {itemName, priceNum, quantityNum});
  86. item.setText("");
  87. price.setText("");
  88. quantity.setText("");
  89.  
  90. }
  91. catch(Exception d)
  92. {}
  93. }
  94. } );
  95.  
  96. //creating the Jtable
  97. String[] columns = new String[] {
  98. "Item", "Price", "Quantity"
  99. };
  100. //actual data for the table in a 2d array
  101. Object[][] data = new Object[][] {
  102. {"Cheese", 2.99, cheeseQ},
  103. {"Peanut Butter", 1.99, PBQ},
  104. {"Salad", 3.99, saladQ},
  105. {"Butter", 1.99, butterQ},
  106. {"Tomatoes", 4.99, tomatoQ}
  107.  
  108. };
  109. //create table with dataNum
  110. tableSet = new DefaultTableModel(data, columns);
  111. JTable table = new JTable(tableSet);
  112. JTable table1 = new JTable(tableSet);
  113. //add the table to the frame
  114. card1.add(new JScrollPane(table));
  115. card1.setVisible(true);
  116.  
  117. JPanel card2 = new JPanel();
  118. card2.add(new JLabel("ITEM"));
  119. card2.add(item);
  120. card2.add(new JLabel("PRICE"));
  121. card2.add(price);
  122. card2.add(new JLabel("QUANTITY"));
  123. card2.add(quantity);
  124. card2.add(add);
  125.  
  126. JPanel card3 = new JPanel();
  127. card3.add(new JLabel("Quantity"));
  128. card3.add(sellquant);
  129. card3.add(sell);
  130.  
  131. card3.add(new JScrollPane(table1));
  132. card3.setVisible(true);
  133.  
  134. tabbedPane.addTab(LOGIN, card);
  135.  
  136. login.addActionListener(new ActionListener() {
  137. public void actionPerformed(ActionEvent e) {
  138. String username = user.getText();
  139. String passName = pass.getText();
  140. if(passName.equals(passwordM))
  141. {
  142. card.add(new JLabel("Welcome manager!"));
  143. user.setText("");
  144. pass.setText("");
  145. tabbedPane.addTab(BUTTONPANEL, card1);
  146. tabbedPane.addTab(TEXTPANEL, card2);
  147. tabbedPane.addTab(CASHIERMODE, card3);
  148. }
  149. else if(passName.equals(passwordC))
  150. {
  151. card.add(new JLabel("Welcome cashier!"));
  152. user.setText("");
  153. pass.setText("");
  154. tabbedPane.addTab(CASHIERMODE, card3);
  155. }
  156. else
  157. {
  158. card.add(new JLabel("Username/Password is invalid."));
  159. user.setText("");
  160. pass.setText("");
  161. }
  162.  
  163. }
  164. } );
  165.  
  166. table1.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
  167. public void valueChanged(ListSelectionEvent event) {
  168.  
  169. // priceSell = Double.parseDouble(table1.getValueAt(table1.getSelectedRow(),1).toString());
  170. String s = table1.getModel().getValueAt(table1.getSelectedRow(), 1).toString();
  171. priceSell = Double.parseDouble(s);
  172. //System.out.println(du);
  173. }
  174. });
  175.  
  176. sell.addActionListener(new ActionListener() {
  177. public void actionPerformed(ActionEvent e) {
  178. String quantitySell = sellquant.getText();
  179. quantityS = Integer.parseInt(quantitySell);
  180. // System.out.println(quantitySell);
  181. // System.out.println(priceSell);
  182. // System.out.println(quantityS);
  183.  
  184. total = priceSell*quantityS;
  185. tot = String.valueOf(total);
  186. // System.out.println(tot);
  187.  
  188. card3.add(totalval);
  189. ce.setText(tot);
  190. card3.add(ce);
  191.  
  192. //. System.out.println(tot);
  193.  
  194. double totalsales = total + temp;
  195. temp = total;
  196. String totalsal = String.valueOf(totalsales);
  197.  
  198. //System.out.println(totalsales);
  199. totalprice.setText(totalsal);
  200. card3.add(totalprice, JLabel.SOUTH);
  201. if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Peanut Butter"))
  202. {
  203. tableSet.setValueAt(PBQ - quantityS,1,2);
  204. PBQ = PBQ - quantityS;
  205. }
  206. if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Cheese"))
  207. {
  208. tableSet.setValueAt(cheeseQ - quantityS,0,2);
  209. cheeseQ = cheeseQ - quantityS;
  210. }
  211. if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Salad"))
  212. {
  213. tableSet.setValueAt(saladQ - quantityS,2,2);
  214. saladQ = saladQ - quantityS;
  215. }
  216. if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Butter"))
  217. {
  218. tableSet.setValueAt(butterQ - quantityS,3,2);
  219. butterQ = butterQ - quantityS;
  220. }
  221. if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Tomatoes"))
  222. {
  223. tableSet.setValueAt(tomatoQ - quantityS,4,2);
  224. tomatoQ = tomatoQ - quantityS;
  225. }
  226. }
  227. } );
  228.  
  229. pane.add(tabbedPane, BorderLayout.CENTER);
  230. }
  231.  
  232. public void itemStateChanged(ItemEvent evt) {
  233. CardLayout cl = (CardLayout)(cards.getLayout());
  234. cl.show(cards, (String)evt.getItem());
  235. }
  236.  
  237. private static void createAndShowGUI() {
  238. //Create and set up the window.
  239. JFrame frame = new JFrame("Retail");
  240. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  241.  
  242. //Create and set up the content pane.
  243. UI demo = new UI();
  244.  
  245. //Display the window.
  246. frame.pack();
  247. frame.setSize(500,500);
  248. frame.setVisible(true);
  249.  
  250. demo.addComponentToPane(frame.getContentPane());
  251. //display
  252. frame.pack();
  253. frame.setVisible(true);
  254. }
  255.  
  256. public static void main(String[] args)
  257. {
  258.  
  259. try {
  260.  
  261. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  262. } catch (UnsupportedLookAndFeelException ex) {
  263. ex.printStackTrace();
  264. } catch (IllegalAccessException ex) {
  265. ex.printStackTrace();
  266. } catch (InstantiationException ex) {
  267. ex.printStackTrace();
  268. } catch (ClassNotFoundException ex) {
  269. ex.printStackTrace();
  270. }
  271. /* Turn off metal's use of bold fonts */
  272. UIManager.put("swing.boldMetal", Boolean.FALSE);
  273.  
  274. //Schedule a job for the event dispatch thread:
  275. //creating and showing this application's GUI.
  276. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  277. public void run() {
  278. createAndShowGUI();
  279. }
  280. });
  281. }
  282.  
  283. }
Add Comment
Please, Sign In to add comment