close

Bark Class :

public class Bark
{
private string sound;

public Bark(string sound)
{
this.sound = sound;
}

public string getSound()
{
return sound;
}

public Boolean equals(Bark bark)
{
Bark otherBark = (Bark)bark;
if (this.sound.Equals(otherBark.sound))
{
return true;
}
return false;
}
}

 

DogDoor Class:

public class DogDoor
{
//門的狀態
private Boolean open;
//宣告一個存放Bark class 型別的list
private List<Bark> allowedBarks;

//建構子初始化門的狀態
public DogDoor()
{
this.allowedBarks = new List<Bark>();
open = false;
}

//將叫聲存於list中
public void addAllowedBrak(Bark bark)
{
allowedBarks.Add(bark);

}

//回傳所有合法叫聲
public List<Bark> getAllowedBarks()
{
return allowedBarks;
}

//開門方法
public string OpenDoor()
{
open = true;
return "the door open and after 5 sec will close.";
//這裡加timer寫自動關門程式
}
//關門方法
public string CloseDoor()
{
open = false;
return "the door close.";
}
//回傳門的狀態
public Boolean isOpen()
{
return open;
}
}

 

BarkRecognizer Class:

public class BarkRecognizer
{
//同樣方式,將傳入參數指定給給宣告的field
private DogDoor door;
public BarkRecognizer(DogDoor door)
{
this.door = door;
}

//狗門接收狗叫聲之方法
public Boolean recognize(Bark bark)
{
//取得所有合法聲音
List<Bark> allowedBarks = door.getAllowedBarks();

foreach (var item in allowedBarks)
{
//去呼叫Bark類別下equals方法
if (item.equals(bark))
{
//狗門廳到合法聲音將門打開,這方法中過幾秒會把門關上
door.OpenDoor();
//回傳0代表合法
return true;
}
}
//回傳1代表不合法聲音
return false;
}
}

 

Remote Class : 

public class Remote
{
//宣告一個DogDoor class之型別變數 : door
private DogDoor door;

public Remote(DogDoor door)
{
//將傳入之參數door値指派給一開始宣告的door
this.door = door;
}

public string pressButton()
{
//呼叫判斷門目前狀態,若打開執行關門方法
if (door.isOpen())
door.CloseDoor();
else//若關閉,執行開門方法
door.OpenDoor();

return "You press the button.";

}
}

 

webpage .CS:

protected void Page_Load(object sender, EventArgs e)
{
//紀錄狗的合法叫聲
DogDoor door = new DogDoor();
door.addAllowedBrak(new Bark("rowlf"));
door.addAllowedBrak(new Bark("rooowlf"));
door.addAllowedBrak(new Bark("rawlf"));
door.addAllowedBrak(new Bark("woof"));

//傳入要聲音認證要打開的門給BarkRecognizer
BarkRecognizer recognizer = new BarkRecognizer(door);
//傳入要按下按鈕要打開的門給Remote
Remote remote = new Remote(door);

Response.Write("Bruce starts barking." + "</br>");
//傳入狗的聲音(一個Bark物件)
validate(recognizer.recognize(new Bark("rowlf")));


//合法的狗Bruce要回家了
Response.Write("Bruce all done..." + "</br>");
Response.Write("...but he's stuck outside!." + "</br>");


//這時候有另一隻狗再叫
Bark smallDogBark = new Bark("yip");
Response.Write("A small dog starts barking." + "</br>");
//傳入聲音去認證
validate(recognizer.recognize(smallDogBark));


//合法的狗Bruce發出叫聲
Response.Write("Bruce starts barking." + "</br>");
validate(recognizer.recognize(new Bark("rowlf")));

} // end of pageload

 

//判斷叫聲是否合法的方法
public void validate(Boolean legal)
{
if (legal == true)
Response.Write("The door open and dog go outside." + "</br>");
else
Response.Write("The door can not open because bark illegal" + "</br>");
}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 melomelo1988 的頭像
    melomelo1988

    melo 唐

    melomelo1988 發表在 痞客邦 留言(0) 人氣()