📄 coffee.java
字号:
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 张维
* @version 1.0.0
* @see Product
*/
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() {
/* Creates a object of class {@link @JPanel} */
JPanel jpanel = new JPanel();
jpanel.setLayout(new BorderLayout());
/* Create the components */
JTextField jtextfield = new JTextField(getCode(), 17);
JTextField jtextfield1 = new JTextField(getDescription(), 17);
JTextField jtextfield2 = new JTextField(Double.toString(getPrice()), 17);
JTextField jtextfield3 = new JTextField(getOrigin(), 17);
JTextField jtextfield4 = new JTextField(getRoast(), 17);
JTextField jtextfield5 = new JTextField(getFlavor(), 17);
JTextField jtextfield6 = new JTextField(getAroma(), 17);
JTextField jtextfield7 = new JTextField(getAcidity(), 17);
JTextField jtextfield8 = new JTextField(getBody(), 17);
/* Make the text area un-editable */
jtextfield.setEditable(false);
jtextfield1.setEditable(false);
jtextfield2.setEditable(false);
jtextfield3.setEditable(false);
jtextfield4.setEditable(false);
jtextfield5.setEditable(false);
jtextfield6.setEditable(false);
jtextfield7.setEditable(false);
jtextfield8.setEditable(false);
JPanel jpanel1 = new JPanel(new GridLayout(9, 1));
jpanel1.add(new JLabel("Code:"));
jpanel1.add(new JLabel("Description: "));
jpanel1.add(new JLabel("Price:"));
jpanel1.add(new JLabel("Origin:"));
jpanel1.add(new JLabel("Roast:"));
jpanel1.add(new JLabel("Flavor:"));
jpanel1.add(new JLabel("Aroma:"));
jpanel1.add(new JLabel("Acidity:"));
jpanel1.add(new JLabel("Body:"));
JPanel jpanel2 = new JPanel(new GridLayout(9, 1));
jpanel2.add(jtextfield);
jpanel2.add(jtextfield1);
jpanel2.add(jtextfield2);
jpanel2.add(jtextfield3);
jpanel2.add(jtextfield4);
jpanel2.add(jtextfield5);
jpanel2.add(jtextfield6);
jpanel2.add(jtextfield7);
jpanel2.add(jtextfield8);
/* Add components to the container */
jpanel.add(jpanel1, BorderLayout.CENTER);
jpanel.add(jpanel2, BorderLayout.EAST);
return jpanel;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -