我有一个任务来实现一个隔离项目,在那里我被留给了一些单元测试和实现的框架。我提供了下面的解决方案,并在面试中被拒绝。我得到的反馈(我感到幸运的是,他们通常有法律问题),
总的来说,这是用Java编写的C代码(有点刺耳,让我很难过)。有什么更好的方法吗?我主要是从经验丰富的工程师那里寻找建议。
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;
}
}发布于 2017-02-13 11:46:47
因此,是的,您的代码缺乏面向对象的设计和模式。简单地看一下,我认为Quarantine可以成为Medicine的工厂,但这是一个小小的改进。
首先要做的就是创建你的模型。您有Quarantine类,但也有一组Patients、一些Medecine和一组Disease。
Disease是一个事实,您可以接受它,所以它可以是一个枚举。模式枚举
Patients维护一个计数器,它可以从一个组移动到另一个组。它只是维护一个整数,a可以看作是一个装饰符。模式装饰器
class Patients {
int size;
void becomes(Patients other) {
other.size += this.size;
this.size = 0;
}
}Quarantine将一个或多个Medicine分发给所有Patients,我已经创建了一个Treatment类来维护所有的Medicine。这个类是不可变的,允许我查询它并添加或删除Medicine。quarantine可以是一个工厂,也可以是一个构建者(因为它构成了处理)。
新的Treatment类可以看作是许多模式、助手或状态]。但也是一个组合,因为它实现了Medicine,但也是一个委托。
最后,最大的部分来自所有逻辑所在的Medicine。当添加到Treatment中时,Medicine将更改它的结果。因此,Medicine必须与现有的Treatment相结合,并提供给一组Patients以产生他的效果。策略模式适用于这个类。
interface Medicine {
void on(Quarantine quarantine);
Treatment combine(Treatment treatment);
}https://codereview.stackexchange.com/questions/154869
复制相似问题