Last active
December 2, 2019 08:57
-
-
Save ourai/efd6e38a8e7fc59bbd1a5acfafdc0bd8 to your computer and use it in GitHub Desktop.
Get water from water dispenser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type WaterVolume = number; | |
interface IPerson { | |
id: number; // 人员标识 | |
competent: boolean; // 是否有能力换水 | |
altruistic: boolean; // 是否乐于助人 | |
} | |
// 所有员工 | |
const staffs: IPerson[] = []; | |
/** | |
* 饮水机剩余水量 | |
*/ | |
function getRemainWaterOfDispenser(): WaterVolume { | |
return 0; | |
} | |
/** | |
* 重装水桶 | |
* | |
* @param person 换水人 | |
*/ | |
function reloadWaterBucket(person: IPerson): boolean { | |
return true; | |
} | |
/** | |
* 从饮水机接水 | |
* | |
* @param person 接水人 | |
* @param need 需要接的水量 | |
*/ | |
function getWaterFromDispenser(person: IPerson, need: WaterVolume): boolean { | |
const remainNeed = need - getRemainWaterOfDispenser(); | |
if (remainNeed < 0 || (remainNeed === 0 && (!person.competent || !person.altruistic))) { | |
return true; | |
} | |
if (remainNeed === 0 || person.competent) { | |
return reloadWaterBucket(person); | |
} | |
const helper = staffs.find((s: IPerson): boolean => s.id !== person.id && s.altruistic && s.competent); | |
if (!helper) { | |
throw new Error('没水喝,渴着吧!'); | |
} | |
return reloadWaterBucket(helper); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment