📄 discountsgui.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.*;
import java.util.*;
/**
* Sales System computing discounts.
*
* @author iCarnegie
* @version 1.1.0
* @see Product
* @see Coffee
* @see CoffeeBrewer
* @see Catalog
* @see Customer
* @see CustomerDatabase
* @see OrderItem
* @see Order
* @see Sales
* @see Store
* @see CatalogTestData
* @see CustomerDatabaseTestData
* @see DataFormatException
* @see DiscountOrder
*/
public class DiscountsGUI extends JPanel {
/* Temporary customer for current order */
private final static Customer CURRENT_ORDER_CUSTOMER =
new Customer (0, "Current Order Customer");
/* Temporary date for current order */
private final static Date CURRENT_ORDER_DATE =
new Date (2003, 1, 1);
/* Line separator*/
private final static String NEW_LINE = System.getProperty("line.separator");
/* Window width in pixels */
static private int WIDTH = 550;
/* Window height in pixels */
static private int HEIGHT = 550;
/* Size of the catalog list cell in pixels */
static private int CATALOG_CELL_SIZE = 60;
/* Visible rows in catalog list */
static private int CATALOG_LIST_ROWS = 13;
/* Size of the sales person database list cell in pixels */
static private int ORDER_ITEMS_CELL_SIZE = 110;
/* Visible rows in sales person database list list */
static private int ORDER_ITEMS_LIST_ROWS = 13;
/* Size of the customer database list cell in pixels */
static private int CUSTOMER_DATABASE_CELL_SIZE = 100;
/* Visible rows in customer database list list */
static private int CUSTOMER_DATABASE_LIST_ROWS = 4;
/* Size store name text field */
static private int STORE_NAME_TEXTFIELD_SIZE = 15;
/* Size store address text field */
static private int STORE_ADDRESS_TEXTFIELD_SIZE = 17;
/* Size quantity text field */
static private int QUANTITY_TEXTFIELD_SIZE = 5;
/* Size total text field */
static private int TOTAL_TEXTFIELD_SIZE = 8;
/* Size file name text field */
static private int FILE_NAME_TEXTFIELD_SIZE = 15;
/* Size year text field */
static private int YEAR_TEXTFIELD_SIZE = 5;
/* Size month text field */
static private int MONTH_TEXTFIELD_SIZE = 3;
/* Size day text field */
static private int DAY_TEXTFIELD_SIZE = 3;
/* Size number of orders text field */
static private int NUMBER_OF_ORDERS_TEXTFIELD_SIZE = 3;
/* Rows in status text area */
static private int STATUS_ROWS = 5;
/* Cols in status text area */
static private int STATUS_COLS = 40;
private JList catalogList;
private DefaultListModel catalogListModel;
private JList customerDatabaseList;
private DefaultListModel customerDatabaseListModel;
private JList orderItemsList;
private DefaultListModel orderItemsListModel;
private JButton addButton;
private JButton deleteAllItemsButton;
private JButton registerSaleButton;
private JButton displaySalesButton;
private JButton loadProductsWithDiscountButton;
private JRadioButton defaultDiscountButton;
private JRadioButton allProductsDiscountButton;
private JRadioButton selectedProductsDiscountButton;
private JPanel productPanel;
private JLabel storeNameLabel;
private JLabel storeAddressLabel;
private JLabel quantityLabel;
private JLabel totalLabel;
private JLabel fileNameLabel;
private JLabel yearLabel;
private JLabel monthLabel;
private JLabel dayLabel;
private JLabel numberOfOrdersLabel;
private JTextField storeNameTextField;
private JTextField storeAddressTextField;
private JTextField quantityTextField;
private JTextField totalTextField;
private JTextField fileNameTextField;
private JTextField yearTextField;
private JTextField monthTextField;
private JTextField dayTextField;
private JTextField numberOfOrdersTextField;
private JTextArea statusTextArea;
private Store store;
private DiscountOrder currentOrder;
private NumberFormat dollarFormatter = NumberFormat.getCurrencyInstance();
private SelectedProductsDiscountStrategy selectedProductsDiscountStrategy;
/**
* Starts the application.
*
* @param args String arguments. Not used.
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Sales System");
frame.setContentPane(new DiscountsGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
}
/**
* Instantiates the components and arranges them in a window.
*
* @param initialCatalog a product catalog.
*/
private DiscountsGUI() {
store = new Store(
"Pittsburgh Gourmet Coffee",
"4615 Forbes Avenue",
CatalogTestData.getCatalog(),
CustomerDatabaseTestData.getCustomerDatabase(),
new Sales());
// create the components
catalogListModel = new DefaultListModel();
catalogList = new JList(catalogListModel);
orderItemsListModel = new DefaultListModel();
orderItemsList = new JList(orderItemsListModel);
customerDatabaseListModel = new DefaultListModel();
customerDatabaseList = new JList(customerDatabaseListModel);
catalogList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
catalogList.setVisibleRowCount(CATALOG_LIST_ROWS);
catalogList.setFixedCellWidth(CATALOG_CELL_SIZE);
orderItemsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
orderItemsList.setVisibleRowCount(ORDER_ITEMS_LIST_ROWS);
orderItemsList.setFixedCellWidth(ORDER_ITEMS_CELL_SIZE);
customerDatabaseList.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
customerDatabaseList.setVisibleRowCount(CUSTOMER_DATABASE_LIST_ROWS);
customerDatabaseList.setFixedCellWidth(CUSTOMER_DATABASE_CELL_SIZE);
addButton = new JButton("Add Order Item");
deleteAllItemsButton = new JButton("Delete All Order Items");
loadProductsWithDiscountButton =
new JButton ("Load Products With Discount");
registerSaleButton = new JButton("Register Sale of Current Order");
displaySalesButton = new JButton("Display Sales");
defaultDiscountButton =
new JRadioButton("Apply Default Discount");
allProductsDiscountButton =
new JRadioButton("Apply 15% on All Products");
selectedProductsDiscountButton =
new JRadioButton("Apply 20% on Selected Products");
ButtonGroup group = new ButtonGroup();
group.add(defaultDiscountButton);
group.add(allProductsDiscountButton);
group.add(selectedProductsDiscountButton);
storeNameLabel = new JLabel("Store:");
storeAddressLabel = new JLabel("Address:");
quantityLabel = new JLabel("Quantity:");
totalLabel = new JLabel("Total:");
fileNameLabel = new JLabel("File Name:");
yearLabel = new JLabel("Year");
monthLabel = new JLabel("Month");
dayLabel = new JLabel ("Day");
numberOfOrdersLabel = new JLabel ("Number of Orders:");
storeNameTextField = new JTextField(store.getName(),
STORE_NAME_TEXTFIELD_SIZE);
storeNameTextField.setEditable(false);
storeAddressTextField = new JTextField(store.getAddress(),
STORE_ADDRESS_TEXTFIELD_SIZE);
storeAddressTextField.setEditable(false);
quantityTextField = new JTextField("", QUANTITY_TEXTFIELD_SIZE);
totalTextField = new JTextField("", TOTAL_TEXTFIELD_SIZE);
totalTextField.setEditable(false);
fileNameTextField = new JTextField("", FILE_NAME_TEXTFIELD_SIZE);
yearTextField = new JTextField("", YEAR_TEXTFIELD_SIZE);
monthTextField = new JTextField("", MONTH_TEXTFIELD_SIZE);
dayTextField = new JTextField("", DAY_TEXTFIELD_SIZE);
numberOfOrdersTextField =
new JTextField(Integer.toString(
store.getSales().getNumberOfOrders()),
NUMBER_OF_ORDERS_TEXTFIELD_SIZE);
numberOfOrdersTextField.setEditable(false);
statusTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS);
statusTextArea.setEditable(false);
// Store info panel
JPanel storeInfoPanel = new JPanel(new GridLayout(1, 2));
JPanel storeNamePanel = new JPanel();
JPanel storeAddressPanel = new JPanel();
storeNamePanel.add(storeNameLabel);
storeNamePanel.add(storeNameTextField);
storeAddressPanel.add(storeAddressLabel);
storeAddressPanel.add(storeAddressTextField);
storeInfoPanel.add(storeNamePanel);
storeInfoPanel.add(storeAddressPanel);
// Catalog panel
JPanel catalogPanel = new JPanel();
catalogPanel.setBorder(BorderFactory.createTitledBorder("Catalog"));
catalogPanel.add (new JScrollPane(catalogList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
// Add product panel
JPanel centralPanel = new JPanel(new BorderLayout());
JPanel addPanel = new JPanel(new GridLayout(2, 1));
JPanel quantityPanel = new JPanel();
quantityPanel.add(quantityLabel);
quantityPanel.add(quantityTextField);
addPanel.add(quantityPanel, BorderLayout.CENTER);
addPanel.add(addButton, BorderLayout.SOUTH);
centralPanel.add(catalogPanel, BorderLayout.CENTER);
centralPanel.add(addPanel, BorderLayout.SOUTH);
// Order items list panel
JPanel orderItemsListPanel = new JPanel();
orderItemsListPanel.add(new JScrollPane(orderItemsList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
// Order items panel
JPanel orderItemPanel = new JPanel(new BorderLayout());
orderItemPanel.setBorder(
BorderFactory.createTitledBorder("Order Items"));
JPanel totalPanel = new JPanel();
totalPanel.add(totalLabel);
totalPanel.add(totalTextField);
orderItemPanel.add(totalPanel, BorderLayout.NORTH);
orderItemPanel.add(orderItemsListPanel, BorderLayout.CENTER);
orderItemPanel.add(deleteAllItemsButton, BorderLayout.SOUTH);
// File name panel
JPanel fileNamePanel = new JPanel();
fileNamePanel.add(fileNameLabel);
fileNamePanel.add(fileNameTextField);
// Discounts buttons panel
JPanel discountButtonsPanel = new JPanel(new GridLayout(3, 1));
discountButtonsPanel.add(defaultDiscountButton);
discountButtonsPanel.add(allProductsDiscountButton);
discountButtonsPanel.add(selectedProductsDiscountButton);
// Discount panel
JPanel discountsPanel = new JPanel(new BorderLayout());
discountsPanel.setBorder(BorderFactory.createTitledBorder("Discounts"));
discountsPanel.add(discountButtonsPanel, BorderLayout.NORTH);
discountsPanel.add(fileNamePanel, BorderLayout.CENTER);
discountsPanel.add(loadProductsWithDiscountButton, BorderLayout.SOUTH);
// Customer database panel
JPanel customerDatabasePanel = new JPanel();
customerDatabasePanel.setBorder(
BorderFactory.createTitledBorder("Customer Database"));
customerDatabasePanel.add(new JScrollPane(customerDatabaseList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
// Date panel
JPanel datePanel = new JPanel(new GridLayout(2, 3));
datePanel.add(monthLabel);
datePanel.add(dayLabel);
datePanel.add(yearLabel);
datePanel.add(monthTextField);
datePanel.add(dayTextField);
datePanel.add(yearTextField);
// Order panel
JPanel orderPanel = new JPanel(new BorderLayout());
JPanel orderInfoPanel = new JPanel(new BorderLayout());
orderInfoPanel.add(discountsPanel, BorderLayout.NORTH);
orderInfoPanel.add(customerDatabasePanel, BorderLayout.CENTER);
orderInfoPanel.add(datePanel, BorderLayout.SOUTH);
orderPanel.add(orderInfoPanel, BorderLayout.CENTER);
orderPanel.add(registerSaleButton, BorderLayout.SOUTH);
// Number of Orders Panel
JPanel numberOfOrdersPanel = new JPanel();
numberOfOrdersPanel.add(numberOfOrdersLabel);
numberOfOrdersPanel.add(numberOfOrdersTextField);
// Status panel
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setBorder(BorderFactory.createTitledBorder("Status"));
bottomPanel.add (new JScrollPane(statusTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.NORTH);
bottomPanel.add(numberOfOrdersPanel, BorderLayout.CENTER);
bottomPanel.add(displaySalesButton, BorderLayout.SOUTH);
// Arrange panels in window
setLayout(new BorderLayout());
add(storeInfoPanel, BorderLayout.NORTH);
add(centralPanel, BorderLayout.WEST);
add(orderItemPanel, BorderLayout.CENTER);
add(orderPanel, BorderLayout.EAST);
add(bottomPanel, BorderLayout.SOUTH);
// Start listening for list and buttons events
addButton.addActionListener(new AddListener());
deleteAllItemsButton.addActionListener(
new DeleteAllItemsListener());
loadProductsWithDiscountButton.addActionListener(
new LoadProductsWithDiscountListener());
registerSaleButton.addActionListener(new RegisterSaleListener());
displaySalesButton.addActionListener(new DisplaySalesListener());
defaultDiscountButton.addActionListener(new DefaultDiscountListener());
allProductsDiscountButton.addActionListener(
new AllProductsDiscountListener());
selectedProductsDiscountButton.addActionListener(
new SelectedProductsDiscountListener());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -