@matfah/

Battery Charger FR 2009

Java

No description

fork
loading
Files
  • Main.java
  • BatteryCharger.java
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.*;
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    
    for(int i = 0; i < 20; i++)
      testGetChargingCost();
      
    for(int i = 0; i < 20; i++)
      testGetChargeStartTime();
      
    System.out.println("All tests passed!");
  }
  
  public static void testGetChargingCost() {
    
    Field rateTableField = null;
    try {
      rateTableField = BatteryCharger.class.getDeclaredField("rateTable");
    }  
    catch(Exception e) {
      System.out.println("Did you mess with the rateTable instance variable?!");
      System.exit(0);
    }
    rateTableField.setAccessible(true);
    
    BatteryCharger charger = new BatteryCharger();
    
    int start = (int)(Math.random()*24);
    int length = (int)(Math.random()*100) + 1;
    int sum = 0;
    
    int[] rates = new int[24];
    int[] copy = new int[24];
    for(int i = 0; i < rates.length; i++) {
      rates[i] = (int)(Math.random()*50)+10;
      copy[i] = rates[i];
      
      int j = i;
      while(j < start) {
        j += 24;
      }
      
      while(j < start + length) {
        sum += rates[i];
        j += 24;
      }
    }
    
    try {
      rateTableField.set(charger, rates);
    }
    catch(Exception e) {
      System.out.println("Error trying to set the rateTable instance variable...");
      System.exit(0);
    }
    
    int result = 0;
    
    try {
      result = charger.getChargingCost(start, length);
    }
    catch(Exception e) {
      System.out.println("Error calling getChargingCode()");
      System.exit(0);
    }
    
    if(result != sum) {
      System.out.println("Value returned by calling getChargingCode() is wrong");
      System.exit(0);
    }
    
    if(!Arrays.equals(copy, rates)) {
      System.out.println("getChargingCode() should NOT change the ratesTable variable!");
      System.exit(0);
    }
    
  }
  
  public static void testGetChargeStartTime() {
    Field rateTableField = null;
    try {
      rateTableField = BatteryCharger.class.getDeclaredField("rateTable");
    }  
    catch(Exception e) {
      System.out.println("Did you mess with the rateTable instance variable?!");
      System.exit(0);
    }
    rateTableField.setAccessible(true);
    
    CustomBatteryCharger charger = new CustomBatteryCharger();
    BatteryCharger other = new BatteryCharger();
    
    int length = (int)(Math.random()*100) + 1;
    int[] rates = new int[24];
    int[] copy = new int[24];
    for(int i = 0; i < rates.length; i++) {
      rates[i] = (int)(Math.random()*50)+10;
      copy[i] = rates[i];
    }
    
    try {
      rateTableField.set(charger, rates);
      rateTableField.set(other, rates);
    }
    catch(Exception e) {
      System.out.println("Error trying to set the rateTable instance variable...");
      System.exit(0);
    }
    
    int answerStart = -1;
    int answerValue = -1;
    for(int i = 0; i < 24; i++) {
      int cost = other.getChargingCost(i, length);
      if(i == 0 || cost < answerValue) {
        answerStart = i;
        answerValue = cost;
      }
    }
    
    int result = 0;
    try {
      result = charger.getChargeStartTime(length);
    }
    catch(Exception e) {
      System.out.println("Error calling getChargeStartTime()");
      System.exit(0);
    }
    
    if(result != answerStart) {
      System.out.println("Value returned by calling getChargeStartTime() is wrong");
      System.exit(0);
    }
    
    if(!Arrays.equals(copy, rates)) {
      System.out.println("getChargeStartTime() should NOT change the ratesTable variable!");
      System.exit(0);
    }
    
    if(charger.hoursChecked.size() != 24) {
      System.out.println("getChargeStartTime() is not calling getChargingCost() with the 24 different possible start times");
      System.exit(0);
    } 
    
    if(charger.illegalStartTime) {
      System.out.println("getChargeStartTime() is calling getChargingCost() with an illegal start time");
      System.exit(0);
    } 
    
    if(charger.chargeTimesChecked.size() != 1 || charger.chargeTimesChecked.first() != length) {
      System.out.println("getChargeStartTime() is calling getChargingCost() with a length different than the given parameter");
      System.exit(0);
    }
  }
}

class CustomBatteryCharger extends BatteryCharger {
  
  TreeSet<Integer> hoursChecked = new TreeSet<Integer>();
  boolean illegalStartTime = false;
  TreeSet<Integer> chargeTimesChecked = new TreeSet<Integer>();
  
  public int getChargingCost(int startHour, int chargeTime) {
    
    if(startHour >= 0 && startHour <= 23) {
      hoursChecked.add(startHour);
    }
    else {
      illegalStartTime = true;
    }
    chargeTimesChecked.add(chargeTime);
    
    return super.getChargingCost(startHour, chargeTime);
  }  
}