0% found this document useful (0 votes)
21 views

Pos

College project
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Pos

College project
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

import javax.swing.

*;
import java.awt.*;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import
java.sql.PreparedStatement;
import java.sql.SQLException;
import
java.text.SimpleDateFormat;
import java.util.Date;

public class POSSystem extends


JFrame implements
ActionListener {
private JTextArea receiptArea;
private JTextField itemField,
priceField, quantityField;
private JButton addButton,
printButton, saveButton,
clearButton;
private JPanel panel;
private double total = 0;
private StringBuilder receipt =
new StringBuilder();

public POSSystem() {
// Set up the frame
setTitle("HiFi Corp POS
System");
setSize(500, 600);
setDefaultCloseOperation(JFram
e.EXIT_ON_CLOSE);
setLayout(new
BorderLayout());

// Initialize components
receiptArea = new
JTextArea();
receiptArea.setEditable(false);
JScrollPane scrollPane =
new JScrollPane(receiptArea);

itemField = new
JTextField(15);
priceField = new
JTextField(10);
quantityField = new
JTextField(5);
addButton = new
JButton("Add to Cart");
printButton = new
JButton("Print Receipt");
saveButton = new
JButton("Save Receipt");
clearButton = new
JButton("Clear");

addButton.addActionListener(this
);
printButton.addActionListener(thi
s);
saveButton.addActionListener(thi
s);
clearButton.addActionListener(thi
s);

panel = new JPanel();


panel.setLayout(new
GridLayout(5, 2, 10, 10));
panel.add(new
JLabel("Item:"));
panel.add(itemField);
panel.add(new
JLabel("Price:"));
panel.add(priceField);
panel.add(new
JLabel("Quantity:"));
panel.add(quantityField);
panel.add(addButton);
panel.add(printButton);
panel.add(saveButton);

add(scrollPane,
BorderLayout.CENTER);
add(panel,
BorderLayout.NORTH);
add(clearButton,
BorderLayout.SOUTH);

setVisible(true);
}

@Override
public void
actionPerformed(ActionEvent e) {
if (e.getSource() ==
addButton) {
String item =
itemField.getText();
double price =
Double.parseDouble(priceField.g
etText());
int quantity =
Integer.parseInt(quantityField.get
Text());
double itemTotal = price *
quantity;

total += itemTotal;

receipt.append(item).append("\t")
.append(price).appe
nd("\t")
.append(quantity).ap
pend("\t")
.append(itemTotal).a
ppend("\n");
itemField.setText("");
priceField.setText("");
quantityField.setText("");
} else if (e.getSource() ==
printButton) {
String receiptHeader =
"HiFi Corp South Africa\n\n" +
new
SimpleDateFormat("dd/MM/yyyy
HH:mm:ss").format(new Date())
+ "\n\n";
String receiptFooter = "\
nTotal: R" + total + "\nThank you
for shopping with us!";
receiptArea.setText(receiptHead
er + receipt.toString() +
receiptFooter);
} else if (e.getSource() ==
saveButton) {
try (FileWriter writer =
new FileWriter("receipt.txt")) {
writer.write(receiptArea.getText())
;
JOptionPane.showMessageDialo
g(this, "Receipt saved to file!");
} catch (IOException ex) {
ex.printStackTrace();
}

try (Connection conn =


DBConnection.getConnection()) {
String sql = "INSERT
INTO receipts (receipt_text)
VALUES (?)";
PreparedStatement
statement =
conn.prepareStatement(sql);
statement.setString(1,
receiptArea.getText());

statement.executeUpdate();
JOptionPane.showMessageDialo
g(this, "Receipt saved to
database!");
} catch (SQLException
ex) {
ex.printStackTrace();
}
} else if (e.getSource() ==
clearButton) {
itemField.setText("");
priceField.setText("");
quantityField.setText("");
receiptArea.setText("");
receipt.setLength(0);
total = 0;
}
}

public static void main(String[]


args) {
SwingUtilities.invokeLater(new
Runnable() {
@Override
public void run() {
new POSSystem();
}
});
}
}

You might also like