Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with my JAVA code. I need help with my btnProcessItem actionlistener function. In this function when the button is pressed, it checks

I need help with my JAVA code. I need help with my btnProcessItem actionlistener function. In this function when the button is pressed, it checks for if a string entered by a user equals an element in a 2d array. If the string matches an element, it proceeds with code without a problem. When the string doesn't match an element in the 2d array, the code displays a JOptionPane message saying the string entered doesn't match, and it resets the text field entry. However, when I click the 'ok' button in the JOptionPane, the JOptionPane doesn't disappear. I need this JOptionPane to disappear and allow the user to enter a new ID so that it can be processed and checked with the 2d array to see if it exist. I've posted my code below. I only need help with my btnProcessItem actionlistener function.

I would appreciate any help.

/*Name: Kenneth Gonzalez

* Course: CNT 4714 - Spring 2018

* Assignment Title: Program 1 - Event Driven Programming

* Date: Sunday January 28, 2018

*/

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.awt.event.ActionEvent;

import java.awt.Font;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import java.lang.String;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.awt.Color;

public class Project1GUI {

private JFrame frame;

private JTextField txtFieldNumOfItems;

private JTextField BookID;

private JTextField Quantity;

private JTextField BookInformation;

private JTextField Subtotal;

private JButton btnProcessItem;

private JButton btnConfirmItem;

private JButton btnViewOrder;

private JButton btnFinishOrder;

// JLabel lblBookID;

private int numOfItems;

int quantityOfBook;

private int count = 1;

private int subTotalCount = 0;

private double subTotalforItems = 0;

private static String array2DFromFile[][];

private static String arrayFromFile[];

String [][]array2 = null;

static int numRows;

static int numCols;

int count1 = 0;

int bookIDProc = 0;

double subTotal = 0;

String everything = null;

String ViewOrderOutput="";

String FinishOrderOutput="";

String text;

String numberList;

String s = null;

String result;

String discount = null;

ArrayList numbers = new ArrayList();

ArrayList cart = new ArrayList();

static ArrayList list = new ArrayList();

ArrayList itemsForTransactionFile = new ArrayList();

/**

* Launch the application.

*/

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

try

{

Project1GUI window = new Project1GUI();

window.frame.setVisible(true);

//Calling the start function that begins by reading in the file.

start();

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public static void start()

{

//Parses the file and returns the elements in a 2D array.

array2DFromFile = ReadFile.convertTo2DArray();

numCols = array2DFromFile[0].length;

numRows = array2DFromFile.length;

//Add the elements of the 2D array to a 1D array so we can check if a BOOKID exist or doesn't exist.

for(int x = 0; x < numRows; x++)

{

for(int y = 0; y < numCols; y++)

{

list.add(array2DFromFile[x][y]);

}

}

arrayFromFile = new String[list.size()];

for(int i = 0; i < arrayFromFile.length; i++)

{

arrayFromFile[i] = list.get(i);

}

}

/**

* Create the application.

*/

public Project1GUI() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

@SuppressWarnings("deprecation")

private void initialize() {

frame = new JFrame();

frame.getContentPane().setBackground(Color.GREEN);

frame.setBounds(100, 100, 920, 432);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

txtFieldNumOfItems = new JTextField();

txtFieldNumOfItems.setBounds(370, 50, 376, 22);

frame.getContentPane().add(txtFieldNumOfItems);

txtFieldNumOfItems.setColumns(10);

BookID = new JTextField();

BookID.setBounds(370, 97, 376, 22);

frame.getContentPane().add(BookID);

BookID.setColumns(10);

Quantity = new JTextField();

Quantity.setBounds(370, 147, 376, 22);

frame.getContentPane().add(Quantity);

Quantity.setColumns(10);

BookInformation = new JTextField();

BookInformation.setBounds(370, 197, 376, 22);

frame.getContentPane().add(BookInformation);

BookInformation.setColumns(10);

BookInformation.setEditable(false);

Subtotal = new JTextField();

Subtotal.setBounds(370, 251, 376, 22);

frame.getContentPane().add(Subtotal);

Subtotal.setColumns(10);

Subtotal.setEditable(false);

JLabel lblItemsInOrder = new JLabel("Enter the number of items in this order: ");

lblItemsInOrder.setFont(new Font("Times New Roman", Font.PLAIN, 16));

lblItemsInOrder.setBounds(43, 53, 274, 19);

frame.getContentPane().add(lblItemsInOrder);

JLabel lblBookID = new JLabel("Enter BookID for item #" + count +":");

lblBookID.setFont(new Font("Times New Roman", Font.PLAIN, 16));

lblBookID.setBounds(43, 100, 274, 16);

frame.getContentPane().add(lblBookID);

JLabel lblQuantity = new JLabel("Enter quantity for item #" + count +":");

lblQuantity.setFont(new Font("Times New Roman", Font.PLAIN, 16));

lblQuantity.setBounds(43, 150, 274, 16);

frame.getContentPane().add(lblQuantity);

JLabel lblItemInfo = new JLabel("Item #" + count + " info:");

lblItemInfo.setFont(new Font("Times New Roman", Font.PLAIN, 16));

lblItemInfo.setBounds(43, 200, 274, 16);

frame.getContentPane().add(lblItemInfo);

JLabel lblSubtotal = new JLabel("Order Subtotal for " +subTotalCount+ " item(s):");

lblSubtotal.setFont(new Font("Times New Roman", Font.PLAIN, 16));

lblSubtotal.setBounds(43, 254, 274, 16);

frame.getContentPane().add(lblSubtotal);

btnProcessItem = new JButton("Process Item # " + count);

btnProcessItem.setBounds(43, 337, 131, 25);

frame.getContentPane().add(btnProcessItem);

//If Process Item button is pressed take certain actions

btnProcessItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0)

{

//Disable how many items the customer wants after they press the Process Item button.

numOfItems = Integer.parseInt(txtFieldNumOfItems.getText());

txtFieldNumOfItems.setEditable(false);

//Check to see if the ID entered is 5 characters long

if(count1 < numOfItems)

{

if(BookID.getText().length() == 5)

{

s = BookID.getText();

bookIDProc = Integer.parseInt(BookID.getText());

}

else if(BookID.getText().length() > 5)

{

JOptionPane.showMessageDialog(frame, "Please enter a five digit number!");

System.exit(0);

}

else if(BookID.getText().length() < 5)

{

JOptionPane.showMessageDialog(frame, "Please enter a five digit number!");

System.exit(0);

}

//Converts text user entered to int and assigns to quantityOfBook

quantityOfBook = Integer.parseInt(Quantity.getText());

//Checks if the bookID entered by the user matches the bookID from array that was read from a file

for(int x = 0; x < numRows; x++)

{

for(int y = 0; y < 1; y++)

{

//Checks that the string s entered by the user matches an item in the 2D array

if(array2DFromFile[x][0].equals(s))

{

if(quantityOfBook <= 4)

{

discount = ("0");

BookInformation.setText(array2DFromFile[x][0] + array2DFromFile[x][1] + array2DFromFile[x][2] +" " +quantityOfBook+" " + discount +"% "+ array2DFromFile[x][2]);

}

else if(quantityOfBook >= 5 && quantityOfBook <= 9)

{

discount = ("10");

BookInformation.setText(array2DFromFile[x][0] + array2DFromFile[x][1] + array2DFromFile[x][2] +" " +quantityOfBook+" " +discount +"% " +array2DFromFile[x][2]);

}

else if(quantityOfBook >= 10 && quantityOfBook <= 14)

{

discount = ("15");

BookInformation.setText(array2DFromFile[x][0] + array2DFromFile[x][1] + array2DFromFile[x][2] +" " +quantityOfBook+" " +discount+"% " +array2DFromFile[x][2]);

}

else if(quantityOfBook >= 15)

{

discount = ("20");

BookInformation.setText(array2DFromFile[x][0] + array2DFromFile[x][1] + array2DFromFile[x][2] +" " +quantityOfBook+" "+ discount+"% " +array2DFromFile[x][2]);

}

//Keeps track of how many items have been processed. Once the nth item has been processed some of the components become disabled

count1++;

if(count1 == numOfItems)

{

BookID.setEditable(false);

Quantity.setEditable(false);

lblQuantity.setVisible(false);

lblBookID.setVisible(false);

btnProcessItem.setText("Process Item");

btnProcessItem.setEnabled(false);

}

}

//If value is not found it will display the following message

else if(!list.contains(s))

{

/*This is where I have a problem. The message shows and when I click 'ok' the content is cleared from BookID and Quantity however

* the dialog message pane doesn't disappear. I need the code to allow the user to enter text to BookID and quantity and allow them to process

* the new ID they entered. I need to repeat this process everytime the user enters the wrong BookID, but it doesnt work. It stay stuck on

* with the message dialog.

*/

JOptionPane.showMessageDialog(frame, "BookID " +s+ " does not exist.");

//System.out.println("Hola");

BookID.setText("");

Quantity.setText("");

//System.exit(0);

}

}

}

/*Check to see if the Process JButton has been pressed; if pressed will disable Process button and enable the Confirm Item button. */

if(arg0.getSource() == btnProcessItem)

{

btnProcessItem.setEnabled(false);

if(btnProcessItem.isEnabled()==false)

{

btnConfirmItem.setEnabled(true);

}

}

//}

//Checks to see when we have reached the item count the user entered in the beginning. Once reached the program will disable the following features.

/*count1++;

if(count1 == numOfItems)

{

BookID.setEditable(false);

Quantity.setEditable(false);

lblQuantity.setVisible(false);

lblBookID.setVisible(false);

btnProcessItem.setText("Process Item");

btnProcessItem.setEnabled(false);

}*/

}

}

});

btnConfirmItem = new JButton("Confirm item #" + count);

btnConfirmItem.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{

btnViewOrder.setEnabled(true);

btnFinishOrder.setEnabled(true);

btnConfirmItem.setEnabled(false);

btnProcessItem.setEnabled(true);

s = BookID.getText();

for(int x = 0; x < numRows; x++)

{

for(int y = 0; y < 1; y++)

{

if(array2DFromFile[x][0].equals(s))

{

JOptionPane.showMessageDialog(frame, "Item # " +count+ " accepted");

BookID.setText("");

Quantity.setText("");

subTotal = Summation.subTotal(array2DFromFile[x][2], quantityOfBook);

subTotalforItems += subTotal;

String number = Double.toString(subTotalforItems);

subTotalCount++;

lblSubtotal.setText("Order Subtotal for " +subTotalCount+ " item(s):");

Subtotal.setText("$" +number);

StringBuilder builder = new StringBuilder();

BookInformation.setText(array2DFromFile[x][0] + array2DFromFile[x][1] + array2DFromFile[x][2] + " " +quantityOfBook + " "+ discount + " "+subTotal);

builder.append(array2DFromFile[x][0]).append(", ").append(array2DFromFile[x][1]).append(", ").append("$").append(array2DFromFile[x][2]).append(", ").append(quantityOfBook).append(", ").append(discount).append(", ").append("$").append(subTotal);

result = builder.toString();

itemsForTransactionFile.add(result);

}

}

}

text = BookInformation.getText();

cart.add(text);

numberList = Integer.toString(count);

numbers.add(numberList);

count = Integer.parseInt(numberList);

count++;

lblBookID.setText("Enter BookID for item #" + count +":");

lblQuantity.setText("Enter quantity for item #" + count + ":");

btnProcessItem.setText("Process Item # " + count);

btnConfirmItem.setText("Confirm item #" + count);

if(count1 == numOfItems)

{

btnProcessItem.setText("Process Item");

btnProcessItem.setEnabled(false);

btnConfirmItem.setText("Confirm Item");

}

}

});

btnConfirmItem.setBounds(186, 337, 131, 25);

frame.getContentPane().add(btnConfirmItem);

btnConfirmItem.setEnabled(false);

btnViewOrder = new JButton("View Order");

btnViewOrder.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{

for(int i =0; i

{

everything = cart.get(i).toString();

String everything2 = numbers.get(i).toString();

ViewOrderOutput += everything2 +". " + " " +everything+ " ";

}

JOptionPane.showMessageDialog(frame, ViewOrderOutput);

}

});

btnViewOrder.setBounds(329, 337, 131, 25);

frame.getContentPane().add(btnViewOrder);

btnViewOrder.setEnabled(false);

btnFinishOrder = new JButton("Finish Order");

btnFinishOrder.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

double TaxAmount = subTotalforItems * .06;

double orderTotal = TaxAmount + subTotalforItems;

Date today = new Date();

SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyMMddHHmmSS");

String date = DATE_FORMAT.format(today);

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");

LocalDateTime now = LocalDateTime.now();

JOptionPane.showMessageDialog(frame, dtf.format(now) + " Number of line items: " + count1 + " Item# / ID / Title / Price / Qty / Disc % / Subtotal: " +

" "+ ViewOrderOutput +" Order Subtotal: $" + subTotalforItems + " Tax Rate: 6% "+ "Tax Amount:\t $" + TaxAmount +

" Order total:\t $" +orderTotal +" Thanks for shopping at the Ye Olde Book Shoppe!");

ArrayList newlist = new ArrayList();

for(int x = 0; x < itemsForTransactionFile.size(); x++)

{

StringBuilder builder = new StringBuilder();

String result = null;

builder.append(date).append(", ").append(itemsForTransactionFile.get(x)).append(", ").append(now);

result = builder.toString();

newlist.add(result);

}

String []results = newlist.toArray(new String[newlist.size()]);

//Writes to transaction file

/*BufferedWriter outputWriter = null;

try {

outputWriter = new BufferedWriter(new FileWriter("transaction.txt"));

for(int x = 0; x < results.length; x++)

{

outputWriter.write(results[x]);

outputWriter.newLine();

}

outputWriter.flush();

outputWriter.close();

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}*/

try {

File file = new File("transaction.txt");

if(!file.exists()) {

file.createNewFile();

}

FileWriter fw = new FileWriter(file, true);

BufferedWriter bw = new BufferedWriter(fw);

for(int x = 0; x < results.length; x++)

{

bw.write(results[x]);

bw.newLine();

}

bw.close();

}

catch(IOException ioe) {

ioe.printStackTrace();

}

}

});

btnFinishOrder.setBounds(472, 337, 131, 25);

frame.getContentPane().add(btnFinishOrder);

btnFinishOrder.setEnabled(false);

JButton btnNewOrder = new JButton("New Order");

btnNewOrder.setBounds(615, 337, 131, 25);

frame.getContentPane().add(btnNewOrder);

/*This JButton will exit the system when the user clicks it*/

JButton btnExit = new JButton("Exit");

btnExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource() == btnExit) System.exit(0);

}

});

btnExit.setBounds(758, 337, 131, 25);

frame.getContentPane().add(btnExit);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

Identify and control your anxieties

Answered: 1 week ago

Question

Understanding and Addressing Anxiety

Answered: 1 week ago