//Import needed packages
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link];
public class BookStoreGUI extends JFrame {
private static final int WINDOW_WIDTH= 800; //Width of GUI frame
private static final int WINDOW_LENGTH = 250; //Length of GUI frame
private JPanel booksPanel; //Holds all the books
private JPanel buttonsPanel; //Has add/remove/checkout buttons
private JPanel shoppingCartPanel; //To hold books added by user
private JPanel bannerPanel; //Banner panel
private JPanel searchButtonsPanel; //Holds search/showall buttons
private JList booksList; //List with all book names
private JList selectedList; //List in shopping cart
private JButton addSelected; //Adds book to shopping cart
private JButton removeSelected; //Removes book from shopping cart
private JButton checkOut; //Adds all books prices + taxes
private JButton searchButton; //Searches for desired book
private JButton showAllButton; //shows all books available
private bookInfo booksInfo = new bookInfo(); //BookInfo object
private String[] bookNames = [Link](); //Array that Holds
all book names
private double[] bookPrices = [Link]();//Array that holds
all book prices
private JScrollPane scrollPane1; //Holds available books list
private JScrollPane scrollPane2; //Holds selected book list
private JLabel panelTitle; //Panel title
private JLabel cartTitle; //Panel title
private JLabel banner; //Panel title
private JTextField searchField; //Allows user to input search
private int element = -1; // control variable
private int selectedIndex; //Index selected among available
books
private int index; //Int that holds selected
index.
private int i,count; //Control variables
private double total; //Total of prices
private double bookPrice; //Hold book prices
private final double TAX=0.06; //Constant for tax value
private ListModel books; //List model for book name list
private ListModel shoppingCart; //List model for shopping cart list
private DefaultListModel shoppingCartDFM;
private DecimalFormat money; //Money format
private Object selectedBookName; //Selected book
private String searchResults; //Hold search results
private String notFound = " Title not found"; //Holds search results
private boolean found = false; //Boolean holds false if search
results not found
/*Constructor
* BookStoreGUI - Buuilds a GUI with multiple panels
*/
public BookStoreGUI() throws IOException{
//Title of GUI
setTitle("Book Store Shopping Cart");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(WINDOW_WIDTH, WINDOW_LENGTH);
//BuildPanels
buildBooksPanel();
buildButtonsPanel();
buildShoppingCartPanel();
buildBannerPanel();
buildSearchButtonsPanel();
//Add panels to GUI frame
add(bannerPanel,[Link]);
add(booksPanel, [Link]);
add(buttonsPanel, [Link]);
add(shoppingCartPanel, [Link]);
add(searchButtonsPanel, [Link]);
//set visibility
setVisible(true);
pack();
}
//METHODS
/*
*buildBooksPanel() - Builds panel containing a JList/ScrollPane
*/
public void buildBooksPanel(){
//Create panel to hold list of books
booksPanel = new JPanel();
//Set Panel layout
[Link](new BorderLayout());
//Create the list
booksList = new JList(bookNames);
//Set selection preferrence
[Link](ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//Visible book names
[Link](5);
//Create scroll pane containing book list
scrollPane1 = new JScrollPane(booksList);
[Link](new Dimension(175,50));
//JLabel/Panel title
panelTitle = new JLabel("Available Books");
//Add JLabel and scroll to panel
[Link](panelTitle, [Link]);
[Link](scrollPane1);
}
/*
* buildButtonsPanel - builds panel containing add/remove/checkout buttons
*/
public void buildButtonsPanel(){
//Create panel to hold buttons
buttonsPanel = new JPanel();
//Set Layout
[Link](new GridLayout(3,1));
//Create Buttons
addSelected = new JButton("Add Selected Item");
removeSelected = new JButton("Remove Selected Item");
checkOut = new JButton("Check Out");
//add Listeners
[Link](new AddButtonListener());
[Link](new RemoveButtonListener());
[Link](new CheckOutButtonListener());
//Add button panel to GUI
[Link](addSelected);
[Link](removeSelected);
[Link](checkOut);
}
/*
* buildShoppingCartPanel builds panel containing JList/Scroll pane
*/
public void buildShoppingCartPanel(){
//Create panel
shoppingCartPanel = new JPanel();
//Set panel layout
[Link](new BorderLayout());
//Create shopping cart list
selectedList = new JList();
//Set row visility
[Link](5);
//Create scrollpane containin selected list items
scrollPane2 = new JScrollPane(selectedList);
[Link](new Dimension(175,50));
//JLabel/Panel title
cartTitle = new JLabel("Shopping Cart ");
//Add JLabel and scroll pane to panel
[Link](cartTitle, [Link]);
[Link](scrollPane2);
}
/*
* buildBannerPanel - builds panel containing banner for GUI
*/
public void buildBannerPanel(){
//Create panel
bannerPanel = new JPanel();
//Set Border layout
setLayout(new BorderLayout());
//String containing JLabel text
String labelText= "<html><b COLOR=RED> Welcome</b>" + "<b><i
COLOR=#006363> To </i></b>" +
"<b><u COLOR=#BF3030>As</u><u
COLOR=#8170D8>The</u><u COLOR=#00CC00>Pages</u><u COLOR=BLUE>[Link]</u></b>" ;
//create JLabel
JLabel banner = new JLabel(labelText);
[Link](new Font("Serif",[Link],28));
//add banner to panel
[Link](banner);
}
/*
* buildSearchButtonsPanel - builds panel containing search and showall
buttons
*/
public void buildSearchButtonsPanel(){
//Create panel
searchButtonsPanel = new JPanel();
//Set Border layout
[Link](new GridLayout(1, 3 ,5,5));
//Create buttons
searchButton = new JButton("Search");
showAllButton = new JButton("Show All");
//Create text field
searchField = new JTextField(15);
//Add listeners
[Link](new SearchButtonListener());
[Link](new ShowAllButtonListener());
//Add buttons and text field to panel
[Link](searchField);
[Link](searchButton);
[Link](showAllButton);
}
//ACTION LISTENERS
/*
* AddButtonListener - adds selected item to shopping cart upon selection
*/
public class AddButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
selectedIndex = [Link]();
selectedBookName = [Link]();
books = [Link]();
shoppingCart = [Link]();
shoppingCartDFM = new DefaultListModel();
for(count=0; count<[Link](); count++){
[Link]([Link](count));
}
if(element == -1)
bookPrice += bookPrices[selectedIndex];
else
bookPrice += bookPrices[element];
[Link](selectedBookName);
[Link](shoppingCartDFM);
}
}
/*
* RemoveButtonListener - Removes selected item from shopping cart upon
selection
*/
public class RemoveButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
index = [Link]();
((DefaultListModel)[Link]()).remove(index);
if(element == -1)
if(bookPrices[selectedIndex] <= bookPrice)
bookPrice -= (bookPrices[selectedIndex]);
else
bookPrice = (bookPrices[index]) - bookPrice;
else
if(bookPrices[element] <= bookPrice)
bookPrice -= (bookPrices[element]);
else
bookPrice = (bookPrices[index]) - bookPrice;
}
}
/*
* CheckOutButtonListener - Calculates total and displays it to user
*/
public class CheckOutButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
money = new DecimalFormat("#,##0.00");
total = (bookPrice + (bookPrice*TAX));
[Link](null, "Subtotal: $" +
([Link](bookPrice)) + "\n" +
"Tax: $" +
([Link]((bookPrice*TAX))) + "\n" +
"Total: $" +
([Link](total)));
}
}
/*
* SearchButtonListener - searches for user desired item
*/
public class SearchButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
index = 0;
while(!found && index < [Link])
{
if(bookNames[index].equals([Link]())){
found = true;
element = index;
}
index++;
}
if(element == -1){
[Link](new DefaultListModel());
((DefaultListModel)[Link]()).addElement(notFound);
}
else{
searchResults = bookNames[element];
[Link](new DefaultListModel());
((DefaultListModel)[Link]()).addElement(searchResults);
}
}
}
/*
* ShowsAllButtonListener - shows all available books
*/
public class ShowAllButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
[Link](new DefaultListModel());
for(i=0; i < [Link]; i++){
((DefaultListModel)[Link]()).addElement(bookNames[i]);
}
}
}
public static void main(String[] args) throws IOException{
new BookStoreGUI();
}
}