-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCake.java
More file actions
122 lines (107 loc) · 3.84 KB
/
Copy pathCake.java
File metadata and controls
122 lines (107 loc) · 3.84 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Represents a cake with different properties such as flavor, icing, and diameter.
*/
public class Cake {
// Define internal enums for flavor, icing, and diameter
public enum Flavor {
LEMON, CHOCOLATE, PINEAPPLE_UPSIDE_DOWN, CUSTOM;
/**
* Returns a user-friendly description of the flavor.
* @return the formatted string of the flavor name with each first letter capitalized and underscores removed.
*/
public String getDescription() {
return Cake.getDescription(name().toLowerCase().replace('_', ' '));
}
}
public enum Icing {
LEMON_GLAZE, CHOCOLATE, WHIPPED_CREAM_FROSTING, CUSTOM;
/**
* Returns a user-friendly description of the icing.
* @return the formatted string of the icing name with each first letter capitalized and underscores removed.
*/
public String getDescription() {
return Cake.getDescription(name().toLowerCase().replace('_', ' '));
}
}
public enum Diameter {
SIX(6),
EIGHT(8),
TEN(10),
TWELVE(12);
private final int value;
Diameter(int value) {
this.value = value;
}
/**
* Returns the value of the diameter.
* @return the integer value of the diameter.
*/
public int getValue() {
return value;
}
}
// Instance variables for flavor, icing, and diameter
private Flavor flavor;
private Icing icing;
private Diameter diameter;
/**
* Constructs a new Cake object with the given flavor, icing, and diameter.
* @param flavor the flavor of the cake.
* @param icing the icing of the cake.
* @param diameter the diameter of the cake.
*/
public Cake(Flavor flavor, Icing icing, Diameter diameter) {
this.flavor = flavor;
this.icing = icing;
this.diameter = diameter;
}
/**
* Capitalizes the first letter of each word in the given enum name
* and removes underscores to be more user-friendly.
* @param name the enum name to be capitalized and formatted
* @return the formatted string of the enum name with each first letter capitalized and underscores removed.
*/
private static String getDescription(String name) {
char[] chars = name.toCharArray();
boolean capitalize = false;
for (int i = 0; i < chars.length; i++) {
if (!capitalize && Character.isLetter(chars[i])) {
chars[i] = Character.toUpperCase(chars[i]);
capitalize = true;
} else if (Character.isWhitespace(chars[i])) {
capitalize = false;
}
}
return String.valueOf(chars);
}
// Gets the flavor of the cake.
public Flavor getFlavor() {
return flavor;
}
// Gets the icing of the cake.
public Icing getIcing() {
return icing;
}
// Gets the diameter of the cake.
public Diameter getDiameter() {
return diameter;
}
/**
* Returns a string representation of the Cake object in a user-friendly format.
* Overrides the Java Object.toString() method, which would return an unreadable reference to the object.
* @return the string representation of the Cake object.
*/
@Override
public String toString() {
return String.format("Cake: %nFlavor: %s%nFrosting: %s%nDiameter: %d%n",
flavor.getDescription(), icing.getDescription(), diameter.getValue(), "\"");
}
/**
* Main method to demonstrate the usage of the Cake class.
* @param args the command line arguments (not used).
*/
public static void main(String[] args) {
Cake cake = new Cake(Flavor.PINEAPPLE_UPSIDE_DOWN, Icing.WHIPPED_CREAM_FROSTING, Cake.Diameter.TEN);
System.out.println(cake);
}
}