-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFruitExample.java
More file actions
54 lines (46 loc) · 1.51 KB
/
FruitExample.java
File metadata and controls
54 lines (46 loc) · 1.51 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
enum Fruit {
APPLE("Red", 1000),
BANANA("Yellow", 700),
GRAPE("Purple", 2000),
BLUEBERRY("Blue", 2500);
private final String color;
private final int price;
Fruit(String color, int price) {
this.color = color;
this.price = price;
}
public String getColor() {
return color;
}
public int getPrice() {
return price;
}
}
public class FruitExample {
public static void main(String[] args) {
Fruit myFruit = Fruit.BANANA;
System.out.println("Selected fruit: " + myFruit);
System.out.println("Color: " + myFruit.getColor());
System.out.println("Price: " + myFruit.getPrice() + " won");
System.out.println("\nAll fruits:");
for (Fruit fruit : Fruit.values()) {
System.out.println(fruit + " - Color: " + fruit.getColor() + ", Price: " + fruit.getPrice() + " won");
}
// switch 문 사용 예제
System.out.println("\nFruit recommendation:");
switch (myFruit) {
case APPLE:
System.out.println("An apple a day keeps the doctor away!");
break;
case BANANA:
System.out.println("Bananas are great for potassium!");
break;
case GRAPE:
System.out.println("Grapes are perfect for snacking!");
break;
case BLUEBERRY:
System.out.println("Blueberries are full of antioxidants!");
break;
}
}
}