-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNutritionFacts.java
More file actions
67 lines (57 loc) · 1.83 KB
/
NutritionFacts.java
File metadata and controls
67 lines (57 loc) · 1.83 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
package chapter2.create.builder;
public class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
@Override
public String toString() {
return "servingSize:" + this.servingSize + "\n" +
"servings:" + this.servings + "\n" +
"calories:" + this.calories + "\n" +
"fat:" + this.fat + "\n" +
"sodium:" + this.sodium + "\n" +
"carbohydrate:" + this.carbohydrate ;
}
private NutritionFacts(Builder builder) {
this.servingSize = builder.servingSize;
this.servings = builder.servings;
this.calories = builder.calories;
this.fat = builder.fat;
this.sodium = builder.calories;
this.carbohydrate = builder.calories;
}
public static class Builder{
private final int servingSize;
private final int servings;
private int calories = 0;
private int fat = 0;
private int sodium = 0;
private int carbohydrate = 0;
public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int value) {
this.calories = value;
return this;
}
public Builder fat(int value) {
this.fat = value;
return this;
}
public Builder sodium(int value) {
this.sodium = value;
return this;
}
public Builder carbohydrate(int value) {
this.carbohydrate = value;
return this;
}
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
}