Академический Документы
Профессиональный Документы
Культура Документы
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
import javax.swing.*;
import java.text.DecimalFormat;
/*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();
//set visibility
setVisible(true);
pack();
}
//METHODS
/*
*buildBooksPanel() - Builds panel containing a JList/ScrollPane
*/
public void buildBooksPanel(){
booksList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//Visible book names
booksList.setVisibleRowCount(5);
//JLabel/Panel title
panelTitle = new JLabel("Available Books");
/*
* buildButtonsPanel - builds panel containing add/remove/checkout buttons
*/
public void buildButtonsPanel(){
//Create panel to hold buttons
buttonsPanel = new JPanel();
//Set Layout
buttonsPanel.setLayout(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
addSelected.addActionListener(new AddButtonListener());
removeSelected.addActionListener(new RemoveButtonListener());
checkOut.addActionListener(new CheckOutButtonListener());
scrollPane2.setPreferredSize(new Dimension(175,50));
//JLabel/Panel title
cartTitle = new JLabel("Shopping Cart ");
/*
* buildBannerPanel - builds panel containing banner for GUI
*/
public void buildBannerPanel(){
//Create panel
bannerPanel = new JPanel();
//create JLabel
JLabel banner = new JLabel(labelText);
banner.setFont(new Font("Serif",Font.BOLD,28));
/*
* buildSearchButtonsPanel - builds panel containing search and showall
buttons
*/
public void buildSearchButtonsPanel(){
//Create panel
searchButtonsPanel = new JPanel();
//Add listeners
searchButton.addActionListener(new SearchButtonListener());
showAllButton.addActionListener(new ShowAllButtonListener());
//ACTION LISTENERS
/*
* AddButtonListener - adds selected item to shopping cart upon selection
*/
public class AddButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
selectedIndex = booksList.getSelectedIndex();
selectedBookName = booksList.getSelectedValue();
books = booksList.getModel();
shoppingCart = selectedList.getModel();
shoppingCartDFM.addElement(shoppingCart.getElementAt(count));
}
if(element == -1)
bookPrice += bookPrices[selectedIndex];
else
bookPrice += bookPrices[element];
shoppingCartDFM.addElement(selectedBookName);
selectedList.setModel(shoppingCartDFM);
}
}
/*
* RemoveButtonListener - Removes selected item from shopping cart upon
selection
*/
public class RemoveButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
index = selectedList.getSelectedIndex();
((DefaultListModel)selectedList.getModel()).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) {
/*
* SearchButtonListener - searches for user desired item
*/
public class SearchButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
index = 0;
if(element == -1){
booksList.setModel(new DefaultListModel());
((DefaultListModel)booksList.getModel()).addElement(notFound);
}
else{
searchResults = bookNames[element];
booksList.setModel(new DefaultListModel());
((DefaultListModel)booksList.getModel()).addElement(searchResults);
}
}
}
/*
* ShowsAllButtonListener - shows all available books
*/
public class ShowAllButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
booksList.setModel(new DefaultListModel());
((DefaultListModel)booksList.getModel()).addElement(bookNames[i]);
}
}
}
new BookStoreGUI();
}
}