Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Nick Conklin
- Course: CNT 4714 – Spring 2018
- Assignment title: Program 1 – Event-driven Programming
- Date: Sunday January 28, 2018
- */
- package com.conklin;
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.List;
- import java.util.ArrayList;
- public class App extends JFrame{
- private JPanel pnlMain;
- private JButton btnProcess;
- private JButton btnConfirm;
- private JButton btnViewOrder;
- private JButton btnFinishOrder;
- private JButton btnNewOrder;
- private JButton btnExit;
- private JTextField txtOrderQty;
- private JTextField txtBookId;
- private JTextField txtBookQty;
- private JTextField txtItemInfo;
- private JTextField txtSubtotal;
- private JLabel lblOrderQty;
- private JLabel lblBookId;
- private JLabel lblBookQty;
- private JLabel lblItemInfo;
- private JLabel lblSubtotal;
- int orderQty = 0;
- Book getBookById(ArrayList<Book> bookList, String id)
- {
- for (Book b : bookList)
- {
- if (b.bookID.equals(id)) {
- b.toString();
- return b;
- }
- }
- JOptionPane.showMessageDialog(null, "Book not found. Please try again.");
- return null;
- }
- // Create an ArrayList of Book objects
- ArrayList<Book> bookList = new ArrayList<Book>();
- // Get the String value of the ID and Qty
- String bookID = txtBookId.getText();
- String bookQty = txtBookQty.getText();
- public App() {
- // Get the file from the directory
- Path fileName = Paths.get("inventory.txt");
- // Read through the file line by line
- // Split the lines into an array called 'tokens'
- // Create a Book object and store it in the ArrayList each time through the loop
- try (InputStream in = Files.newInputStream(fileName);
- BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
- String line = null;
- while ((line = reader.readLine()) != null) {
- String[] tokens = line.split(",");
- Book newBook = new Book(tokens[0], tokens[1], tokens[2]);
- bookList.add(newBook);
- System.out.println(line);
- }
- } catch (IOException x) {
- System.err.println(x);
- }
- // Action Listener for the Exit button
- btnExit.addActionListener(e -> System.exit(0));
- btnProcess.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // Validate the txtOrderQty field to verify a number was entered
- try {
- orderQty = Integer.parseInt(txtOrderQty.getText());
- } catch (NumberFormatException nfe) {
- JOptionPane.showMessageDialog(null, "Please input a number!");
- }
- getBookById(bookList, bookID);
- btnConfirm.setEnabled(false);
- }
- });
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame("App");
- frame.setContentPane(new App().pnlMain);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement