⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gourmetcoffeegui.java

📁 CMU_ ssd3_第三单元答案 绝对正确
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			/* Creat the textField and add it to the panel holding textFields*/
			productTextField = new JTextField( dataFields.get(i).getValue(), 18 );
			productTextField.setEditable(false);
			tempPanelT.add(productTextField);
	    }

	    /* Add the panel holding labels and the other holding textFields to a panel*/
	    productPanel.add(tempPanelL, BorderLayout.WEST);
	    productPanel.add(tempPanelT, BorderLayout.EAST);
	
		return productPanel; // 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) {
			
			try {
				
				int newQuantity = Integer.parseInt( quantityTextField.getText() );
				
				if( newQuantity <= 0 ) {
					
					/* The quantity text field contains a negative integer or zero. */
					statusTextArea.setText("Please enter a positive integer.");
				
				} else if( catalogList.isSelectionEmpty() ) {
					
						/* The user has not selected a product. */
						statusTextArea.setText("Please select a product code from the catalog list.");
						
				} else if ( ! catalogList.getValueIsAdjusting() 
								&& catalogList.getSelectedIndex() != -1 ) {
						   
					    /* The code of the selected product in the catalog list. */
						String code = (String)catalogList.getSelectedValue();
								
						/* Judge if the product existed in current order. */
						boolean isExist = false;
						
						/* Using a for loop to locate the item in the order with the selected 
						 * product order and changes its quantity to the value specified by 
						 * the user if it's in the current.
						 */
						for(Iterator i = currentOrder.iterator(); i.hasNext();) {
									
							OrderItem orderItem = (OrderItem)i.next();
							Product product = orderItem.getProduct();
									
							if( code.equals(product.getCode()) ) {
								
								orderItem.setQuantity( newQuantity );
								isExist = true;
								break;
							} 
						}			
				
						/* Create a new order item and add it to the current order,
						 * because the current order does not contain an item with 
						 * the selected product.
						 */
						if( ! isExist ) {
							
							Product product = catalog.getProduct(code);
							currentOrder.addItem( new OrderItem(product, newQuantity) );
						}
						
						/* Update the display of the order list. */
						orderList.setListData( currentOrder.getItems() );
						
						/* Update the display of the order's total cost. */
						totalTextField.setText(dollarFormatter.format(currentOrder.getTotalCost()));
						
						/* Display a status message in the status area */
						statusTextArea.setText("The product has been modified.");
				}
			} catch(NumberFormatException nfe) {
				
				/* The quantity text field does not contain an integer. */
				statusTextArea.setText("Please enter an integer.");
			} 
		}
	}

	/**
	 * 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) {

			if (currentOrder.getNumberOfItems() == 0) {
				
				/* The current order is empty. */
				statusTextArea.setText("The order is empty.");
				
			} else if( orderList.isSelectionEmpty() ) {
				
				/* The user has not selected an item in the order. */
				statusTextArea.setText("Please select an item from the order list.");
				
			} else {
				
				/* Remove the selected item from the current order. */
				OrderItem orderItem = (OrderItem)orderList.getSelectedValue();
				currentOrder.removeItem( orderItem );
				orderList.setListData(currentOrder.getItems());
				
				/* Update the display of the order's total cost. */
				totalTextField.setText(dollarFormatter.format(currentOrder.getTotalCost()));
				
				/* Display a status message in the status area. */
				statusTextArea.setText("The product has been removed.");
			}
		}
	}

	/**
	 * 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) {

			 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			 
			if ( sales.getNumberOfOrders() == 0 ) {
				
				/* No orders have been sold. */
				statusTextArea.setText("No orders have been sold.");
				
			} else {
				
				int result = fileChooser.showSaveDialog(null);

				if ( result == JFileChooser.APPROVE_OPTION ) {

					try {

						File file = fileChooser.getSelectedFile();
						PrintWriter output =
								new PrintWriter(new FileWriter(file));

						output.println( salesFormatter.formatSales(sales) );
						output.close();
	                   
					} catch (IOException ioe) {
							stdErr.println(ioe.toString());
					}
				} else if( result == JFileChooser.CANCEL_OPTION ) {
	        	
					/* The user closes the file chooser without selecting a file. */
					statusTextArea.setText("The sales information has not been saved.");
				}
			}
		}
	}

	/*
	 * 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 + -