-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
103 lines (92 loc) · 3.03 KB
/
Copy pathMain.java
File metadata and controls
103 lines (92 loc) · 3.03 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
import java.util.Scanner;
import java.util.stream.Collectors;
import kitchen.Salad;
import kitchen.vegetable.*;
class Main {
public static void main(String[] args) {
System.out.println("hello world");
Scanner scanner = new Scanner(System.in);
Salad salad = new Salad();
while (true) {
System.out.println("\noptions:");
System.out.println(" 1) create new salad");
System.out.println(" 2) add a vegetable");
System.out.println(" 3) get total calories");
System.out.println(" 4) sort vegetables by weight");
System.out.println(" 5) find vegetables by calories");
System.out.println(" 6) exit");
System.out.println();
System.out.print("i want to: ");
int opt = Integer.parseInt(scanner.nextLine());
if (opt == 6) {
System.out.println("bye");
break;
}
switch (opt) {
case 1:
{
salad = new Salad();
System.out.println("done");
}
break;
case 2:
{
System.out.println("available vegetables: cucumber, onion, potato, tomato");
System.out.print("enter a vegetable: ");
String vegetableName = scanner.nextLine().toLowerCase();
System.out.print("enter a weight (in grams): ");
double weight = Double.parseDouble(scanner.nextLine());
AbstractVegetable vegetable = null;
switch (vegetableName) {
case "cucumber":
vegetable = new Cucumber(weight);
break;
case "onion":
vegetable = new Onion(weight);
break;
case "potato":
vegetable = new Potato(weight);
break;
case "tomato":
vegetable = new Tomato(weight);
break;
}
if (vegetable != null) {
salad.addVegetable(vegetable);
System.out.println("done");
} else {
System.out.println("invalid vegetable");
}
}
break;
case 3:
{
System.out.printf("total calories = %.2f%n", salad.calories());
}
break;
case 4:
{
System.out.println("by weight:");
System.out.println(salad.sortedByWeight()
.stream()
.map(Object::toString)
.collect(Collectors.joining("\n")));
}
break;
case 5:
{
System.out.print("enter min calories (per 100g): ");
double min = Double.parseDouble(scanner.nextLine());
System.out.print("enter max calories (per 100g): ");
double max = Double.parseDouble(scanner.nextLine());
System.out.println("\nresult:");
System.out.println(salad.withCaloriesPer100gBetween(min, max)
.stream()
.map(Object::toString)
.collect(Collectors.joining("\n")));
}
break;
}
}
}
}