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

📄 product.java

📁 CMU_ ssd3_第三单元答案 绝对正确
💻 JAVA
字号:
import  java.util.*;
import  java.awt.*;

import  javax.swing.*;

/**
 * This class models a product sold by the Gourmet Coffee store.
 * It contains the following information:
 * <ol>
 * <li>the code of the product, a <code>String</code></li>
 * <li>a short description of the product, a <code>String</code></li>
 * <li>the price of the product, a <code>double</code></li>
 * </ol>
 *
 * @author ICarnegie, LT
 * @version  
 * Created on 2006-12-14
 */
public class Product {

	/* Code of the product. */
	private String  code;

	/* Description of the product. */
	private String  description;

	/* Price of the product. */
	private double  price;

	/**
	 * Constructs a <code>Product</code> object.
	 *
	 * @param initialCode  the code of the product.
	 * @param initialDescription  the description of the product.
	 * @param initialPrice  the price of the product.
	 */
	public Product(String initialCode, String initialDescription,
	               double initialPrice)  {

		code = initialCode;
		description = initialDescription;
		price = initialPrice;
	}

	/**
	 * Returns the code of this product.
	 *
	 * @return  the code of this product.
	 */
	public String  getCode()  {

		return  code;
	}

	/**
	 * Returns the description of this product.
	 *
	 * @return  the descripton of this product.
	 */
	public String  getDescription()  {

		return  description;
	}

	/**
	 * Returns the price of this product.
	 *
	 * @return  price of this product.
	 */
	public double  getPrice()  {

		return  price;
	}

	/**
	 * Returns <code>true</code> if code of this product is
	 * equal to code of the argument.
	 * </p>
	 *
	 * @param object  object with which this product is compared.
	 * @return  <code>true</code> if code of this product is
	 *          equal to code of the argument; <code>false</code> otherwise.
	 */
	public boolean  equals(Object  object)  {

		return object instanceof Product
		       && getCode().equals(((Product) object).getCode());
	}

	/**
	 * Returns the string representation of this product.
	 *
	 * @return  the string representation of this product.
	 */
	public String  toString()  {

		return  getCode() + "_" + getDescription() + "_" + getPrice();
	}

	/**
	 * Obtains a {@link JPanel} object with the information of this product.
	 *
	 * @return a <code>JPanel</code> with the information of this product.
	 */
	public JPanel getPanel() {

		/* Creat panel to hold all labels and textFields */
		JPanel productPanel = new JPanel(new BorderLayout());
		
		/* Creat panel to hold a label with the names of the attributes in this class */
		JPanel tempPanelL = new JPanel(new GridLayout(3, 1));
		
		/* Creat panel to hold a textField to display the value of the attributes */
		JPanel tempPanelT = new JPanel(new GridLayout(3, 1));
		
		/* The label with the names of the attributes*/
		JLabel productLabel;
		
		/* The textField with the value of the attributes in this class */
		JTextField productTextField;
		
		/* The names of attributes of this class in the label*/
		String string = "Code:_Description:_Price:";
	   
		/** The first tokenizer is for the label and 
		  * the second one is for the textField.
		  */
		StringTokenizer  tokenizerL =
	            new  StringTokenizer( string, "_" );
	    StringTokenizer  tokenizerT =
            new  StringTokenizer( toString(), "_" );

	    /* Using a for loop to traverse all attributes in this class */
	    for(;tokenizerL.hasMoreTokens() && tokenizerT.hasMoreTokens();) {
	    	
	    	/* Creat the label and add it to the panel holding labels */
			productLabel = new JLabel( tokenizerL.nextToken() );
			tempPanelL.add(productLabel);
			
			/* Creat the textField and add it to the panel holding textFields*/
			productTextField = new JTextField( tokenizerT.nextToken(), 16 );
			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
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -