
臺北捷運公司 106 年4月9日新進工程員(二)(資訊類)
甄試試題-應用系統程式設計
第 2 頁,共 5 頁
途。
public Alert() { warning = 0; }
public String sendAle r t(int code) {
warning = code;
if (code == 2) {
Console.Write("Urgent! ");
return "Urgent !";
}
else if (code == 1) {
Console.Write(" HELP ! ");
return "HELP!";
}
else {
Console.Write("OK! " );
return "OK";
}
}
public bool wasAlertSend() {
if (warning == 0) return false;
return true;
}
privat e int warning;
}
public class Door {
public Door( ) {status ="CLOSE";}
public String Status {
get { return status; }
set { status = value; }
}
private String status;
}
public inter face I Monitor {
int execute();
}
public class Monitor:IMo nitor{
public Monitor(Door d) {door = d;}
public int execute() {
String s = door.Status;
if (s =="BROKEN") return 2;
else if (s =="OPEN") return 1;
else return 0;
}
private Door door;
}
public Server(Alert a) { alert = a; }
public void monitor() {
int code = doorMonitor.execute();
if (code > 0) alert.sendAlert(code);
}
public void setMonitor (IMonitor dm){
doorMonitor = dm;
}
private IMonitor doorMonitor;
private Alert alert;
}
public class ServerApp {
private static Door door;
private static Alert alert;
private static IMonitor monitor;
private static Server server;
private static void te stAlert(String msg) {
door = new Door();
alert = new Alert();
monitor = new Monitor(door);
Server server = new Server(alert);
server.setMonitor(monitor);
door.Status = msg;
server.monitor();
if (!alert.wasAlertSend())
Console.Write("OK! ");
}
public static void te st0 1() {
alert = new Alert();
Console.WriteLine(alert.sendAlert(2)); //
}
public static void te st0 2() {
door = new Door();
door.Status = "OPEN";
Console.WriteLine(door.Status); //
}
public static void te st0 3() {
Door door = new Door();
Monitor monitor = new Monitor(door);
door.Status = "OPEN";
Console.WriteLine(monitor.execute()); //
}
public static void te st0 4() {
testAlert("OPEN"); //
testAlert("CLOSE"); //
testAlert("BROKEN"); //
}