Answer To: Fall 2020: CS 203 - Object-Oriented Programming Assignment 5 Book Store Application XXXXXXXXXXdue on...
Aditya answered on Dec 03 2021
New folder/AddBook.java
New folder/AddBook.java
import javax.swing.JOptionPane;
public class AddBook extends javax.swing.JFrame {
/**
* Creates new form AddBook
*/
public AddBook() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
Title = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
Author = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
Publisher = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
Page = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
Copies = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Book Information");
jLabel2.setText("Title");
jLabel3.setText("Author");
jLabel4.setText("Publisher");
jLabel5.setText("Pages");
jLabel6.setText("Copies");
jButton1.setText("Add Book");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Back");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel2)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addGap(61, 61, 61)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1)
.addComponent(Title)
.addComponent(Author)
.addComponent(Publisher)
.addComponent(Page)
.addComponent(Copies)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(48, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1)
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Author, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(Publisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(Page, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(Copies, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(50, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
BookStore bookStore = new BookStore();
bookStore.setVisible(true);
this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
String title = Title.getText();
String author = Author.getText();
String publisher = Publisher.getText();
String copy = Copies.getText();
String page = Page.getText();
if(title.isEmpty() || author.isEmpty() || publisher.isEmpty() || copy.isEmpty() || page.isEmpty())
{
JOptionPane.showMessageDialog(this, "Fill all the information");
}
else
{
try
{
int nummbberOfPages = Integer.parseInt(page);
int copies = Integer.parseInt(copy);
Book book = new Book(title, author, nummbberOfPages, publisher, copies);
Manager manager = Manager.getInstance();
manager.addBook(book);
int id = book.getBookID();
JOptionPane.showMessageDialog(this, "Book is added to Book Store: "+id);
BookStore bookStore = new BookStore();
bookStore.setVisible(true);
this.dispose();
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this, "Enter valid integer values for number of copies or page");
Copies.setText("");
Page.setText("");
}
}
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField Author;
private javax.swing.JTextField Copies;
private javax.swing.JTextField Page;
private javax.swing.JTextField Publisher;
private javax.swing.JTextField Title;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
// End of variables declaration//GEN-END:variables
}
New folder/AddCustomer.java
New folder/AddCustomer.java
import javax.swing.JOptionPane;
public class AddCustomer extends javax.swing.JFrame {
/**
* Creates new form AddCustomer
*/
public AddCustomer() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
first = new javax.swing.JTextField();
last = new javax.swing.JTextField();
email = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
phone = new javax.swing.JTextField();
AddCustomerButton = new javax.swing.JButton();
backButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Customer Information");
jLabel2.setText("First Name");
jLabel3.setText("Last Name");
jLabel4.setText("Email Address");
jLabel5.setText("Phone Number");
AddCustomerButton.setText("Add Customer");
AddCustomerButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AddCustomerButtonActionPerformed(evt);
}
});
backButton.setText("Back");
backButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(139, 139, 139)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(AddCustomerButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addComponent(backButton))
.addComponent(first)
.addComponent(last)
.addComponent(email)
.addComponent(phone))))
.addContainerGap(34, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jLabel1)
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(first, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(last, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(phone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(AddCustomerButton)
.addComponent(backButton))
.addContainerGap(77, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
// TODO add your handling code here:
BookStore bookStore = new BookStore();
bookStore.setVisible(true);
this.dispose();
}//GEN-LAST:event_backButtonActionPerformed
private void AddCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddCustomerButtonActionPerformed
// TODO add your handling code here:
String firstName = first.getText();
String lastName = last.getText();
String emailAddress = email.getText();
String number = phone.getText();
if(firstName.isEmpty() || lastName.isEmpty() || emailAddress.isEmpty() || number.isEmpty())
{
JOptionPane.showMessageDialog(this, "Fill all the Details");
}
else
{
try
{
int phoneNumber = Integer.parseInt(number);
Customer customer = new Customer(firstName, lastName, emailAddress, phoneNumber);
Manager manager = Manager.getInstance();
manager.addCustomer(customer);
int id = customer.getCustomerId();
JOptionPane.showMessageDialog(this, "New Customer is added with id: "+id);
BookStore bookStore = new BookStore();
bookStore.setVisible(true);
this.dispose();
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this, "Enter a valid phone number");
phone.setText(null);
}
}
}//GEN-LAST:event_AddCustomerButtonActionPerformed
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton AddCustomerButton;
private javax.swing.JButton backButton;
private javax.swing.JTextField email;
private javax.swing.JTextField first;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField last;
private javax.swing.JTextField phone;
// End of variables declaration//GEN-END:variables
}
New folder/Book.java
New folder/Book.java
public class Book {
private int bookID;
private String title;
private String author;
private int numberOfPage;
private String publisher;
private int copies;
private static int autoId = 2001;
public Book(String title, String author, int numberOfPage, String publisher, int copies) {
this.title = title;
this.author = author;
this.numberOfPage = numberOfPage;
this.publisher = publisher;
this.copies = copies;
this.bookID = autoId;
autoId++;
}
@Override
public String toString() {
return "Book{" + "bookID=" + bookID + ", title=" + title + ", author=" + author + ", numberOfPage=" + numberOfPage + ", publisher=" + publisher + ", copies=" + copies + '}';
}
public String getBookInformation()
{
String text = "Book ID: "+bookID+"\nTitle: "+title+"\nAuthor: "+author+"\nNumber Of Pages: "+numberOfPage+"\nPublisher: "+publisher+"\nCopies: "+copies;
return text;
}
public String getFileData()
{
String data = title+"%"+author+"%"+numberOfPage+"%"+publisher+"%"+copies;
return data;
}
public void issueBook()
{
this.copies --;
}
public void returnBook()
{
this.copies++;
}
public int getBookID() {
return bookID;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getNumberOfPage() {
return numberOfPage;
}
public String getPublisher() {
return publisher;
}
public int getCopies() {
return copies;
}
}
New folder/BookRentedByCustomer.java
New folder/BookRentedByCustomer.java
import java.util.ArrayList;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Aditya
*/
public class BookRentedByCustomer extends javax.swing.JFrame {
/**
* Creates new form BookRentedByCustomer
*/
public BookRentedByCustomer() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
lastName = new javax.swing.JTextField();
search = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
bookInformation = new javax.swing.JTextArea();
back = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Book Rented By Customer");
jLabel2.setText("Enter Last Name: ");
search.setText("Search");
search.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchActionPerformed(evt);
}
});
bookInformation.setEditable(false);
bookInformation.setColumns(20);
bookInformation.setRows(5);
jScrollPane1.setViewportView(bookInformation);
back.setText("Back");
back.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addGroup(layout.createSequentialGroup()
.addGap(129, 129, 129)
.addComponent(jLabel1)
.addGap(0, 136, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lastName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(search))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(back)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(lastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(search))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(back)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
// TODO add your handling code here:
BookStore bookStore = new BookStore();
bookStore.setVisible(true);
this.dispose();
}//GEN-LAST:event_backActionPerformed
private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
// TODO add your handling code here:
String lastName = this.lastName.getText();
if(lastName.isEmpty() || lastName == "")
{
JOptionPane.showMessageDialog(this, "");
}
else
{
Manager manager = Manager.getInstance();
Customer customer = manager.getCustomerByLastName(lastName);
if(customer == null)
{
JOptionPane.showMessageDialog(this, "No Customer Exists with this Last Name");
this.lastName.setText("");
}
else
{
ArrayList bookList = customer.getRentedBooks();
if(bookList.isEmpty())
{
bookInformation.setText("No Book is rented by this customer");
}
else
{
String text = "Rented Book Informations \n\n";
for(int i =0;i {
String data = "Book Name: "+bookList.get(i).getTitle()+"\nAuthor: "+bookList.get(i).getAuthor();
text = text + data + "\n\n";
}
bookInformation.setText(text);
}
}
}
}//GEN-LAST:event_searchActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton back;
private javax.swing.JTextArea bookInformation;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField lastName;
private javax.swing.JButton search;
// End of variables declaration//GEN-END:variables
}
New folder/BookStore.java
New...