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

📄 coffeebrewer.java

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

import  javax.swing.*;

/**
 * This class models a coffee brewer. It extends {@link Product} and adds the
 * following information:
 * <ol>
 * <li>the model of the coffee brewer, a <code>String</code></li>
 * <li>the water supply ("Pour-over" or "Automatic")</li>
 * <li>the capacity expressed in number of cups, a <code>int</code></li>
 * </ol>

 * @author ICarnegie, LT
 * @version 
 * @see Product
 * Created on 2006-12-14
 */
public class CoffeeBrewer extends Product  {

	/* Model of the coffee brewer. */
	private String  model;

	/* The water supply of the coffee brewer ("Pour-over" or "Automatic"). */
	private String  waterSupply;

	/* The capacity expressed in number of cups. */
	private int  numberOfCups;

	/**
	 * Constructs a <code>CoffeeBrewer</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 initialModel  the model of the coffee brewer.
	 * @param initialWaterSupply  the water supply is ("Pour-over" or
	 *                        "Automatic").
	 * @param initialNumberOfCups  the capacity expressed in number of cups.
	 */
	public CoffeeBrewer(String initialCode, String initialDescription,
			double  initialPrice, String initialModel,
			String initialWaterSupply, int initialNumberOfCups)  {

		super(initialCode, initialDescription, initialPrice);

		model = initialModel;
		waterSupply = initialWaterSupply;
		numberOfCups = initialNumberOfCups;
	}

	/**
	 * Returns the model of this coffee brewer.
	 *
	 * @return  the model of this coffee brewer.
	 */
	public String  getModel()  {

		return  model;
	}

	/**
	 * Returns the type of water supply.
	 *
	 * @return  returns "Pour-over" or "Automatic".
	 */
	public String  getWaterSupply()  {

		return  waterSupply;
	}

	/**
	 * Returns the capacity of this coffee brewer expressed in number of cups.
	 *
	 * @return  the capacity of this coffee brewer expressed in number of cups.
	 */
	public int  getNumberOfCups()  {

		return  numberOfCups;
	}

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

		return super.toString() + "_" + getModel() + "_" + getWaterSupply()
		       + "_" + getNumberOfCups();
	}

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

		/* Creat panel to hold all labels and textFields */
		JPanel coffeeBrewerPanel = 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(6, 1));
		
		/* Creat panel to hold a textField to display the value of the attributes */
		JPanel tempPanelT = new JPanel(new GridLayout(6, 1));
		
		/* The label with the names of the attributes in this class */
		JLabel coffeeBrewerLabel;
		
		/* The textField with the value of the attributes */
		JTextField coffeeBrewerTextField;
		
		/* The names of attributes of this class in the label */
		String string = "Code:_Description:_Price:_Model:_WaterSupply:_NumberOfCups:";
	   
		/** 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 */
	    	coffeeBrewerLabel = new JLabel( tokenizerL.nextToken() );
			tempPanelL.add(coffeeBrewerLabel);
			
			/* Creat the textField and add it to the panel holding textFields*/
			coffeeBrewerTextField = new JTextField( tokenizerT.nextToken(), 15 );
			coffeeBrewerTextField.setEditable(false);
			tempPanelT.add(coffeeBrewerTextField);
			
	    }

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

⌨️ 快捷键说明

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