Skills to be Applied:

Swing/AWT, ArrayList (very similar to ArrayList class)

Classes may be needed:

JApplet, JButton, JTextField, JTextArea, JLabel, Container, JPanel, JTabbedPane, JCheckBox, , and ActionListener. You may use other classes.

How to run an applet program:

-Create an html file, say "hw6.html" with the following content:

--------------------------------------------------------
< html>
< head>
< title>Assignment 6 Applet< /title> < /head>
< body>
< applet code="Assignment6.class" width=500 height=400> < /applet>
< /body>
< /html>
------------------------------------------------------

-Compile your java program as usual.

-In a console, type:

appletviewer hw6.html

(instead of typing "java Assignment6").

-In the TextPad, choose Tool->Run Java Applet

-In the jGrasp,

choose Run->Run as Applet.

Program Description

Suggested Class Diagram: see image.

Write a Java program that constructs an Applet. Your program should provide labels and textfields to a user to enter information regarding books.

The Applet (JApplet) of your program should contain two tabs. The first tab is labeled "Book Addition" and the second tab is labeled "Book Selection".

(The size of the applet here is approximately 800 X 200).

The section under the first tab should be divided into two parts:

The left part contains labels, textfields, and a button for a user to enter some book information. The right part shows "No Book" at the beginning (it is done using JTextArea). see image.

A user can enter some book information, and push "Add a book" button. see image.

Then the book information should appear on the right hand side panel (note that the format of the book information can be using toString() method of the Book class). A message "Book added" should also appear with red color at the top of the panel. see image.

Error handling:

If a user forgets to enter some field and pushes "Add a book" button, show a message "Please enter all fields." with red color, and nothing should be added to the right hand side panel. see image.

If a user enters non-numeric value for book version and book price and pushes "Add a book" button, show a message "Please enter a number for book version and price" with red color, and nothing should be added to the right hand side panel. see image.

After entering several books, the applet will have the following appearance. Note that a scroll pane needs to be added to show multiple books. see image.

Under the "Book Selection" tab, a user can choose books from. There should be a JPanel containing all books created by Book Addition panel and a user can choose books by clicking its check box.

The list of books in the Selection Panel should be exactly same as the list under "Book Addition" tab. see image.

After choosing some books by checking some check boxes, it should show the total price of the chosen books in the JTextField below. This total purchase amount should be updated everytime a user check or uncheck each check box. see image.

A user should be able to go back and forth between "Book Addition" tab and "Book Selection" tab, and these two panels need to have consistent information, i.e., the same list of books.

Class description

SelectPanel

SelectPanel class extends JPanel defined in javax.swing package. It should contain at least the following instance variable:

Attribute name, Attribute type, Description
bookList, ArrayList, a list of Book objects.

This class should have a constructor:

public SelectPanel(ArrayList bookList)

where the parameter "bookList" is passed from the Assignment6 class. The constructor layouts and organizes components in this panel. You will be adding more variables (components) than what is listed here, including a JLabel, a JTextField,and JCheckBoxs.

public void addCheckBox(Book book1)

This method create an object of JCheckBox using the toString of the Book object parameter. This JCheckBox object needs to be added to the panel to be displayed. An object of CheckBoxListener class needs to be added to this JCheckBox so that it listens when a user checks or un-checks. This method needs to be called from the actionPerformed of ButtonListener of the AddPanel class when a new Book object is added.

This class contains a nested class called CheckBoxListener class that implements ItemListener interface. Thus you need to define its itemStateChanged method that is supposed to verify which check boxes are checked and add the price of the corresponding books and display the total purchase amount to the JLabel using the dollar format (Please use NumberFormat class).

AddPanel

AddPanel extends JPanel defined in the javax.swing package. It should contain at least the following instance variable:

Attribute name, Attribute type, Description
bookList, ArrayList, a list of Book objects.
selectPanel, SelectPanel, an object of SelectPanel.

This class should have a constructor:

public AddPanel(ArrayList bookList, SelectPanel selectPanel)

where the parameter "bookList" is passed from the Assignment6 class and the second parameter is an object of SelectPanel. The constructor layouts and organizes components in this panel. You will be adding more variables (components) than what is listed here, including labels, textfields, a button, and a text area.

This class contains a nested class called ButtonListener class that implements ActionListener interface. Thus the ButtonListener needs to have a definition for actionPerformed method that adds some book information to the list and does error handling. See the UML class diagram for the parameter and return type of this method. In the actionPerformed, you need to extract the information from

the textfields for book information. Then you can instantiate an object of the Book class using the information. You can use the toString( ) method of the Book object to display the information on the textarea on the right hand side and also add the Book object to the "bookList". This is also where addCheckBox( ) method of the SelectPanel class needs to be called with its object "selectPanel". so that a new book is added to the AddPanel, the same one is added to the SelectPanel.

Assignment6 class

Assignment6 class extends JApplet defined in javax.swing package. It contains at least init() method (see UML diagram for its return type) to instantiate all instance variables and adds its components to itself. It also sets its size. It contains at least following instance variables:

Attribute name, Attribute type, Description
bookList, ArrayList, a list of Book objects. It will be used in both AddPanel and SelectPanel.
selectPanel, SelectPanel, an object of SelectPanel.
addPanel, AddPanel, an object of AddPanel.
tPane, JTabbedPane, an object JTabbedPane. It will contain AddPanel and selectPanel under each tab.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.