Rabu, 11 Desember 2019

Evaluasi Akhir Semester Ganjil Pemrograman Berbasis Objek 2019

Tugas Evaluasi Akhir Semester kali ini adalah membuat aplikasi Point of Sales. Point of Sales adalah aplikasi yang digunakan untuk membantu melakukan transaksi jual beli. Berikut akan dijelaskan lebih lanjut mengenai Point of Sales :

1.   Mesin cash register/POS
a.   Merk :
Merk POS kali ini adalah SPOTS

b.   Fungsi/fitur yang tersedia dan penjelasan bagian-bagiannya
 Menu login digunakan agar pengguna bisa masuk menggunakan POS sesuai identitas yang telah didaftarkan.
-   Menu Create New Item digunakan untuk membuat daftar barang baru yang akan dijual. Menu ini terdiri dari :
·     Item Name : memberi nama barang yang akan dijual
·     Category : mengelompokkan barang-barang yang dijual ke dalam beberapa kategori
·     Price : memberi harga jual pada barang yang dijual
·     SKU : memberikan kode barang yang dijual agar memudahkan untuk membedakan setiap barang
·     Description : memberikan penjelasan pada barang

-     Menu Purchase digunakan untuk melakukan pembayaran barang yang dibeli

2. Source Code
a. Class Login
Class ini berfungsi untuk memastikan bahwa hanya orang-orang yang berhak mendapatkan akses ke POS yang bisa menggunakan aplikasi ini.
 import javax.swing.*; 
 import java.awt.*; 
 import java.awt.event.*; 

 public class Login{ 
   String Username = "admin"; 
   String Password = "admin"; 
   String msg = " "; 
   private JTextField txtUsername;
   private JPasswordField txtPassword;
   static JButton ToLogin;
   public static void main(String[] args){ 
     Login gui = new Login(); 
     gui.go(); 
   } 
   public void go(){ 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new JPanel(); 
     JLabel lblUsername = new JLabel("Username:");   
     JLabel lblPassword = new JLabel("Password:"); 
     txtUsername = new JTextField(20); 
     txtPassword = new JPasswordField(20); 
     JButton btnLogin = new JButton("Login"); 
     btnLogin.addActionListener(new LoginListener()); 
     JButton btnCancel = new JButton("Cancel"); 
     btnCancel.addActionListener(new CancelListener()); 
     panel.add(lblUsername); 
     panel.add(txtUsername); 
     panel.add(lblPassword); 
     panel.add(txtPassword);     
     panel.add(btnLogin);
     panel.add(btnCancel);
     frame.getContentPane().add(BorderLayout.CENTER,panel); 
     frame.setSize(300,300); 
     frame.setVisible(true); 
   } 
   public class LoginListener implements ActionListener{ 
     public void actionPerformed(ActionEvent event){ 
       if(Username.equals(txtUsername.getText())){ 
         if(Password.equals(txtPassword.getText())){ 
           msg = "Login success!"; 
           Main main = new Main();
           main.create();

         }else{ 
           msg = "Login failed!"; 
         } 
       }else{ 
         msg = "The username or password incorrect. Please try again!"; 
       }   
       JOptionPane.showMessageDialog(null,msg);           
     } 
   } 
   public class CancelListener implements ActionListener{ 
     public void actionPerformed(ActionEvent event){ 
       txtUsername.setText(""); 
       txtPassword.setText(""); 
       txtUsername.requestFocus(); 
     } 
   }


b. Class Main
Class Main berisi tentang
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.table.*;


class Main implements ActionListener {
  static Main main;
 
  // create a frame
static JFrame frame;

// create components
static JTextField labeltotal;
//static JTextField labelcustom;
  static JButton iconedit, iconpurchase;
static JButton butttonlib; //, buttoncus
  static JButton buttoncreate, buttondone;
static JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonc, buttonadd;
  static JTable maintable;
  static DefaultTableModel model;
  static JScrollPane sp;

  static Boolean edit = false;

// store operands
static String s0 = "";
  static String st;

  static String[] columnNames = { "Name", "Price", "Category", "SKU", "Description", "Count" };

  // create an itemframe
static JFrame itemframe;

// create components
  static JTextField labelname;
  static DefaultListModel listModel;
  static JTextField labelcategory;
static JTextField labelprice;
  static JTextField labelSKU;
  static JTextField labeldesc;

  static JButton buttonexit, buttonsave, buttondelete;
 
  static Boolean edititem; 

  static Boolean purchase; 
  static JFrame purchaseframe;
  static JLabel currentsale;
static JTextField labelpurchasetotal;
  static JButton buttonexitpurchase, buttonclear;
  static JTable purchasetable;
  static JScrollPane purchasesp;
  static DefaultTableModel purchasemodel;
  static String[] purchasecolumnNames = { "Name", "Count", "Price", "Delete" };

// default constructor
Main()
{
    st = "0";
}

// main function
public static void create()
{
             // create a frame
             frame = new JFrame("Point Of Sales");
             frame.setVisible(true);
             frame.getContentPane().setBackground(Color.green);

             // create a object of class
             main = new Main();

             // create a textfield
             labeltotal = new JTextField(24);
             labeltotal.setText("Charge Rp. 0");



             // set the textfield to non editable
             labeltotal.setEditable(false);
             //labelcustom.setEditable(false);

    ImageIcon imageIcon = new ImageIcon("resources/search-button-icon.png");
    Image image = imageIcon.getImage();
    Image newimg = image.getScaledInstance(18, 18, Image.SCALE_SMOOTH);
    imageIcon = new ImageIcon(newimg);
   


   

    imageIcon = new ImageIcon("resources/edit-button-icon.jpg");
    image = imageIcon.getImage();
    newimg = image.getScaledInstance(16, 16, Image.SCALE_SMOOTH);
    imageIcon = new ImageIcon(newimg);

    iconedit = new JButton("", imageIcon);
    iconedit.setActionCommand("edit");
    iconedit.setPreferredSize(new Dimension(18, 18));
    iconedit.setBorder(BorderFactory.createEmptyBorder());

    //create menubutton
    butttonlib = new JButton("Library");
    butttonlib.setPreferredSize(new Dimension(120, 50));

    //buttoncus = new JButton("Custom");
    //buttoncus.setPreferredSize(new Dimension(120, 50));

    // create submenu buttons
             buttoncreate = new JButton("CREATE NEW ITEM");
    buttoncreate.setPreferredSize(new Dimension(240, 50));
    buttondone = new JButton("DONE");
    buttondone.setPreferredSize(new Dimension(240, 50));
   
    imageIcon = new ImageIcon("resources/purchase-icon.png");
    image = imageIcon.getImage();
    newimg = image.getScaledInstance(40, 40, Image.SCALE_SMOOTH);
    imageIcon = new ImageIcon(newimg);
   
    iconpurchase = new JButton("", imageIcon);
    iconpurchase.setActionCommand("Purchase");
    iconpurchase.setPreferredSize(new Dimension(240, 50));


             // create a panel
             JPanel panel = new JPanel();

    // Initializing the JTable
    model = new DefaultTableModel(columnNames, 0);
    maintable = new JTable(model);

    maintable.setTableHeader(null);
    maintable.setShowGrid(false);
    maintable.setDefaultEditor(Object.class, null);
    maintable.getColumnModel().getColumn(2).setMinWidth(0);
    maintable.getColumnModel().getColumn(2).setMaxWidth(0);
    maintable.getColumnModel().getColumn(2).setWidth(0);
    maintable.getColumnModel().getColumn(3).setMinWidth(0);
    maintable.getColumnModel().getColumn(3).setMaxWidth(0);
    maintable.getColumnModel().getColumn(3).setWidth(0);
    maintable.getColumnModel().getColumn(4).setMinWidth(0);
    maintable.getColumnModel().getColumn(4).setMaxWidth(0);
    maintable.getColumnModel().getColumn(4).setWidth(0);
    maintable.getColumnModel().getColumn(5).setMinWidth(0);
    maintable.getColumnModel().getColumn(5).setMaxWidth(0);
    maintable.getColumnModel().getColumn(5).setWidth(0);

    maintable.addMouseListener(new MouseAdapter()
    {
      public void mouseClicked(MouseEvent event)
      {
        if (!edit)
        {
          int row = maintable.rowAtPoint(event.getPoint());
          int col = 1;
          int te = 0;
          s0 = maintable.getValueAt(row, col).toString();
          te = (Integer.parseInt(s0) + Integer.parseInt(st));
          st = Integer.toString(te);
          labeltotal.setText("Charge Rp. " + st);

          model.setValueAt(Integer.parseInt(maintable.getValueAt(row, 5).toString()) + 1, row, 5);

          maintable.setModel(model);
        }
        else
        {
          createItemGUI("edit", maintable.rowAtPoint(event.getPoint()));
        }
      }
    }
    );

    // adding it to JScrollPane
    sp = new JScrollPane(maintable);
    sp.setPreferredSize(new Dimension(240, 150));
            
    // add action listeners
    iconedit.addActionListener(main);
    iconpurchase.addActionListener(main);    
    butttonlib.addActionListener(main);          
  //  buttoncus.addActionListener(main);       

             buttoncreate.addActionListener(main);
    buttondone.addActionListener(main);


             // add elements to panel
             panel.add(labeltotal);
    panel.add(butttonlib);
   // panel.add(buttoncus);
    panel.add(iconedit);

             panel.add(buttoncreate);
    panel.add(sp);
    panel.add(buttondone);
    panel.add(iconpurchase);

  

             // add panel to frame
             frame.add(panel);

             frame.setSize(280, 500);
    main.startLibrary();
             frame.show();
  }
 
  public static void createItemGUI(String buffer, int row)
{
    if(buffer.compareTo("new") == 0)
    {
      edititem = false;
    }
    else
    {
      edititem = true;
    }

    if(!edititem)
    {
      // create an itemframe
      itemframe = new JFrame("Create New Item");
    }
    else
    {
      itemframe = new JFrame("Edit Item");
    }
             // create a textfield

    labelname = new JTextField(24);
    labelprice = new JTextField(24);
    labelcategory = new JTextField(24);
    labelSKU = new JTextField(24);
    labeldesc = new JTextField(24);
    if(edititem)
    {
      labelname.setText(maintable.getValueAt(row, 0).toString());
      labelprice.setText(maintable.getValueAt(row, 1).toString());
      labelcategory.setText(maintable.getValueAt(row, 2).toString());
      labelSKU.setText(maintable.getValueAt(row, 3).toString());
      labeldesc.setText(maintable.getValueAt(row, 4).toString());
    }
    else
    {
      labelname.setText("Item Name");
      labelprice.setText("Price");
      labelcategory.setText("Category");
      labelSKU.setText("SKU");
      labeldesc.setText("Description"); 
    }
   
    labelname.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelname.getText().equals("Item Name"))
          {
            labelname.setText("");
          }          
        }
    }
    );
   
    labelcategory.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelcategory.getText().equals("Category"))
          {
            labelcategory.setText("");
          }          
        }
    }
    );

    labelprice.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelprice.getText().equals("Price"))
          {
            labelprice.setText("");
          }          
        }
    }
    );
            
    labelSKU.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelSKU.getText().equals("SKU"))
          {
            labelSKU.setText("");
          }          
        }
    }
    );
   
    labeldesc.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labeldesc.getText().equals("Description"))
          {
            labeldesc.setText("");
          }          
        }
    }
    );

    buttonexit = new JButton("X");
    buttonexit.setPreferredSize(new Dimension(50, 50));

             buttonsave = new JButton("SAVE");
    buttonsave.setPreferredSize(new Dimension(190, 50));

    buttondelete = new JButton("DELETE");
    buttondelete.setPreferredSize(new Dimension(240, 50));
 
    // create a itempanel
             JPanel itempanel = new JPanel();
            
    // add action listeners
             buttonsave.addActionListener(main);
    buttonexit.addActionListener(main);
    buttondelete.addActionListener(main);

             // add elements to itempanel
    itempanel.add(labelname);
    itempanel.add(labelcategory);
    itempanel.add(labelprice);
    itempanel.add(labelSKU);
    itempanel.add(labeldesc);
             itempanel.add(buttonexit);
             itempanel.add(buttonsave);

    if(edititem)
    {
      itempanel.add(buttondelete);
    }
             // add itempanel to itemframe
             itemframe.add(itempanel);

             itemframe.setSize(300, 500);
             itemframe.show();
  }
 
  public static void createPurchaseGUI()
{
    int total = 0;
    Object[] object = new Object[4];

    purchaseframe = new JFrame("Purchase");
    purchase = true;
    currentsale = new JLabel("CURRENT SALE");
    labelpurchasetotal = new JTextField(24);
             labelpurchasetotal.setEditable(false);
    labelpurchasetotal.setText("Charge Rp. " + st);

    //Initializing the JTable
    purchasemodel = new DefaultTableModel(purchasecolumnNames, 0);

    for(int i = 0; i < maintable.getRowCount(); i++)
    {
      if(Integer.parseInt(maintable.getValueAt(i, 5).toString()) > 0)
      {
        object[0] = maintable.getValueAt(i, 0).toString();
        object[1] = ("x" + maintable.getValueAt(i, 5).toString());
        object[2] = Integer.parseInt(maintable.getValueAt(i, 1).toString()) * Integer.parseInt(maintable.getValueAt(i, 5).toString());
        object[3] = "X";
        total = total + Integer.parseInt(maintable.getValueAt(i, 1).toString()) * Integer.parseInt(maintable.getValueAt(i, 5).toString());
        purchasemodel.addRow(object);
      }
    }

    if (total < Integer.parseInt(st))
    {
      object[0] = "Custom";
      object[1] = "";
      object[2] = Integer.parseInt(st) - total;
      object[3] = "X";
     
      purchasemodel.addRow(object);
    }

    purchasetable = new JTable(purchasemodel);
    purchasetable.getColumnModel().getColumn(3).setMinWidth(11);
    purchasetable.getColumnModel().getColumn(3).setMaxWidth(11);
    purchasetable.getColumnModel().getColumn(3).setWidth(11);
    purchasetable.setTableHeader(null);
    purchasetable.setShowGrid(false);
    purchasetable.setDefaultEditor(Object.class, null);

    purchasetable.addMouseListener(new MouseAdapter()
    {
      public void mouseClicked(MouseEvent event)
      {
        int col = purchasetable.columnAtPoint(event.getPoint());
        int row = purchasetable.rowAtPoint(event.getPoint());
        if (col == 3)
        {
          int te = 0;
          s0 = purchasetable.getValueAt(row, 2).toString();
          te = (Integer.parseInt(st) - Integer.parseInt(s0));
          st = Integer.toString(te);
          labelpurchasetotal.setText("Charge Rp. " + st);
          labeltotal.setText("Charge Rp. " + st);
          
          for (int i = 0; i < model.getRowCount(); i++)
          {
            if(model.getValueAt(i, 0) == purchasetable.getValueAt(row, 0))
            {
              model.setValueAt(0, i, 5);
            }
          }
          int numRows = purchasetable.getSelectedRows().length;
          for(int i = 0; i < numRows ; i++)
          {
            purchasemodel.removeRow(purchasetable.getSelectedRow());
          }
          purchasetable.setModel(purchasemodel);
        }
      }
    }
    );

    purchasesp = new JScrollPane(purchasetable);
    purchasesp.setPreferredSize(new Dimension(240, 150));
   
    buttonexitpurchase = new JButton("X");
    buttonexitpurchase.setPreferredSize(new Dimension(50, 50));

             buttonclear = new JButton("Clear Sale");
    buttonclear.setPreferredSize(new Dimension(190, 50));
 
    // create a purchasepanel
             JPanel purchasepanel = new JPanel();
            
    // add action listeners
             buttonclear.addActionListener(main);
    buttonexitpurchase.addActionListener(main);


             // add elements to purchasepanel
             purchasepanel.add(currentsale);
             purchasepanel.add(purchasesp);
             purchasepanel.add(labelpurchasetotal);
             purchasepanel.add(buttonexitpurchase);
             purchasepanel.add(buttonclear);

             // add purchasepanel to purchaseframe
             purchaseframe.add(purchasepanel);
             purchaseframe.setSize(300, 500);
             purchaseframe.show();
}

  public static void startLibrary()
  {
    butttonlib.setEnabled(false);
 //   buttoncus.setEnabled(true);
   // labelcustom.setVisible(false);
    iconedit.setVisible(true);

    buttoncreate.setVisible(false);

    sp.setVisible(true);

    buttondone.setVisible(false);
    iconpurchase.setVisible(true);


  }

  public static void starteditLibrary()
  {
    butttonlib.setEnabled(false);
   // buttoncus.setEnabled(false);
    iconedit.setVisible(false);

    buttoncreate.setVisible(true);
    iconpurchase.setVisible(false);

    buttondone.setVisible(true);
  }



public void actionPerformed(ActionEvent event)
{
             String s = event.getActionCommand();

             // if the value is a number
             if ((s.charAt(0) >= '0' && s.charAt(0) <= '9')) {
                         // if operand is present then add to second no
                         s0 = s0 + s;

                         // set the value of text
                         //labelcustom.setText("Rp. " + s0);
             }
             else if (s.equals("C")) {
      s0 = "";
                         // set the value of text
                         //labelcustom.setText("Rp. 0");
             }
             else if (s.charAt(0) == '+') {
                         // if there was no operand
                         int te = 0;
      if(s0 == "")
      {
        s0 = "0";
      }
      te = (Integer.parseInt(s0) + Integer.parseInt(st));

      // convert it to string
      st = Integer.toString(te);
      s0 = ""; 

             // set the value of text
                         //labelcustom.setText("Rp. 0");
                         labeltotal.setText("Charge Rp. " + st);
             }
    else if (s.equals("Library"))
    {
      startLibrary();
    }
    else if (s.equals("Custom"))
    {
    //  startCustom();
    }
    else if (s.equals("edit"))
    {
      starteditLibrary();
      edit = true;
    }
    else if (s.equals("Purchase"))
    {
      createPurchaseGUI();
      frame.dispose();
    }
    else if (s.equals("DONE"))
    {
      startLibrary();
      edit = false;
    }
    else if (s.equals("CREATE NEW ITEM"))
    {
      // frame.dispose();
      createItemGUI("new", 0);
    }
    else if (s.equals("SAVE"))
    {
      Object[] row = new Object[6];
      row[0] = (String)labelname.getText();
      row[1] = labelprice.getText();
      row[2] = (String)labelcategory.getText();
      row[3] = (String)labelSKU.getText();
      row[4] = (String)labeldesc.getText();
      row[5] = 0;
      model.addRow(row);
      maintable.setModel(model);
      if(edititem)
      {
        int numRows = maintable.getSelectedRows().length;
        for(int i = 0; i < numRows ; i++)
        {
          model.removeRow(maintable.getSelectedRow());
        }
      }
      itemframe.dispose();

      // frame.show();
    }
    else if (s.equals("X"))
    {
      if(purchase == true)
      {
        purchaseframe.dispose();
        frame.show();
      }
      else
      {
        itemframe.dispose();
      }
    }
    else if (s.equals("DELETE"))
    {
      int numRows = maintable.getSelectedRows().length;
      for(int i = 0; i < numRows ; i++)
      {
        model.removeRow(maintable.getSelectedRow());
      }
      itemframe.dispose();

    }
    else if (s.equals("Clear Sale"))
    {
    //  labelcustom.setText("Rp. 0");
      labeltotal.setText("Charge Rp. 0");
      labelpurchasetotal.setText("Charge Rp. 0");
      s0 = "";
      st = "0";
     
      for (int i = 0; i < purchasemodel.getRowCount(); i++)
      {
        purchasemodel.removeRow(i);
      }
      purchasetable.setModel(purchasemodel);
     
      for (int i = 0; i < model.getRowCount(); i++)
      {
        model.setValueAt(0, i, 5);
      }
    }
  }
 
  public void doA(){
      frame.show();
    }
}
Tampilan Awal

Total Harga yang Harus Dibayar

3. Fitur untuk menambah daftar barang yang dijual, mengedit, atau menghapus :
a. Menambah barang
public static void createItemGUI(String buffer, int row)
{
    if(buffer.compareTo("new") == 0)
    {
      edititem = false;
    }
    else
    {
      edititem = true;
    }

    if(!edititem)
    {
      // create an itemframe
      itemframe = new JFrame("Create New Item");
    }
    else
    {
      itemframe = new JFrame("Edit Item");
    }
             // create a textfield

    labelname = new JTextField(24);
    labelprice = new JTextField(24);
    labelcategory = new JTextField(24);
    labelSKU = new JTextField(24);
    labeldesc = new JTextField(24);
    if(edititem)
    {
      labelname.setText(maintable.getValueAt(row, 0).toString());
      labelprice.setText(maintable.getValueAt(row, 1).toString());
      labelcategory.setText(maintable.getValueAt(row, 2).toString());
      labelSKU.setText(maintable.getValueAt(row, 3).toString());
      labeldesc.setText(maintable.getValueAt(row, 4).toString());
    }
    else
    {
      labelname.setText("Item Name");
      labelprice.setText("Price");
      labelcategory.setText("Category");
      labelSKU.setText("SKU");
      labeldesc.setText("Description"); 
    }
   
    labelname.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelname.getText().equals("Item Name"))
          {
            labelname.setText("");
          }          
        }
    }
    );
   
    labelcategory.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelcategory.getText().equals("Category"))
          {
            labelcategory.setText("");
          }          
        }
    }
    );

    labelprice.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelprice.getText().equals("Price"))
          {
            labelprice.setText("");
          }          
        }
    }
    );
            
    labelSKU.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labelSKU.getText().equals("SKU"))
          {
            labelSKU.setText("");
          }          
        }
    }
    );
   
    labeldesc.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent e)
        {
          if(labeldesc.getText().equals("Description"))
          {
            labeldesc.setText("");
          }          
        }
    }
    );
Menambah Barang Baru

Hasil Menambah Barang Baru

b. Mengedit barang
public static void starteditLibrary()
  {
    butttonlib.setEnabled(false);
   // buttoncus.setEnabled(false);
    iconedit.setVisible(false);

    buttoncreate.setVisible(true);
    iconpurchase.setVisible(false);

    buttondone.setVisible(true);
  }
else if (s.equals("edit"))
    {
      starteditLibrary();
      edit = true;
    }
Kondisi awal sebelum diedit

Harga pulpen diedit
Hasil setelah diedit

c. Menghapus barang
else if (s.equals("DELETE"))
    {
      int numRows = maintable.getSelectedRows().length;
      for(int i = 0; i < numRows ; i++)
      {
        model.removeRow(maintable.getSelectedRow());
      }
      itemframe.dispose();

    }
Kondisi awal sebelum barang dihapus
Barang sedang dihapus
Penghapus telah terhapus

Sekian dari Saya. Terimakasih :)

Tidak ada komentar:

Posting Komentar