📄 gourmetcoffeegui.java
字号:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.text.*;
/**
* Gourmet Coffee System.
*
* @author Wang Yunsheng
* @version 1.1.0
* @see Product
* @see Coffee
* @see CoffeeBrewer
* @see Catalog
* @see OrderItem
* @see Order
* @see Sales
* @see CatalogLoader
* @see FileCatalogLoader
* @see DataFormatException
* @see SalesFormatter
* @see PlainTextSalesFormatter
* @see HTMLSalesFormatter
* @see XMLSalesFormatter
* @see DataField
*/
public class GourmetCoffeeGUI extends JPanel {
/* Standar error stream */
static private PrintWriter stdErr = new PrintWriter(System.err, true);
/* Window width in pixels */
static private int WIDTH = 620;
/* Window height in pixels */
static private int HEIGHT = 530;
/* Size of the catalog list cell in pixels */
static private int CATALOG_CELL_SIZE = 50;
/* Visible rows in catalog list */
static private int CATALOG_LIST_ROWS = 14;
/* Size of the order list cell in pixels */
static private int ORDER_CELL_SIZE = 100;
/* Visible rows in order list */
static private int ORDER_LIST_ROWS = 6;
/* Size quantity text field */
static private int QUANTITY__TEXTFIELD_SIZE = 5;
/* Size total text field */
static private int TOTAL__TEXTFIELD_SIZE = 8;
/* Rows in status text area rows */
static private int STATUS_ROWS = 10;
/* Rows in status text area cols */
static private int STATUS_COLS = 40;
private JList catalogList;
private JList orderList;
private JButton addModifyButton;
private JButton removeButton;
private JButton registerSaleButton;
private JButton displaySalesButton;
private JButton saveSalesButton;
private JPanel productPanel;
private JLabel quantityLabel;
private JLabel totalLabel;
private JTextField quantityTextField;
private JTextField totalTextField;
private JTextArea statusTextArea;
private JRadioButton plainRadioButton;
private JRadioButton HTMLRadioButton;
private JRadioButton XMLRadioButton;
private JFileChooser fileChooser;
private Catalog catalog;
private Order currentOrder;
private Sales sales;
private SalesFormatter salesFormatter;
private NumberFormat dollarFormatter;
/**
* Loads a product catalog and starts the application.
*
* @param args
* String arguments. Not used.
* @throws IOException
* if there are errors in the loading the catalog.
*/
public static void main(String[] args) throws IOException {
String filename = "";
if (args.length != 1) {
filename = "catalog.dat";
} else {
filename = args[0];
}
try {
Catalog catalog = (new FileCatalogLoader()).loadCatalog(filename);
JFrame frame = new JFrame("Gourmet Coffee");
frame.setContentPane(new GourmetCoffeeGUI(catalog));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setVisible(true);
} catch (FileNotFoundException fnfe) {
stdErr.println("The file does not exist");
System.exit(1);
} catch (DataFormatException dfe) {
stdErr.println("The file contains malformed data: "
+ dfe.getMessage());
System.exit(1);
}
}
/**
* Instantiates the components and arranges them in a window.
*
* @param initialCatalog
* a product catalog.
*/
public GourmetCoffeeGUI(Catalog initialCatalog) {
// create the components
catalogList = new JList();
orderList = new JList();
catalogList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
catalogList.setVisibleRowCount(CATALOG_LIST_ROWS);
catalogList.setFixedCellWidth(CATALOG_CELL_SIZE);
orderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
orderList.setVisibleRowCount(ORDER_LIST_ROWS);
orderList.setFixedCellWidth(ORDER_CELL_SIZE);
addModifyButton = new JButton("Add|Modify Order Item");
removeButton = new JButton("Remove Order Item");
registerSaleButton = new JButton("Register Sale of Current Order");
displaySalesButton = new JButton("Display Sales");
saveSalesButton = new JButton("Save Sales");
quantityLabel = new JLabel("Quantity:");
totalLabel = new JLabel("Total:");
quantityTextField = new JTextField("", QUANTITY__TEXTFIELD_SIZE);
totalTextField = new JTextField("$0.00", TOTAL__TEXTFIELD_SIZE);
totalTextField.setEditable(false);
statusTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS);
statusTextArea.setEditable(false);
plainRadioButton = new JRadioButton("Plain", true);
HTMLRadioButton = new JRadioButton("HTML");
XMLRadioButton = new JRadioButton("XML");
ButtonGroup group = new ButtonGroup();
group.add(plainRadioButton);
group.add(HTMLRadioButton);
group.add(XMLRadioButton);
// Product Information panel
productPanel = new JPanel();
productPanel.setBorder(BorderFactory
.createTitledBorder("Product Information"));
// 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|Modify Product" panel
JPanel centralPanel = new JPanel(new BorderLayout());
JPanel addModifyPanel = new JPanel(new GridLayout(2, 1));
JPanel quantityPanel = new JPanel();
quantityPanel.add(quantityLabel);
quantityPanel.add(quantityTextField);
addModifyPanel.add(quantityPanel);
addModifyPanel.add(addModifyButton);
centralPanel.add(productPanel, BorderLayout.CENTER);
centralPanel.add(addModifyPanel, BorderLayout.SOUTH);
// Order panel
JPanel orderPanel = new JPanel(new BorderLayout());
orderPanel.setBorder(BorderFactory.createTitledBorder("Order"));
JPanel totalPanel = new JPanel();
totalPanel.add(totalLabel);
totalPanel.add(totalTextField);
JPanel buttonsPanel = new JPanel(new GridLayout(2, 1));
buttonsPanel.add(removeButton);
buttonsPanel.add(registerSaleButton);
orderPanel.add(totalPanel, BorderLayout.NORTH);
orderPanel.add(new JScrollPane(orderList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
orderPanel.add(buttonsPanel, BorderLayout.SOUTH);
// Status panel
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setBorder(BorderFactory.createTitledBorder("Status"));
JPanel salesButtonsPanel = new JPanel(new GridLayout(1, 5));
salesButtonsPanel.add(plainRadioButton);
salesButtonsPanel.add(HTMLRadioButton);
salesButtonsPanel.add(XMLRadioButton);
salesButtonsPanel.add(displaySalesButton);
salesButtonsPanel.add(saveSalesButton);
bottomPanel.add(new JScrollPane(statusTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
BorderLayout.CENTER);
bottomPanel.add(salesButtonsPanel, BorderLayout.SOUTH);
// arrange panels in window
setLayout(new BorderLayout());
add(catalogPanel, BorderLayout.WEST);
add(centralPanel, BorderLayout.CENTER);
add(orderPanel, BorderLayout.EAST);
add(bottomPanel, BorderLayout.SOUTH);
// start listening for list and buttons events
catalogList.addListSelectionListener(new DisplayProductListener());
addModifyButton.addActionListener(new AddModifyListener());
removeButton.addActionListener(new RemoveListener());
registerSaleButton.addActionListener(new RegisterSaleListener());
displaySalesButton.addActionListener(new DisplaySalesListener());
saveSalesButton.addActionListener(new SaveSalesListener());
plainRadioButton.addActionListener(new PlainListener());
HTMLRadioButton.addActionListener(new HTMLListener());
XMLRadioButton.addActionListener(new XMLListener());
// populate the product catalog
catalog = initialCatalog;
catalogList.setListData(catalog.getCodes());
currentOrder = new Order();
sales = new Sales();
salesFormatter = PlainTextSalesFormatter.getSingletonInstance();
fileChooser = new JFileChooser();
dollarFormatter = NumberFormat.getCurrencyInstance();
}
/*
* Obtains a {@link JPanel} object with the information of a product.
*
* @param dataFields an {@link ArrayList} of {@link DataField} with the
* product information. @return a {@link JPanel} with the product
* information.
*/
private JPanel getDataFieldsPanel(ArrayList<DataField> dataFields) {
/* COPY YOUR CODE FROM PREVIOUS EXERCISE */
JPanel panel = new JPanel();
for (DataField dataField : dataFields) {
JLabel label = new JLabel(dataField.getName());
JTextField textField = new JTextField(dataField.getValue());
textField.setEditable(false);
panel.add(label);
panel.add(textField);
panel.setLayout(new GridLayout(0, 2));
}
return panel; // REMOVE; USED SO THIS FILE COMPILES
}
/**
* This inner class handles list-selection events.
*/
class DisplayProductListener implements ListSelectionListener {
/**
* Displays the information of the selected product.
*
* @param event
* the event object.
*/
public void valueChanged(ListSelectionEvent event) {
if (!catalogList.getValueIsAdjusting()) {
String code = (String) catalogList.getSelectedValue();
Product product = catalog.getProduct(code);
productPanel.removeAll();
productPanel.setVisible(false); // Use this
productPanel.add( // to update
getDataFieldsPanel(product.getDataFields())); // the
// panel
productPanel.setVisible(true); // correctly
statusTextArea.setText("Product " + code
+ " has been displayed");
}
}
}
/**
* This inner class processes <code>addModifyButton</code> events.
*/
class AddModifyListener implements ActionListener {
/**
* Adds an order item to the current order.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
/* PLACE YOUR CODE HERE */
try {
if (!catalogList.getValueIsAdjusting()) {
String code = (String) catalogList.getSelectedValue();
Product product = catalog.getProduct(code);
int quantity = Integer
.parseInt(quantityTextField.getText());
if (quantity > 0) {
OrderItem orderItem = new OrderItem(product, quantity);
if (currentOrder.getItem(product) != null) {
currentOrder.getItem(product).setQuantity(quantity);
} else {
currentOrder.addItem(orderItem);
}
orderList.setListData(currentOrder.getItems());
totalTextField.setText(dollarFormatter
.format(currentOrder.getTotalCost()));
statusTextArea.setText("The sale has been added.");
} else {
statusTextArea.setText("Invalid Input.");
}
} else {
statusTextArea.setText("You have not selected a product.");
}
} catch (NumberFormatException nfe) {
statusTextArea.setText("Invalid Input.");
}
}
}
/**
* This inner class processes <code>removeButton</code> events.
*/
class RemoveListener implements ActionListener {
/**
* Removes an order item from the current order.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
/* PLACE YOUR CODE HERE */
if (!orderList.getValueIsAdjusting()) {
OrderItem orderItem = (OrderItem) orderList.getSelectedValue();
if (orderItem != null) {
currentOrder.removeItem(orderItem);
statusTextArea.setText("The sale has been removed.");
} else {
statusTextArea.setText("Empoty order.");
}
orderList.setListData(currentOrder.getItems());
totalTextField.setText(dollarFormatter.format(currentOrder
.getTotalCost()));
} else {
statusTextArea.setText("You have not selected a product.");
}
}
}
/**
* This inner class processes <code>registerSaleButton</code> button
* events.
*/
class RegisterSaleListener implements ActionListener {
/**
* Registers the sale of the current order.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
if (currentOrder.getNumberOfItems() == 0) {
statusTextArea.setText("The order is empty.");
} else {
sales.addOrder(currentOrder);
currentOrder = new Order();
orderList.setListData(currentOrder.getItems());
totalTextField.setText(dollarFormatter.format(0));
statusTextArea.setText("The sale has been registered.");
}
}
}
/**
* This inner class processes <code>displaySalesButton</code>events.
*/
class DisplaySalesListener implements ActionListener {
/**
* Displays the sales information.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
if (sales.getNumberOfOrders() == 0) {
statusTextArea.setText("No orders have been sold.");
} else {
statusTextArea.setText(salesFormatter.formatSales(sales));
}
}
}
/*
* This inner class processes <code>saveSalesButton</code> events.
*/
class SaveSalesListener implements ActionListener {
/**
* Saves the sales informations in a file.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
/* PLACE YOUR CODE HERE */
int a = fileChooser.showSaveDialog(null);
if (a == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChooser.getSelectedFile();
PrintWriter output = new PrintWriter(new FileWriter(file));
output.print(statusTextArea.getText());
output.close();
} catch (IOException ioe) {
stdErr.println(ioe.toString());
}
}
}
}
/*
* This inner class processes <code>plainRadioButton</code> events.
*/
class PlainListener implements ActionListener {
/**
* Sets the sales formatter to plain text.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
salesFormatter = PlainTextSalesFormatter.getSingletonInstance();
}
}
/*
* This inner class processes <code>HTMLRadioButton</code> events.
*/
class HTMLListener implements ActionListener {
/**
* Sets the sales formatter to HTML.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
salesFormatter = HTMLSalesFormatter.getSingletonInstance();
}
}
/*
* This inner class processes <code>XMLRadioButton</code> events.
*/
class XMLListener implements ActionListener {
/**
* Sets the sales formatter to XML.
*
* @param event
* the event object.
*/
public void actionPerformed(ActionEvent event) {
salesFormatter = XMLSalesFormatter.getSingletonInstance();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -