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

📄 coffee.java

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

import  javax.swing.*;

/**
 * This class models a coffee product. It extends
 * {@link Product} and adds the following information:
 * <ol>
 * <li>the origin of the coffee, a <code>String</code></li>
 * <li>the roast of the coffee, a <code>String</li>
 * <li>the flavor of the coffee, a <code>String</li>
 * <li>the aroma of the coffee, a <code>String</li>
 * <li>the acidity of the coffee, a <code>String</li>
 * <li>the body of the product, a <code>double</li>
 * </ol>
 *
 * @author ICarnegie, LT
 * @version  
 * @see Product
 * Created on 2006-12-14
 */
public class Coffee extends Product  {

	/* Origin of the coffee. */
	private String  origin;

	/* Roast of the coffee. */
	private String  roast;

	/* Flavor of the coffee. */
	private String  flavor;

	/* Aroma of the coffee. */
	private String  aroma;

	/* Acidity of the coffee.*/
	private String  acidity;

	/* Body of the coffee */
	private String  body;

	/**
	 * Constructs a <code>Coffee</code> object.
	 *
	 * @param initialCode  the code of the product.
	 * @param initialDescription  a short description of the product.
	 * @param initialPrice  the price of the product.
	 * @param initialOrigin the origin of coffee.
	 * @param initialRoast the kind of roast of the coffee.
	 * @param initialFlavor  the flavor of the coffee.
	 * @param initialAroma  the aroma of the coffee.
	 * @param initialAcidity  the acidity of the coffee.
	 * @param initialBody  the body of the coffee.
	 */
	public Coffee(String initialCode, String initialDescription,
			double  initialPrice, String initialOrigin,
			String initialRoast, String initialFlavor,
			String initialAroma, String initialAcidity,
			String initialBody) {

		super(initialCode, initialDescription, initialPrice);

		origin = initialOrigin;
		roast = initialRoast;
		flavor = initialFlavor;
		aroma = initialAroma;
		acidity = initialAcidity;
		body = initialBody;
	}

	/**
	 * Returns the origin of this coffee.
	 *
	 * @return  the origin of this coffee.
	 */
	public String  getOrigin()  {

		return  origin;
	}

	/**
	 * Returns the roast of this coffee.
	 *
	 * @return  the roast of this coffee.
	 */
	public String  getRoast()  {

		return  roast;
	}

	/**
	 * Returns the flavor of this coffee.
	 *
	 * @return  the flavor of this coffee.
	 */
	public String  getFlavor()  {

		return  flavor;
	}

	/**
	 * Returns the aroma of this coffee.
	 *
	 * @return  the aroma of this coffee.
	 */
	public String  getAroma()  {

		return  aroma;
	}

	/**
	 * Returns  the acidity of this coffee.
	 *
	 * @return  the acidity of this coffee.
	 */
	public String  getAcidity()  {

		return  acidity;
	}

	/**
	 * Returns the body of this coffee.
	 *
	 * @return  the body of this coffee.
	 */
	public String  getBody()  {

		return  body;
	}

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

		return super.toString() + "_" + getOrigin() + "_" + getRoast() + "_"
		       + getFlavor() + "_" + getAroma()	+ getAcidity() + "_"
		       + getBody();
	}

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

		/* Creat panel to hold all labels and textFields */
		JPanel coffeePanel = 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(9, 1));
		
		/* Creat panel to hold a textField to display the value of the attributes */
		JPanel tempPanelT = new JPanel(new GridLayout(9, 1));
		
		/* The label with the names of the attributes in this class */
		JLabel coffeeLabel;
		
		/* The textField with the value of the attributes */
		JTextField coffeeTextField;
		
		/* The names of attributes of this class in the label */
		String string = "Code:_Description:_Price:_Origin:_Roast:_Flavor:_Aroma:_Acidity:_Body:";
	   
		/** 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 */
	    	coffeeLabel = new JLabel( tokenizerL.nextToken() );
			tempPanelL.add(coffeeLabel);
			
			/* Creat the textField and add it to the panel holding textFields*/
			coffeeTextField = new JTextField( tokenizerT.nextToken(), 15 );
			coffeeTextField.setEditable(false);
			tempPanelT.add(coffeeTextField);
	    }

	    /* Add the panel holding labels and the other holding textFields to a panel*/
		coffeePanel.add(tempPanelL, BorderLayout.WEST);
		coffeePanel.add(tempPanelT, BorderLayout.EAST);
		
		return coffeePanel; // REMOVE; USED SO THIS FILE COMPILES
	}
}

⌨️ 快捷键说明

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