forked from khwang0/Line-chatbot-for-COMP3111
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request khwang0#7 from gzwongkk/master
Add Lab 5
- Loading branch information
Showing
7 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package skeleton; | ||
|
||
import java.util.HashMap; | ||
|
||
public class Adapter { | ||
public static final String[] BEVERAGES = new String[] { | ||
"Caffè Americano", "Caffè Mocha", "Caffè Latte", | ||
"Cappuccino", "Caramel Macchiato", "Espresso" }; // You can change these | ||
|
||
/** | ||
* This function compute the edit distance between a string and every | ||
* strings in your selected beverage set. The beverage with the minimum | ||
* edit distance <= 3 is returned. The use of Wagner_Fischer algorithm | ||
* is shown in the main function in WagnerFischer.java | ||
**/ | ||
public String getBeverage(String s){ | ||
// TODO: find the word with minimum edit distance | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package skeleton; | ||
|
||
public class Observer { | ||
private int id; | ||
private Subject subject; | ||
|
||
public Observer(int id) { | ||
this.id = id; | ||
} | ||
|
||
public int getID(){ | ||
return id; | ||
} | ||
|
||
public void subscribe(Subject sub) { | ||
this.subject = sub; | ||
} | ||
|
||
public void unsubscribe() { | ||
subject.unregister(this); | ||
} | ||
|
||
public void update(){ | ||
// TODO: The observer will exit the queue | ||
// once the notification has value >= this.id+7. | ||
// Don't forget they will leave if it is their number too. | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package skeleton; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Subject { | ||
private List<Observer> observers; | ||
private String message; | ||
private boolean changed; | ||
|
||
public Subject() { | ||
observers = new ArrayList<Observer>(); | ||
message = null; | ||
changed = false; | ||
} | ||
|
||
public void register(Observer obj) { | ||
if ( !observers.contains(obj) ) observers.add(obj); | ||
} | ||
|
||
public void unregister(Observer obj) { | ||
observers.remove(obj); | ||
} | ||
|
||
public void notifyObservers() { | ||
// TODO: notify every observers | ||
} | ||
|
||
public void setMessage(String msg) { | ||
this.message=msg; | ||
this.changed=true; | ||
notifyObservers(); | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setChanged(boolean changed) { | ||
this.changed = changed; | ||
} | ||
|
||
public List<Observer> getQueue() { | ||
return observers; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package skeleton; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class Test { | ||
public static final String MESSAGE_WELCOME = "Welcome to Starbucks!"; | ||
public static final String MESSAGE_SEPERATOR = "==============================================="; | ||
public static final String MESSAGE_SELECT = "Select functions:\n 1) Order\n 2) An order is ready\n 3) Check the queue\n 4) Exit"; | ||
public static final String MESSAGE_SELECT_ONE = "Which drink you would like to order?"; | ||
public static final String MESSAGE_SELECT_TWO = "Which order is ready?"; | ||
public static final String MESSAGE_SELECT_THREE = "The following ids are in the queue:"; | ||
public static final String MESSAGE_SELECT_FOUR = "Goodbye!~"; | ||
public static final String MESSAGE_ERROR = "Please enter the number."; | ||
|
||
public static void main(String[] args) { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
// TODO: possible setup | ||
Subject starbucks = new Subject(); | ||
Adapter adapter = new Adapter(); | ||
int id = 1; | ||
|
||
try { | ||
System.out.println(MESSAGE_WELCOME); | ||
loop: while(true) { | ||
System.out.println(MESSAGE_SEPERATOR); | ||
System.out.println(MESSAGE_SELECT); | ||
String in = br.readLine(); | ||
switch(in){ | ||
case "1": | ||
System.out.println(MESSAGE_SELECT_ONE); | ||
String drink = br.readLine(); | ||
// TODO: give an ordered id | ||
// order is successful only if edit distance <= 3 | ||
String beverage = adapter.getBeverage(drink); | ||
if ( beverage != null ) { | ||
Observer customer = new Observer(id++); | ||
starbucks.register(customer); | ||
customer.subscribe(starbucks); | ||
System.out.println( | ||
String.format("%s is ordered and your order id is %d" | ||
, beverage, customer.getID())); //success | ||
} else { | ||
System.out.println(String.format("%s not found.", drink)); //fail | ||
} | ||
break; | ||
case "2": | ||
System.out.println(MESSAGE_SELECT_TWO); | ||
String order = br.readLine(); | ||
// TODO: act appropriately according to your design | ||
starbucks.setMessage(order); | ||
break; | ||
case "3": | ||
System.out.println(MESSAGE_SELECT_THREE); | ||
// print all id in the queue | ||
for( Observer obs : starbucks.getQueue() ) System.out.println(obs.getID()); | ||
break; | ||
case "4": | ||
break loop; | ||
default: | ||
System.out.println(MESSAGE_ERROR); | ||
} | ||
} | ||
|
||
br.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package skeleton; | ||
public class WagnerFischer { | ||
private char[] s1; | ||
private char[] s2; | ||
|
||
public WagnerFischer(String s1, String s2) { | ||
this.s1 = s1.toLowerCase().toCharArray(); | ||
this.s2 = s2.toLowerCase().toCharArray(); | ||
} | ||
|
||
private int min(int a, int b, int c) { | ||
return Math.min(a, Math.min(b, c)); | ||
} | ||
|
||
/** | ||
* Using Dynamic Programming, the Wagner-Fischer algorithm is able to | ||
* calculate the edit distance between two strings. | ||
* @return edit distance between s1 and s2 | ||
*/ | ||
public int getDistance() { | ||
int[][] dp = new int[s1.length + 1][s2.length + 1]; | ||
for (int i = 0; i <= s1.length; dp[i][0] = i++); | ||
for (int j = 0; j <= s2.length; dp[0][j] = j++); | ||
|
||
for (int i = 1; i <= s1.length; i++) { | ||
for (int j = 1; j <= s2.length; j++) { | ||
if (s1[i - 1] == s2[j - 1]) { | ||
dp[i][j] = dp[i - 1][j - 1]; | ||
} else { | ||
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, | ||
dp[i - 1][j - 1] + 1); | ||
} | ||
} | ||
} | ||
return dp[s1.length][s2.length]; | ||
} | ||
|
||
public static void main(String[] args) { | ||
WagnerFischer wf = new WagnerFischer("Caffe Mocha", "coffee moka"); | ||
System.out.println(wf.getDistance()); | ||
wf = new WagnerFischer("Frappuccino", "fappiccino"); | ||
System.out.println(wf.getDistance()); | ||
} | ||
|
||
} |
Binary file not shown.