0% found this document useful (0 votes)
8 views3 pages

MP 6

Uploaded by

Envy n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

MP 6

Uploaded by

Envy n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import javax.swing.

*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MP6 extends JFrame {


JTextField txtItemCode, txtItemQuantity, txtAmountTendered;
JLabel lblDescription, lblPrice, lblTotalAmount, lblChange;

String[] itemCodes = {"001", "002", "003", "004", "005", "006", "007", "008",
"009", "010", "011"};
String[] itemDescriptions = {"Beverage", "Rice", "Apparel", "Liquor", "Flour",
"Vinegar", "Soy Sauce", "Patis", "Beef", "Pork", "Chicken"};
double[] itemPrices = {50, 50, 300, 250, 50, 35, 20, 18, 350, 300, 320};

public MP6() {
setTitle("ABC Company - Point of Sale System");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);

JLabel lblTitle = new JLabel("ABC Company");


lblTitle.setBounds(250, 10, 200, 25);
JLabel lblSubtitle = new JLabel("<< Point of Sale System >>");
lblSubtitle.setBounds(230, 35, 200, 25);

JLabel lblItemCode = new JLabel("Item Code:");


lblItemCode.setBounds(50, 80, 100, 25);
txtItemCode = new JTextField();
txtItemCode.setBounds(150, 80, 150, 25);

JLabel lblItemDescription = new JLabel("Item Description:");


lblItemDescription.setBounds(350, 80, 150, 25);
lblDescription = new JLabel(" ");
lblDescription.setBounds(470, 80, 100, 25);

JLabel lblItemPrice = new JLabel("Item Price:");


lblItemPrice.setBounds(50, 120, 100, 25);
lblPrice = new JLabel(" ");
lblPrice.setBounds(150, 120, 150, 25);

JLabel lblItemQuantity = new JLabel("Item Quantity:");


lblItemQuantity.setBounds(350, 120, 150, 25);
txtItemQuantity = new JTextField();
txtItemQuantity.setBounds(470, 120, 100, 25);

JLabel lblTotalAmountLabel = new JLabel("Total Amount:");


lblTotalAmountLabel.setBounds(50, 160, 100, 25);
lblTotalAmount = new JLabel(" ");
lblTotalAmount.setBounds(150, 160, 150, 25);

JLabel lblAmountTendered = new JLabel("Amount Tendered:");


lblAmountTendered.setBounds(350, 160, 150, 25);
txtAmountTendered = new JTextField();
txtAmountTendered.setBounds(470, 160, 100, 25);

JLabel lblChangeLabel = new JLabel("Change:");


lblChangeLabel.setBounds(50, 200, 100, 25);
lblChange = new JLabel(" ");
lblChange.setBounds(150, 200, 150, 25);
JButton btnCompute = new JButton("Compute");
btnCompute.setBounds(150, 260, 100, 25);
JButton btnClear = new JButton("Clear");
btnClear.setBounds(260, 260, 100, 25);
JButton btnExit = new JButton("Exit");
btnExit.setBounds(370, 260, 100, 25);

add(lblTitle);
add(lblSubtitle);
add(lblItemCode);
add(txtItemCode);
add(lblItemDescription);
add(lblDescription);
add(lblItemPrice);
add(lblPrice);
add(lblItemQuantity);
add(txtItemQuantity);
add(lblTotalAmountLabel);
add(lblTotalAmount);
add(lblAmountTendered);
add(txtAmountTendered);
add(lblChangeLabel);
add(lblChange);
add(btnCompute);
add(btnClear);
add(btnExit);

btnCompute.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
computeTotal();
}
});

btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFields();
}
});

btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

setVisible(true);
}

void computeTotal() {
String itemCode = txtItemCode.getText();
int quantity;
double amountTendered;

try {
quantity = Integer.parseInt(txtItemQuantity.getText());
amountTendered = Double.parseDouble(txtAmountTendered.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter valid numeric values
for Quantity and Amount Tendered.");
return;
}

int itemIndex = -1;


for (int i = 0; i < itemCodes.length; i++) {
if (itemCodes[i].equals(itemCode)) {
itemIndex = i;
break;
}
}

if (itemIndex == -1) {
JOptionPane.showMessageDialog(this, "Invalid Item Code.");
return;
}

String description = itemDescriptions[itemIndex];


double price = itemPrices[itemIndex];
double totalAmount = price * quantity;
double change = amountTendered - totalAmount;

lblDescription.setText(description);
lblPrice.setText(String.format("%.2f", price));
lblTotalAmount.setText(String.format("%.2f", totalAmount));
lblChange.setText(String.format("%.2f", change < 0 ? 0 : change));

if (change < 0) {
JOptionPane.showMessageDialog(this, "Insufficient Amount Tendered.");
}
}

void clearFields() {
txtItemCode.setText("");
txtItemQuantity.setText("");
txtAmountTendered.setText("");
lblDescription.setText(" ");
lblPrice.setText(" ");
lblTotalAmount.setText(" ");
lblChange.setText(" ");
}

public static void main(String[] args) {


new MP6();
}
}

You might also like