Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PurchaseItem extends JFrame implements ActionListener {
- JTextField stockNo = new JTextField (7);
- TextArea information = new TextArea (5, 30);
- JButton check = new JButton ("Check Stock");
- JButton Buy = new JButton ("Buy Item");
- DecimalFormat pounds = new DecimalFormat ("£#,##0.00");
- JTextField quantity = new JTextField (7);
- public PurchaseItem() {
- JFrame PurchaseItem = new JFrame ();
- PurchaseItem.setSize ( 500, 250 );
- PurchaseItem.setLocationRelativeTo (null);
- PurchaseItem.setVisible (true);
- PurchaseItem.setTitle("Purchase Item");
- PurchaseItem.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- PurchaseItem.setResizable(false);
- PurchaseItem.setLayout(new GridBagLayout());
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.weightx = 1;
- gbc.weighty = 1;
- gbc.gridx = 2;
- gbc.gridy = 0;
- PurchaseItem.add(check, gbc);
- gbc.gridx = 1;
- gbc.gridy = 2;
- PurchaseItem.add(information, gbc);
- gbc.gridx = 1;
- gbc.gridy = 3;
- PurchaseItem.add(Buy, gbc);
- gbc.anchor = GridBagConstraints.EAST;
- gbc.gridx = 0;
- gbc.gridy = 0;
- PurchaseItem.add(new JLabel ("Enter Stock Number: "), gbc);
- gbc.gridx = 0;
- gbc.gridy = 1;
- PurchaseItem.add(new JLabel("Enter Quantity: "), gbc);
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.gridx = 1;
- gbc.gridy = 0;
- PurchaseItem.add(stockNo, gbc);
- gbc.gridx = 1;
- gbc.gridy = 1;
- PurchaseItem.add(quantity, gbc);
- Buy.addActionListener(this);
- check.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String key = stockNo.getText();
- String name = StockData.getName(key);
- String q = quantity.getText();
- int stock = StockData.getQuantity(key);
- try {
- int qty = Integer.parseInt(quantity.getText());
- information.requestFocusInWindow();
- } catch (Exception z) {
- if (name == null){
- JOptionPane.showMessageDialog(this, "Enter valid stock number!",
- "Error", JOptionPane.ERROR_MESSAGE);
- information.setText("");
- information.requestFocusInWindow();
- return;
- } else {
- JOptionPane.showMessageDialog(this, "Enter quantity using numbers!",
- "Error", JOptionPane.ERROR_MESSAGE);
- information.setText("");
- information.requestFocusInWindow();
- return;
- }
- } if (e.getSource() == check) {
- int qty = Integer.parseInt(quantity.getText());
- double TotalPrice;
- TotalPrice = (StockData.getPrice(key) * qty);
- information.setText(name);
- information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
- information.append("nTotal in stock: " + StockData.getQuantity(key));
- information.append("nQuantity: " + qty);
- information.append("nTotalPrice: " + pounds.format(TotalPrice));
- } if (e.getSource() == Buy){
- int qty = Integer.parseInt(quantity.getText());
- if (qty <= stock) {
- double TotalPrice;
- TotalPrice = (StockData.getPrice(key) * qty);
- StockData.update(key, - qty);
- information.getText();
- information.setText("Thank you for your order!");
- information.append("nProduct name: " + name);
- information.append("nQuantity: " + qty);
- information.append("nTotal Price: " + pounds.format(TotalPrice));
- information.append("nNow in stock: " + StockData.getQuantity(key));
- } else {
- information.setText("Not enough stock");
- }
- }
- }
- }
- public class ReadDatabase {
- static Connection con;
- static Statement st;
- static PreparedStatement pst;
- static ResultSet rs;
- public static void main(String[] args)throws SQLException{
- try{
- String dbURL = "jdbc:ucanaccess://C:/Users/Lee/Documents/Computing - Year 1/Programming/StockData.accdb";
- String username = "";
- String password = "";
- con = DriverManager.getConnection(dbURL, username, password);
- String query = "select * from Stock";
- pst = con.prepareStatement(query);
- rs = null;
- try{
- rs = pst.executeQuery();
- while(rs.next())
- {
- System.out.println(rs.getString("StockNo") +" "+ rs.getString("StockName") +" "+rs.getDouble("Price") +" "+rs.getString("Quantity"));
- }
- }catch(SQLException sql){}
- pst.close();
- con.close();
- }
- catch(SQLException e)
- {}
- }
- }
- public class StockData {
- private static class Item {
- Item(String n, double p, int q) {
- name = n;
- price = p;
- quantity = q;
- }
- // get methods
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public int getQuantity() {
- return quantity;
- }
- // instance variables
- private String name;
- private double price;
- public int quantity;
- }
- // with a Map you use put to insert a key, value pair
- // and get(key) to retrieve the value associated with a key
- // You don't need to understand how this works!
- private static Map<String, Item> stock = new HashMap<String, Item>();
- static {
- // if you want to have extra stock items, put them in here
- // use the same style - keys should be Strings
- stock.put("00", new Item("Bath towel", 5.50, 10));
- stock.put("11", new Item("Plebney light", 20.00, 5));
- stock.put("22", new Item("Gorilla suit", 30.00, 7));
- stock.put("33", new Item("Whizz games console", 50.00, 8));
- stock.put("44", new Item("Oven", 200.00, 4));
- stock.put("55", new Item("Games Console", 320.00, 15)); //new Stock
- stock.put("66", new Item("Football", 5.99, 20)); //new Stock
- stock.put("77", new Item("Mobile Phone", 599.99, 10)); //new Stock
- stock.put("88", new Item ("Trainers", 99.99, 10)); //new Stock
- stock.put("99", new Item ("Playing Cards", 2.99, 50)); //new Stock
- }
- public static String getName(String key) {
- Item item = stock.get(key);
- if (item == null) {
- return null; // null means no such item
- } else {
- return item.getName();
- }
- }
- public static double getPrice(String key) {
- Item item = stock.get(key);
- if (item == null) {
- return 0; // negative price means no such item
- } else {
- return item.getPrice();
- }
- }
- public static int getQuantity(String key) {
- Item item = stock.get(key);
- if (item == null) {
- return 0; // negative quantity means no such item
- } else {
- return item.getQuantity();
- }
- }
- // update stock levels
- // extra is +ve if adding stock
- // extra is -ve if selling stock
- public static void update(String key, int extra) {
- Item item = stock.get(key);
- if (item != null) {
- item.quantity += extra;
- }
- }
- public static void close() {
- // Does nothing for this static version.
- // Write a statement to close the database when you are using one
- }
- }
- public class UpdateStock extends JFrame implements ActionListener {
- JTextField IncQuantity = new JTextField (7);
- JTextField DecQuantity = new JTextField (7);
- JButton increase = new JButton ("+");
- DecimalFormat pounds = new DecimalFormat ("£#,##0.00");
- JTextField stockNo = new JTextField (7);
- JButton check = new JButton ("Check Stock");
- TextArea information = new TextArea (5, 25);
- public UpdateStock() {
- JFrame UpdateStock = new JFrame ();
- UpdateStock.setSize ( 500, 200 );
- UpdateStock.setLocationRelativeTo (null);
- UpdateStock.setVisible (true);
- UpdateStock.setTitle("Update Stock");
- UpdateStock.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- UpdateStock.setResizable(false);
- UpdateStock.setLayout(new GridBagLayout());
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.weightx = 1;
- gbc.weighty = 1;
- ////First Column////
- gbc.gridx = 1;
- gbc.gridy = 0;
- UpdateStock.add(new JLabel("Enter Stock Number"), gbc);
- //// Second Column /////
- gbc.gridx = 2;
- gbc.gridy = 0;
- UpdateStock.add(stockNo, gbc);
- gbc.gridy++;
- UpdateStock.add(information, gbc);
- ////Third Column////
- gbc.gridx = 3;
- gbc.gridy = 0;
- UpdateStock.add(check, gbc);
- gbc.gridy++;
- UpdateStock.add(IncQuantity, gbc);
- gbc.gridy++;
- UpdateStock.add(increase, gbc);
- check.addActionListener(this);
- increase.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String key = stockNo.getText();
- String name = StockData.getName(key);
- int stock = StockData.getQuantity(key);
- String I = IncQuantity.getText();
- if (name == null) {
- JOptionPane.showMessageDialog(this, "Enter valid stock number!",
- "Error", JOptionPane.ERROR_MESSAGE);
- information.setText("");
- information.requestFocusInWindow();
- return;
- } else {
- information.setText(name);
- information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
- information.append("nNumber in stock: " + StockData.getQuantity(key));
- } try {
- int Inc = Integer.parseInt(IncQuantity.getText());
- information.requestFocusInWindow();
- } catch (Exception z) {
- if (I.isEmpty()){
- return;
- }
- JOptionPane.showMessageDialog(this, "Enter Quantity - Only numbers allowed!",
- "Error", JOptionPane.ERROR_MESSAGE);
- information.setText(name);
- information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
- information.append("nNumber in stock: " + StockData.getQuantity(key));
- information.requestFocusInWindow();
- return;
- } if (e.getSource() == increase) {
- int Inc = Integer.parseInt(IncQuantity.getText());
- StockData.update(key, + Inc);
- information.setText(name);
- information.append("nPrice: " + pounds.format(StockData.getPrice(key)));
- information.append("nNow in stock: " + StockData.getQuantity(key));
- }
- }
- }
- public class UpdateDatabase extends JFrame{
- JLabel JL_StockName,JL_Price,JL_Quantity,JL_StockNo;
- JTextField JT_StockName,JT_Price,JT_Quantity,JT_StockNo;
- JButton btn_insert,btn_update,btn_delete;
- static PreparedStatement pst;
- public UpdateDatabase(){
- super("INSERT UPDATE DELETE");
- JL_StockNo = new JLabel("StockNo:");
- JL_StockName = new JLabel("StockName:");
- JL_Price = new JLabel("Price:");
- JL_Quantity = new JLabel("Quantity:");
- JL_StockNo.setBounds(20, 20, 100, 20);
- JL_StockName.setBounds(20, 50, 100, 20);
- JL_Price.setBounds(20, 80, 100, 20);
- JL_Quantity.setBounds(20, 110, 100, 20);
- JT_StockNo = new JTextField(20);
- JT_StockName = new JTextField(20);
- JT_Price = new JTextField(20);
- JT_Quantity = new JTextField(20);
- JT_StockNo.setBounds(130,20,150,20);
- JT_StockName.setBounds(130, 50, 150, 20);
- JT_Price.setBounds(130, 80, 150, 20);
- JT_Quantity.setBounds(130, 110, 150, 20);
- btn_insert = new JButton("Insert");
- btn_update = new JButton("Update");
- btn_delete = new JButton("Delete");
- btn_insert.setBounds(300, 50, 80, 20);
- btn_update.setBounds(300, 80, 80, 20);
- btn_delete.setBounds(300, 110, 80, 20);
- setLayout(null);
- add(JL_StockNo);
- add(JL_StockName);
- add(JL_Price);
- add(JL_Quantity);
- add(JT_StockNo);
- add(JT_StockName);
- add(JT_Price);
- add(JT_Quantity);
- add(btn_insert);
- add(btn_update);
- add(btn_delete);
- //button insert
- btn_insert.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- theQuery("insert into Stock (StockNo,StockName,Price,Quantity) values('"+JT_StockNo.getText()+"','"+JT_StockName.getText()+"','"+JT_Price.getText()+"',"+JT_Quantity.getText()+")");
- }
- catch(Exception ex){}
- }
- });
- //button update
- btn_update.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- theQuery("update Stock set StockName = '"+JT_StockName.getText()+"',Price = '"+JT_Price.getText()+"', Quantity = "+JT_Quantity.getText()+" where StockNo = "+JT_StockNo.getText());
- }
- catch(Exception ex){}
- }
- });
- //button delete
- btn_delete.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try{
- theQuery("delete from Stock where StockNo = "+JT_StockNo.getText());
- }
- catch(Exception ex){}
- }
- });
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- setLocationRelativeTo(null);
- setSize(500,200);
- }
- //function to execute the insert update delete query
- public void theQuery(String query){
- Connection con = null;
- Statement st = null;
- try{
- con = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Lee/Documents/Computing - Year 1/Programming/StockData.accdb","root","");
- st = con.createStatement();
- st.executeUpdate(query);
- String StockU = "Select * from stock";
- pst = con.prepareStatement(StockU);
- JOptionPane.showMessageDialog(null,"Query Executed");
- rs = pst.executeQuery();
- while(rs.next())
- {
- System.out.println(rs.getString("StockNo") +" "+ rs.getString("StockName") +" "+rs.getDouble("Price") +" "+rs.getString("Quantity"));
- }
- }catch(Exception ex){
- JOptionPane.showMessageDialog(null,ex.getMessage());
- }
- }
- public static void main(String[] args){
- new UpdateDatabase();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement