Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. /*
  2.  Name: Nick Conklin
  3.  Course: CNT 4714 – Spring 2018
  4.  Assignment title: Program 1 – Event-driven Programming
  5.  Date: Sunday January 28, 2018
  6. */
  7.  
  8. package com.conklin;
  9.  
  10. import javax.swing.*;
  11. import java.awt.event.*;
  12. import java.io.*;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import java.nio.file.Paths;
  16. import java.util.List;
  17. import java.util.ArrayList;
  18.  
  19. public class App extends JFrame{
  20.  
  21.     private JPanel pnlMain;
  22.     private JButton btnProcess;
  23.     private JButton btnConfirm;
  24.     private JButton btnViewOrder;
  25.     private JButton btnFinishOrder;
  26.     private JButton btnNewOrder;
  27.     private JButton btnExit;
  28.     private JTextField txtOrderQty;
  29.     private JTextField txtBookId;
  30.     private JTextField txtBookQty;
  31.     private JTextField txtItemInfo;
  32.     private JTextField txtSubtotal;
  33.     private JLabel lblOrderQty;
  34.     private JLabel lblBookId;
  35.     private JLabel lblBookQty;
  36.     private JLabel lblItemInfo;
  37.     private JLabel lblSubtotal;
  38.  
  39.     int orderQty = 0;
  40.  
  41.     Book getBookById(ArrayList<Book> bookList, String id)
  42.     {
  43.         for (Book b : bookList)
  44.         {
  45.             if (b.bookID.equals(id)) {
  46.                 b.toString();
  47.                 return b;
  48.             }
  49.         }
  50.         JOptionPane.showMessageDialog(null, "Book not found.  Please try again.");
  51.         return null;
  52.     }
  53.  
  54.     // Create an ArrayList of Book objects
  55.     ArrayList<Book> bookList = new ArrayList<Book>();
  56.  
  57.     // Get the String value of the ID and Qty
  58.     String bookID = txtBookId.getText();
  59.     String bookQty = txtBookQty.getText();
  60.  
  61.     public App() {
  62.  
  63.         // Get the file from the directory
  64.         Path fileName = Paths.get("inventory.txt");
  65.  
  66.         // Read through the file line by line
  67.         // Split the lines into an array called 'tokens'
  68.         // Create a Book object and store it in the ArrayList each time through the loop
  69.         try (InputStream in = Files.newInputStream(fileName);
  70.              BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
  71.             String line = null;
  72.             while ((line = reader.readLine()) != null) {
  73.                 String[] tokens = line.split(",");
  74.                 Book newBook = new Book(tokens[0], tokens[1], tokens[2]);
  75.                 bookList.add(newBook);
  76.                 System.out.println(line);
  77.             }
  78.         } catch (IOException x) {
  79.             System.err.println(x);
  80.         }
  81.  
  82.         // Action Listener for the Exit button
  83.         btnExit.addActionListener(e -> System.exit(0));
  84.  
  85.         btnProcess.addActionListener(new ActionListener() {
  86.             @Override
  87.             public void actionPerformed(ActionEvent e) {
  88.                 // Validate the txtOrderQty field to verify a number was entered
  89.                 try {
  90.                     orderQty = Integer.parseInt(txtOrderQty.getText());
  91.                 } catch (NumberFormatException nfe) {
  92.                     JOptionPane.showMessageDialog(null, "Please input a number!");
  93.                 }
  94.  
  95.                 getBookById(bookList, bookID);
  96.                 btnConfirm.setEnabled(false);
  97.             }
  98.         });
  99.     }
  100.  
  101.  
  102.     public static void main(String[] args) {
  103.         JFrame frame = new JFrame("App");
  104.         frame.setContentPane(new App().pnlMain);
  105.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106.         frame.pack();
  107.         frame.setVisible(true);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement