It215 week 9 final bookstore project part 6

//IT215 Week 9 Final
//Bookstore Project Part 6
//

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.NumberFormat; //application uses Number Format
import java.util.Locale; //Application uses United States Currenc

public final class Bookstore extends JFrame //Class: Bookstore
{
    final int MAX = 5;
    Books pt[] = new Books[MAX];

   
    int numBooks; //Keeps track of the number of books in the array
    int curBooks;// The current number of books

    JPanel detailPanel; //JPanels
    JPanel navigation;
    JPanel finalButtons;

    JLabel lb1, lb2, lb3, lb4, lb5, lb6, lb7; //JLabels 1-7
    JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7; //JTextField 1-7
    JButton first, previous, next, last, add, delete, modify, save, search; //JButtons    
   Bookstore() //Bookstore Constructor
   {
        super(“Book Store”); //Frame Title

       numBooks = 0; //Zero books on initial
       curBooks = -1;
       detailPanel = new JPanel();
       detailPanel.setLayout(null);
       first = new JButton(“First”); //Navigation “First” button
       previous = new JButton(“Previous”); //Navigation “Previous” button
       next = new JButton(“Next”);//Navigation “Next” button
       last = new JButton(“Last”);//Navigation “Last” button
       first.addActionListener(new ActionHandler()); //Actionlistener “First” event button
       previous.addActionListener(new ActionHandler()); //Actionlistener “Previous” event button
       next.addActionListener(new ActionHandler()); //Actionlistener “Next” event button
       last.addActionListener(new ActionHandler()); //Actionlistener “Last” event button
//Navigation panel
       navigation = new JPanel();
       navigation.setLayout(new GridLayout(2,2));
       navigation.setBounds(150,0,370,150);
       navigation.add(first);
       navigation.add(previous);
       navigation.add(next);
       navigation.add(last);
       add(navigation);
      
//JLabels
       lb1 = new JLabel(“Title”); //Title Label
       lb2 = new JLabel(“ISBN”); //ISBN Label
       lb3 = new JLabel(“Price”); //Author’s name Label
       lb4 = new JLabel(“Year”); //Year Published Label
       lb5 = new JLabel(“Publisher”); //Publisher’s name label
       lb6 = new JLabel(“Author”); //Price of the book label
       lb7 = new JLabel(“Total”); //Total cost of all books label

//Text fields
       tf1 = new JTextField(20); //Title Text field
       tf2 = new JTextField(20); //ISBN Text field
       tf3 = new JTextField(20); //Author’s name Text field
       tf4 = new JTextField(20); //Year Published Text field
       tf5 = new JTextField(20); //Publisher’s name Text field
       tf6 = new JTextField(20); //Price of the book Text field
       tf7 = new JTextField(20); //Total cost of all books Text field

//Additions for Final Project: buttons
       finalButtons = new JPanel();
       finalButtons.setBounds(0,150,150,350);
       finalButtons.setLayout(new GridLayout(5,1));
       add = new JButton(“Add”);
       delete = new JButton(“Delete”);
       modify = new JButton(“Modify”);
       save = new JButton(“Save”);
       search = new JButton(“Search”);

//Final Project: buttons Actionlisteners
       add.addActionListener(new ActionHandler());
       delete.addActionListener(new ActionHandler());
       modify.addActionListener(new ActionHandler());
       save.addActionListener(new ActionHandler());
       search.addActionListener(new ActionHandler());

       finalButtons.add(add);
       finalButtons.add(delete);
       finalButtons.add(modify);
       finalButtons.add(save);
       finalButtons.add(search);
       add(finalButtons);
     
       setLayout(null);
      
//Set Frame Size
       setSize(528,533);
       setLocation(150,100);
       setResizable(false);

//Panel label positions
       lb1.setBounds(20,30,200,25);   
       lb2.setBounds(20,60,200,25);
       lb3.setBounds(20,90,200,25);
       lb4.setBounds(20,120,200,25);
       lb5.setBounds(20,150,200,25);
       lb6.setBounds(20,180,200,25);
       lb7.setBounds(20,210,200,25);
//Panel Text field positions
       tf1.setBounds(240,30,100,25);
       tf2.setBounds(240,60,100,25);
       tf3.setBounds(240,90,100,25);
       tf4.setBounds(240,120,100,25);
       tf5.setBounds(240,150,100,25);
       tf6.setBounds(240,180,100,25);
       tf7.setBounds(240,210,100,25);
//Removing ability to manually edit text fields
       editText(false);
//Setting text field BG color to WHITE
       tf1.setBackground(Color.WHITE);
       tf2.setBackground(Color.WHITE);
       tf3.setBackground(Color.WHITE);
       tf4.setBackground(Color.WHITE);
       tf5.setBackground(Color.WHITE);
       tf6.setBackground(Color.WHITE);
       tf7.setBackground(Color.WHITE);
//Adding labels to Detail Panel
       detailPanel.add(lb1);
       detailPanel.add(lb2);
       detailPanel.add(lb3);
       detailPanel.add(lb4);
       detailPanel.add(lb5);
       detailPanel.add(lb6);
       detailPanel.add(lb7);
//Adding TF to Detail Panel
       detailPanel.add(tf1);
       detailPanel.add(tf2);
       detailPanel.add(tf3);
       detailPanel.add(tf4);
       detailPanel.add(tf5);
       detailPanel.add(tf6);
       detailPanel.add(tf7);
//Setting Detail Panel to frame
       detailPanel.setBounds(150,150,370,350);
       add(detailPanel);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
   }

//Total value of all items in inventory
   public float total()
    {
    float total = 0;
    for(int i=0; i<numBooks; i++)
    {
        total += pt[i].getTotal();
                  }
    return total;
    }

//bookDisplay method
   public void bookDisplay(int index)
    {
    //Numberformat = US   
    NumberFormat nfc = NumberFormat.getCurrencyInstance(Locale.US);       

    tf1.setText( pt[index].getTitle() );
    tf2.setText( pt[index].getISBN() + “”);
    tf3.setText( nfc.format( pt[index].getPrice() ) );
    tf4.setText( pt[index].getYear() + “”);
    tf5.setText( nfc.format( pt[index].getPublisher() ) );
    tf6.setText( pt[index].getAuthor() );
    tf7.setText( nfc.format(pt[index].getTotal) );
    }

   //this method sorts the array accoding to the names (Sorting technique: Bubble Sort)
   public void arraySort(int curBooks)
    {
    int pass , i;
    int temp;
    Books s = null;
//sorts books with more than one in array
    if(curBooks > 1)
    {
    //make curBooks – 1 passes through the array
    for(pass=1; pass<curBooks; pass++)
    {
        //in each pass shift largest element to right most position
        for(i=0; i<curBooks-pass; i++)
        {
        //comparing the names of Individuals
        temp = (pt[i].getTitle()).compareTo(pt[i+1].getTitle());
            if( temp > 0)
            {
            //if name at i position of array is greater than one at i+1 position than swap
            s = pt[i];
            pt[i] = pt[i+1];
            pt[i+1] = s;
            }//if ends
        }//second for loop ends
    }//first for loop ends
    }//if condition ends
    }

   //Method to enable and disable textfields.
   void editText(boolean state)
   {
       //disable editing of text fields
      tf1.setEditable(state);
      tf2.setEditable(state);
      tf3.setEditable(state);
      tf4.setEditable(state);
      tf5.setEditable(false);
      tf6.setEditable(state);
      tf7.setEditable(false);
   }

//book search by titles
   int searchByTitle(String searchTitle)
   {
       int i;
            for(i=0;i<numBooks;i++)
            {
                if(pt[i].getTitle().equals(searchTitle)) //case sensative matching of Titles
                {
                    curBooks = i;
                    bookDisplay(curBooks);
                    return i;   //return index if match is found
                }
            }
            if(i == numBooks)    // means no match found
            {
                return -1;  //return -1
            }
       return -1;           // no match found
   }
  
   //inner class for handling action events       
   class ActionHandler implements ActionListener
   {
    //handle button event
    public void actionPerformed( ActionEvent event )
    {
    // handling events generated by buttons
                  if(event.getSource() == first)              //moving to first record
    {
                     if(numBooks != 0)
                     {
                        curBooks = 0;
                        bookDisplay(curBooks);
                     }
                     else
                     {
                        JOptionPane.showMessageDialog(null,”No books in program.”);
                     }
                  }
    else if( event.getSource() == previous )    //moving to previous record
    {
                  if(numBooks != 0)                  // array is not empty
                  {
             if( curBooks == 0 )                       // showing first record
             {
                        curBooks = numBooks-1;      // move to last
             }
             else
             {
                        if(numBooks > 1)
                        {
                            curBooks–;                        // move to previous
                        }
             }
                     bookDisplay(curBooks);                  // display in GUI
                  }
                  else
                  {
                     JOptionPane.showMessageDialog(null,”No books in program.”);      // array is empty
                  }
        }
        else if( event.getSource() == next )    // moving to next record
        {
                  if(numBooks != 0)              // array is not empty
                  {
             if( curBooks == numBooks – 1 ) //showing last record
             {
            curBooks = 0;                      // move to first
            bookDisplay(curBooks);
             }
             else
             {
            curBooks++;                        // display next
            bookDisplay(curBooks);
             }
                  }
                  else
                  {
                      JOptionPane.showMessageDialog(null,”No books in program.”);     // array is empty
                  }
        }
        else if( event.getSource() == last )    // moving to last record
        {
                    if(numBooks != 0)            // array is not empty
                    {
            curBooks = numBooks – 1;    // move to last
            bookDisplay( curBooks );
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null,”No books in program.”);   // array is empty
                    }
        }
                else if( event.getSource() == add )  // Adds book info to array
                {
                    if(numBooks == MAX)
                    {
                        JOptionPane.showMessageDialog(null,”No more room in array.”);
                        return;
                    }

//Method to enter book information.
                    String name = JOptionPane.showInputDialog(null,”Enter Book Name(or QUIT to exit.)”);
                    name = name.trim();
                    if(name.equalsIgnoreCase(“quit”))
                    {
                        return;
                    }
                    while (name.isEmpty())        // Makes sure that field is not left empty
                    {
                        name = JOptionPane.showInputDialog(null,”Incorrect Syntax; Enter the Book Name”);
                        name = name.trim();                     // trims any spaces
                    }

                    String ISBN = JOptionPane.showInputDialog(null,”Enter the ISBN”);
                    {
                   
                     while (ISBN.isEmpty())
                        {
                            ISBN = JOptionPane.showInputDialog(null,”Incorrect Syntax; Enter the ISBN”);
                            ISBN = ISBN.trim();
                        }
                    }

                    float price = 0;
                    //try catch used to data other than required format
                    try
                    {
                    price = Float.parseFloat(JOptionPane.showInputDialog(null,”Enter the Book’s Price”));
                    }
                    catch(HeadlessException | NumberFormatException e)
                    {
                        JOptionPane.showMessageDialog(null,”Please enter the correct price.”);
                    }
                    while (price <= 0)
                    {
                        try
                        {
                            price = Float.parseFloat(JOptionPane.showInputDialog(null,”Please enter the correct price.”));
                        }
                        catch(HeadlessException | NumberFormatException e)
                        {
                            JOptionPane.showMessageDialog(null,”Please enter the correct price.”);
                        }
                    }
                    String Year = JOptionPane.showInputDialog(null,”Enter the Year of publishing.”);
                    {
                   
                     while (Year.isEmpty())
                        {
                            Year = JOptionPane.showInputDialog(null,”Incorrect Syntax; Enter the correct Year.”);
                            Year = Year.trim();
                        }
                    }
                    String Publisher = JOptionPane.showInputDialog(null,”Enter the Publisher’s Name.”);
                    {
                   
                     while (Publisher.isEmpty())
                        {
                            Publisher = JOptionPane.showInputDialog(null,”Incorrect Syntax; Enter the Publisher’s Name.”);
                            Publisher = Publisher.trim();
                        }
                    }
                    String Author = JOptionPane.showInputDialog(null,”Enter the Author’s Name.”);
                    {
                   
                     while (Author.isEmpty())
                        {
                            Author = JOptionPane.showInputDialog(null,”Incorrect Syntax; Enter the Author’s Name.”);
                            Author = Author.trim();
                        }
                    }
                    //prompts the user to input units in stock
                    int Total = 0;
                    //try catch used to data other than required format
                    try
                    {
                        Total = Integer.parseInt(JOptionPane.showInputDialog(null,”Enter the total number of books.”));
                    }
                    catch(HeadlessException | NumberFormatException e)
                    {
                        JOptionPane.showMessageDialog(null,”Please enter a positive number”);
                    }
                    while ( Total <= 0)        //Makes sure user inputs a positive number.
                    {
                        try
                        {
                            Total = Integer.parseInt(JOptionPane.showInputDialog(null,”Please enter a positive number.”));//Tells user to input a positve number.
                        }
                        catch(HeadlessException | NumberFormatException e)
                        {
                            JOptionPane.showMessageDialog(null,”Please enter a positive number.”);
                        }
                    }

                    //create new Books object
                    Books book = new Books();

                    //add book to array
                    pt[numBooks] = book;
   
                    //increment total
                    numBooks++;

                    //display in GUI
                    curBooks = numBooks -1;
                    bookDisplay(curBooks);
   
                    // buttons will not be enabled unless user enters at least one detail
                    if(numBooks > 0)
                    {
                        first.setEnabled(true);
                        previous.setEnabled(true);
                        next.setEnabled(true);
                        last.setEnabled(true);
                    }

                    //sort the array
                    arraySort(numBooks);
                }
                else if(event.getSource() == delete)   // handle events from dlt button
                {
                    if(numBooks>=1)          // array is not empty
                    {
                        int index;
                        String deleteByTitle=null;
                        // prompt for name to delete
                        deleteByTitle = JOptionPane.showInputDialog(null,”Enter the name to delete”);
                        if(deleteByTitle == null)
                        {  
                            return;
                        }
                        index = searchByTitle(deleteByTitle); //search name in the array
                        if(index != -1)
                        {
                            //found
                            if(numBooks==1)  // present at first position
                            {
                                pt[0] = null; //delete
                            }
                            else
                            {
                            for(int i=index;i<numBooks-1;i++)    //shift all to right
                            {
                                pt[i]=pt[i+1];
                            }
                            }
                            pt[numBooks-1] = null;   // delete
                            numBooks–;          //one deleted
                            if(numBooks!=0) // check array is not empty
                            {
                            curBooks=0;
                            bookDisplay(curBooks);   // display first
                            }
                            else        // empty so make text fields empty
                            {
                                tf1.setText(“”);
                                tf2.setText(“”);
                                tf3.setText(“”);
                                tf4.setText(“”);
                                tf5.setText(“”);
                                tf6.setText(“”);
                                tf7.setText(“”);
                            }
                            JOptionPane.showMessageDialog(null,”Record deleted successfully”);
                        }
                        else    //not found
                        {
                            JOptionPane.showMessageDialog(null,”Record not available in Array.”);
                        }
                    }
                    else    // array is empty
                    {
                        JOptionPane.showMessageDialog(null,”No Record Available!!!”);
                    }
                }
                //modify button is used for modification
                //first it displays “Modify” when user presses it, it changes
                // to “Update” to update the changes made
                else if(event.getSource() == modify && modify.getText().equals(“Modify”))   //modify a record
                {
                    if(numBooks != 0) //array is not empty
                    {
                        int index;
                        String modifyTitle=null;
                        // prompt for Title
                        modifyTitle = JOptionPane.showInputDialog(null,”Enter Title to Modify”);
                        //search in array
                        if(modifyTitle == null)
                        {
                            return;
                        }
                        index = searchByTitle(modifyTitle);

                        if(index != -1) //found in array
                        {
                            //display in GUI
                            curBooks = index;
                            bookDisplay(curBooks);
                            JOptionPane.showMessageDialog(null,”Record found. Now you can modify the details in textfields. nAfter you are done, please press the Update button.”);
                            //change “Modify” to “Update”
                            modify.setText(“Update”);

                            //enable editing of text fields
                            editText(true);
                        }
                        else    //not found
                        {
                            JOptionPane.showMessageDialog(null,”Record not found.”);
                        }
                    }
                    else    // array is empty
                    {
                        JOptionPane.showMessageDialog(null,”No Record Available!!!”);
                    }
                }
                else if(event.getSource() == modify && modify.getText().equals(“Update”)) //user presses “Update”
                {
                    modify.setText(“Modify”);   //change to “Modify”
                    editText(false);        //disable textfields
                    //use setter functions to update all the data
                    pt[curBooks].setTitle(tf1.getText());
                    pt[curBooks].setISBN(tf2.getText());
                    pt[curBooks].setPrice(Float.parseFloat(tf3.getText().substring(1)));
                    pt[curBooks].setYear(tf4.getText());
                    pt[curBooks].setPublisher(tf5.getText());
                    pt[curBooks].setAuthor(tf6.getText());
                    pt[curBooks].setTotal((long) Float.parseFloat(tf7.getText().substring(1)));
                  
                    bookDisplay(curBooks);
                   
                   
                   
                }
                else if(event.getSource() == save) //events from save button
                {
                    if(numBooks != 0)    // array is not empty
                    {
                    try
                    {
                    //first create the directory
                    File f = new File(“c:\data\it215\”);
                    f.mkdir(); 
                    //now create the file in that directory
                    FileOutputStream outputFile = new FileOutputStream(“c:\data\it215\bookinventory.dat”);
                        try (ObjectOutputStream ob = new ObjectOutputStream(outputFile)) {
                            for(int i=0;i<numBooks;i++)
                            {
                                //one object at time to file
                                ob.writeObject(pt[i]);
                            }   }
                    JOptionPane.showMessageDialog(null,”Save successful.”);
                    }
                    catch(IOException | HeadlessException e)
                    {
                        JOptionPane.showMessageDialog(null,”Error opening file.”);
                    }
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null,”Array is Empty”);
                    }
                }
                else if(event.getSource() == search)    //search button
                {
                    String searchTitle=null;
                    if(numBooks==0)  //empty array
                    {
                        JOptionPane.showMessageDialog(null,”Array is Empty”);
                    }
                    else //prompt fot name to search
                    {
                    searchTitle = JOptionPane.showInputDialog(null,”Enter the Title to search”);
                    if(searchTitle==null)
                    {
                        return;
                    }
                    //method searchByName displays the detail to GUI if found and returns its index
                    if(searchByTitle(searchTitle)!= -1)
                    {
                        JOptionPane.showMessageDialog(null,”Record available in Array.”);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null,”Record not available in Array.”);
                    }
                    }
                }
    }
    }

    // main method execution starts here
    public static void main(String[] args)
    {
    //creates new Bookstore object
    Bookstore tip = new Bookstore();

    } //Ends main method.
class Books extends Book implements Serializable
{
        private Object getTotal;
   
   

    Books()
    {
        super();
       
       
    }
   
    Books(String Title, String ISBN, float Price, String Year, String Publisher, String Author, long Total)
    {
        super();
        }
   
}//class ends
class Book implements Serializable
{
    //Variable specifications
    private String Title;    // Variable that stores the title
    private String ISBN;        // Variable that stores the product ID/number
    private float Price;    // Variable that stores the unit price of the product
        private String Year;
        private String Publisher;
        private String Author;
        private long Total;    // Variable that stores the number of product units in stock
       
    // Constructor for a product that is not specified
    Book()
    {       
        setTitle(“UNKNOWN”);
        setISBN(“UNKNOWN”);
        setPrice(0);
        setTotal(0);               
    }
       
    // Constructor for a product with a Title, ID/number, unit price, and number of units in stock
    Book(String Title, String ISBN, float Price, String Year, String Publisher, String Author, long Total)
    {
        setTitle(Title);
        setISBN(ISBN);
        setPrice(0);
                setYear(Year);
                setPublisher(Publisher);
                setAuthor(Author);
        setTotal(0);
               
    }
//Setters
    public void setTitle (String Title)
    {
        this.Title = Title;
    }
    public void setISBN (String ISBN)
    {
        this.ISBN = ISBN;
    }
    public void setPrice (float Price)
    {
        this.Price = Price;
    }
        public void setYear (String Year)
    {
        this.Year = Year;
    }
    public void setPublisher (String Publisher)
    {
        this.Publisher = Publisher;
    }
        public void setAuthor (String Author)
    {
        this.Author = Author;
    }
    public void setTotal (long Total)
    {
        this.Total = Total;
    }
//Getters       
    public String getTitle()
    {
        return Title;       
    }
    public String getISBN()
    {
        return ISBN;
    }
    public float getPrice()
    {
        return Price;
    }   
        public String getYear()
    {
        return Year;       
    }
        public String getPublisher()
    {
        return Publisher;       
    }
        public String getAuthor()
    {
        return Author;       
    }
    public long getTotal()
    {
        return Total;
    }
                           
    //Computes and Returns total price of all objects
    public float getTotal2()
    {
        return (Total * Price);
    }
           
} // end class Product
} //Ends class Bookstore

Calculate Your Essay Price
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more