-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductButton.java
More file actions
59 lines (53 loc) · 1.59 KB
/
ProductButton.java
File metadata and controls
59 lines (53 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.awt.*;
import javax.swing.*;
import java.text.NumberFormat;
/**
*A ProductButton is a special kind of JButton,
*used to display products for sale.
*@author Natalie Webber
*@author Michael Roscoe
*/
public class ProductButton extends JButton {
/**
*The name of the product.
*/
private String productName;
/**
*The price for which we will sell the product
*/
private double productPrice;
/**
*Creates a button that will display an image of the product
*(assumed to be stored in a file starting with the specified
*name and ending with ".jpg"), the specified product name,
*and the specified price (formatted properly); the text is
*displayed below the image and is centered.
*@param name The product name.
*@param price The selling price for this product.
*/
public ProductButton (String name, double price) {
super(name);
ImageIcon buttonIcon = new ImageIcon(name + ".jpg");
NumberFormat formatter = NumberFormat.getCurrencyInstance();
super.setText(name + ": " + formatter.format(price));
super.setIcon(buttonIcon);
this.setVerticalTextPosition(AbstractButton.BOTTOM);
this.setHorizontalTextPosition(AbstractButton.CENTER);
productName = name;
productPrice = price;
}
/**
*Retrieves the product name.
*@return The product name.
*/
public String getName() {
return productName;
}
/**
*Retrieves the selling price.
*@return The price for which we will sell the product.
*/
public double getPrice() {
return productPrice;
}
}