I have an assignment to implemented a Quarantine project where I was left with the some unit tests and a skeleton of the implementation. I provided the solution below and get rejected from the interview. The feedbacks I get (I feel lucky that they provided, usually, they have legal issues) are,
- NO indicator of design (DDD, or design patterns)
- The code is not scalable (extendable)
Overall, this is C code written in Java (bit harsh and made me sad). Is there any better way to do that? I mainly looking suggestions from the very experienced engineers.
public class QuarantineTest {
private Quarantine quarantine;
@Before
public void setUp() {
// The responsibility of the Quarantine object is to simulate diseases on a group of patients.
// It is initialized with a list of patients' health status, separated by a comma.
// Each health status is described by one or more characters
// (in the test below, we will always have only one disease / patient)
// The characters mean:
// H : Healthy
// F : Fever
// D : Diabetes
// T : Tuberculosis
quarantine = new Quarantine("F,H,D,D,D,H,T");
// Quarantine provides medicines to the patients, but can not target a specific group of patient.
// The same medicines are always given to all the patients.
// Then Quarantine can provide a report with this format:
// "F:1 H:2 D:0 T:1 X:3"
// Report give the number of patients that have the given disease.
// X means Dead
}
@Test
public void beforeTreatment() throws Exception {
assertEquals("F:1 H:2 D:3 T:1 X:0", quarantine.report());
}
// people died in the Diabetes
@Test
public void noTreatment() throws Exception {
quarantine.wait40Days();
// diabetics die without insulin
assertEquals("F:1 H:2 D:0 T:1 X:3", quarantine.report());
}
// feaver is cured
// people died in the Diabetes
@Test
public void aspirin() throws Exception {
quarantine.aspirin();
quarantine.wait40Days();
// aspirin cure Fever
assertEquals("F:0 H:3 D:0 T:1 X:3", quarantine.report());
}
@Test
public void antibiotic() throws Exception {
quarantine.antibiotic();
quarantine.wait40Days();
// antibiotic cure Tuberculosis
// but healthy people catch Fever if mixed with insulin.
assertEquals("F:1 H:3 D:0 T:0 X:3", quarantine.report());
}
@Test
public void insulin() throws Exception {
quarantine.insulin();
quarantine.wait40Days();
// insulin prevent diabetic subject from dying, does not cure Diabetes,
assertEquals("F:1 H:2 D:3 T:1 X:0", quarantine.report());
}
@Test
public void antibioticPlusInsulin() throws Exception {
quarantine.antibiotic();
quarantine.insulin();
quarantine.wait40Days();
// if insulin is mixed with antibiotic, healthy people catch Fever
assertEquals("F:3 H:1 D:3 T:0 X:0", quarantine.report());
}
@Test
public void paracetamol() throws Exception {
quarantine.paracetamol();
quarantine.wait40Days();
// paracetamol heals fever
assertEquals("F:0 H:3 D:0 T:1 X:3", quarantine.report());
}
@Test
public void paracetamolAndAspirin() throws Exception {
quarantine.paracetamol();
quarantine.aspirin();
quarantine.wait40Days();
// paracetamol kills subject if mixed with aspirin
assertEquals("F:0 H:0 D:0 T:0 X:7", quarantine.report());
}
}
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Quarantine {
private Map<Character, Integer> map;
boolean insuline;
boolean wait40Days;
boolean antibiotic;
boolean aspirin;
boolean paracetamol;
public Quarantine(String subjects) {
try {
map = Pattern.compile(",")
.splitAsStream(subjects)
.collect(Collectors.groupingBy(
s -> s.charAt(0),
LinkedHashMap::new,
Collectors.collectingAndThen(Collectors.counting(), Long::intValue)
));
map.put('X', 0);
} catch (Exception e) {
e.printStackTrace();
}
}
// aspirin cures fever
public void aspirin() {
aspirin = true;
}
public void antibiotic() {
antibiotic = true;
}
public void insulin() {
insuline = true;
}
public void paracetamol() {
paracetamol = true;
}
public void wait40Days() {
if (antibiotic) {
// antobiotic is mixed with the insuline
if (insuline) {
map.put('F', map.get('F') + map.get('H'));
map.put('H', map.get('T'));
map.put('T', 0);
return;
}
// only the antibiotic
else {
map.put('H', map.get('H') + map.get('T'));
map.put('T', 0);
wait40Days = true;
}
} else if (paracetamol) {
// paracetamol mixed with the aspirin kills everyone
if (aspirin) {
map.put('X', map.get('X') + map.get('F') + map.get('H') + map.get('D') + map.get('T'));
map.put('F', 0);
map.put('H', 0);
map.put('D', 0);
map.put('T', 0);
return;
} else { // only provides the paracetamol as medication
map.put('H', map.get('H') + map.get('F'));
map.put('F', 0);
wait40Days = true;
}
} else if(aspirin) { // only provides aspirin as medication
map.put('H', map.get('H') + map.get('F'));
map.put('F', 0);
wait40Days = true;
} else if (insuline) {
// only provision of insuline prevents death from the diabetes
return;
} else { // no medicine was provided, just waited for the 40 days
wait40Days = true;
}
/*
check if we will needs to wait for 40 days
after the medication to see the affect
* */
if (wait40Days) {
map.put('X', map.get('D'));
map.put('D', 0);
wait40Days = false;
}
}
// get the Quarantine report
public String report() {
try {
final String[] result = {""};
map.forEach((k, v) -> result[0] += k.toString() + ":" + v.toString() + " ");
return result[0].trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
wait40Days
in more small methods. \$\endgroup\$ – alayor Feb 9 '17 at 5:30have a quarantine class, which acts as a factory for treatments and as control object, etc
, do you mean I need to useFactory
design patterns for theQuarantine
class ? I will see the factory in the night and will try to implement the code. \$\endgroup\$ – Arefe Feb 9 '17 at 13:25