Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class UI here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.JScrollPane;
- import java.awt.CardLayout;
- import java.text.*;
- import java.io.*;
- import java.util.*;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.table.*;
- import javax.swing.event.*;
- public class UI implements ItemListener
- {
- JPanel cards;
- final static String BUTTONPANEL = "View Inventory";
- final static String TEXTPANEL = "Add Inventory";
- final static String LOGIN = "Login";
- final static String CASHIERMODE = "Cashier Mode";
- String passwordM = "manager";
- String passwordC = "cashier";
- String itemName = "";
- String priceNum = "";
- String quantityNum = "";
- String tot = "";
- double priceSell = 0.0;
- double total = 0.0;
- int quantityS = 0;
- int cheeseQ = 5;
- int PBQ = 10;
- int saladQ = 5;
- int butterQ = 15;
- int tomatoQ = 25;
- double temp = 0;
- DefaultTableModel tableSet;
- public void addComponentToPane(Container pane)
- {
- JPanel comboBoxPane = new JPanel();
- JTabbedPane tabbedPane = new JTabbedPane();
- JButton add = new JButton("ADD");
- JButton login = new JButton("LOGIN");
- JButton sell = new JButton("SELL");
- JTextField sellquant = new JTextField("",20);
- JLabel totalval = new JLabel("TOTAL: ");
- JLabel totalprice = new JLabel();
- JLabel ce = new JLabel();
- JTextField item = new JTextField("", 20);
- JTextField price = new JTextField("", 20);
- JTextField quantity = new JTextField("", 20);
- JTextField user = new JTextField("", 20);
- JTextField pass = new JPasswordField("", 20);
- JPanel card = new JPanel();
- String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL, LOGIN};
- JComboBox cb = new JComboBox(comboBoxItems);
- cb.setEditable(false);
- cb.addItemListener(this);
- comboBoxPane.add(cb);
- //creating the card
- card.add(new Label("Login: "));
- card.add(user);
- card.add(pass);
- card.add(login);
- JPanel card1 = new JPanel();
- add.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- try{
- itemName = item.getText();
- priceNum = price.getText();
- quantityNum= quantity.getText();
- tableSet.addRow(new String[] {itemName, priceNum, quantityNum});
- item.setText("");
- price.setText("");
- quantity.setText("");
- }
- catch(Exception d)
- {}
- }
- } );
- //creating the Jtable
- String[] columns = new String[] {
- "Item", "Price", "Quantity"
- };
- //actual data for the table in a 2d array
- Object[][] data = new Object[][] {
- {"Cheese", 2.99, cheeseQ},
- {"Peanut Butter", 1.99, PBQ},
- {"Salad", 3.99, saladQ},
- {"Butter", 1.99, butterQ},
- {"Tomatoes", 4.99, tomatoQ}
- };
- //create table with dataNum
- tableSet = new DefaultTableModel(data, columns);
- JTable table = new JTable(tableSet);
- JTable table1 = new JTable(tableSet);
- //add the table to the frame
- card1.add(new JScrollPane(table));
- card1.setVisible(true);
- JPanel card2 = new JPanel();
- card2.add(new JLabel("ITEM"));
- card2.add(item);
- card2.add(new JLabel("PRICE"));
- card2.add(price);
- card2.add(new JLabel("QUANTITY"));
- card2.add(quantity);
- card2.add(add);
- JPanel card3 = new JPanel();
- card3.add(new JLabel("Quantity"));
- card3.add(sellquant);
- card3.add(sell);
- card3.add(new JScrollPane(table1));
- card3.setVisible(true);
- tabbedPane.addTab(LOGIN, card);
- login.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String username = user.getText();
- String passName = pass.getText();
- if(passName.equals(passwordM))
- {
- card.add(new JLabel("Welcome manager!"));
- user.setText("");
- pass.setText("");
- tabbedPane.addTab(BUTTONPANEL, card1);
- tabbedPane.addTab(TEXTPANEL, card2);
- tabbedPane.addTab(CASHIERMODE, card3);
- }
- else if(passName.equals(passwordC))
- {
- card.add(new JLabel("Welcome cashier!"));
- user.setText("");
- pass.setText("");
- tabbedPane.addTab(CASHIERMODE, card3);
- }
- else
- {
- card.add(new JLabel("Username/Password is invalid."));
- user.setText("");
- pass.setText("");
- }
- }
- } );
- table1.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
- public void valueChanged(ListSelectionEvent event) {
- // priceSell = Double.parseDouble(table1.getValueAt(table1.getSelectedRow(),1).toString());
- String s = table1.getModel().getValueAt(table1.getSelectedRow(), 1).toString();
- priceSell = Double.parseDouble(s);
- //System.out.println(du);
- }
- });
- sell.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String quantitySell = sellquant.getText();
- quantityS = Integer.parseInt(quantitySell);
- // System.out.println(quantitySell);
- // System.out.println(priceSell);
- // System.out.println(quantityS);
- total = priceSell*quantityS;
- tot = String.valueOf(total);
- // System.out.println(tot);
- card3.add(totalval);
- ce.setText(tot);
- card3.add(ce);
- //. System.out.println(tot);
- double totalsales = total + temp;
- temp = total;
- String totalsal = String.valueOf(totalsales);
- //System.out.println(totalsales);
- totalprice.setText(totalsal);
- card3.add(totalprice, JLabel.SOUTH);
- if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Peanut Butter"))
- {
- tableSet.setValueAt(PBQ - quantityS,1,2);
- PBQ = PBQ - quantityS;
- }
- if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Cheese"))
- {
- tableSet.setValueAt(cheeseQ - quantityS,0,2);
- cheeseQ = cheeseQ - quantityS;
- }
- if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Salad"))
- {
- tableSet.setValueAt(saladQ - quantityS,2,2);
- saladQ = saladQ - quantityS;
- }
- if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Butter"))
- {
- tableSet.setValueAt(butterQ - quantityS,3,2);
- butterQ = butterQ - quantityS;
- }
- if(table1.getModel().getValueAt(table1.getSelectedRow(), 0).toString().equals("Tomatoes"))
- {
- tableSet.setValueAt(tomatoQ - quantityS,4,2);
- tomatoQ = tomatoQ - quantityS;
- }
- }
- } );
- pane.add(tabbedPane, BorderLayout.CENTER);
- }
- public void itemStateChanged(ItemEvent evt) {
- CardLayout cl = (CardLayout)(cards.getLayout());
- cl.show(cards, (String)evt.getItem());
- }
- private static void createAndShowGUI() {
- //Create and set up the window.
- JFrame frame = new JFrame("Retail");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Create and set up the content pane.
- UI demo = new UI();
- //Display the window.
- frame.pack();
- frame.setSize(500,500);
- frame.setVisible(true);
- demo.addComponentToPane(frame.getContentPane());
- //display
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args)
- {
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
- } catch (UnsupportedLookAndFeelException ex) {
- ex.printStackTrace();
- } catch (IllegalAccessException ex) {
- ex.printStackTrace();
- } catch (InstantiationException ex) {
- ex.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- /* Turn off metal's use of bold fonts */
- UIManager.put("swing.boldMetal", Boolean.FALSE);
- //Schedule a job for the event dispatch thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment