-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
93 lines (77 loc) · 2.04 KB
/
Main.java
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
package com.zenmo.parameterized;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
var simulation = new Simulation(12);
simulation.run();
}
}
class HessenpoortSImulation extends Simulation {
public HessenpoortSImulation() {
super(24);
}
@Override
Iterable<Agent> createAgents() {
return Arrays.asList(
new Agent("Alice", 1),
new Agent("Bob", 0.5f),
new Agent("Charlie", 2),
new Agent("Dennis", 1.5f)
);
}
}
/**
* Engine
*/
class Simulation {
protected final int durationHours;
public Simulation(int durationHours) {
this.durationHours = durationHours;
}
public void run() {
var agents = createAgents();
run(agents);
var totalConsumedKwh = accumulateConsumpedKwh(agents);
System.out.println("Total consumed kwh: " + totalConsumedKwh);
}
Iterable<Agent> createAgents() {
return Arrays.asList(
new Agent("Alice", 1),
new Agent("Bob", 0.5f),
new Agent("Charlie", 2)
);
}
void run(Iterable<Agent> agents) {
for (var hour = 0; hour < this.durationHours; hour++) {
for (var agent : agents) {
agent.runOneHour();
}
}
}
double accumulateConsumpedKwh(Iterable<Agent> agents) {
var totalKwh = 0.0;
for (var agent : agents) {
totalKwh += agent.getConsumedKwh();
}
return totalKwh;
}
}
/**
* Agent
*/
class Agent {
protected final String name;
protected final float consumptionKw;
protected double consumedKwh = 0.0;
public Agent(String name, float consumptionKw) {
this.name = name;
this.consumptionKw = consumptionKw;
}
public void runOneHour() {
System.out.println("Running agent " + name);
this.consumedKwh += consumptionKw;
}
public double getConsumedKwh() {
return consumedKwh;
}
}