Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions Lab5/solution/Adapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package solution;

public class Adapter {
public static final String[] BEVERAGES = new String[] {
"Caff� Americano", "Caff� Mocha", "Caff� Latte",
"Cappuccino", "Caramel Macchiato", "Espresso" };

public String getBeverage(String s){
// Return null if nothing is similar
int min = Integer.MAX_VALUE;
String min_string = null;
// Compare each word in the array with the query word
for ( String b : BEVERAGES ) {
WagnerFischer ob = new WagnerFischer( s, b );
int dist = ob.getDistance();
// Record the minimum
if ( dist <= 3 && dist < min ) {
min = dist;
min_string = b;
}
}
return min_string;
}
}
31 changes: 31 additions & 0 deletions Lab5/solution/Observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package solution;

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(){
// Get the message first
int order = Integer.valueOf(subject.getMessage());
// React based on the message
if ( order >= getID()+7 || order == getID() ) {
unsubscribe();
}
}
}
60 changes: 60 additions & 0 deletions Lab5/solution/Subject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package solution;

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() {
// if nothing is changed, we can skip this
if ( !changed ) return;

// we need to use a new list because observers may unsubscribe from the list
// and this might cause unexpected behaviours
// forEach is a faster way to loop over a list (see Java 1.8 lambda expression)
new ArrayList<>(observers).forEach( o -> o.update() );

// another approach to do it is to traverse the list starting from the end
for ( int i = observers.size()-1; i >= 0; --i ) {
observers.get(i).update();
}

// reset parameter
changed = false;
}

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;
}
}
70 changes: 70 additions & 0 deletions Lab5/solution/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package solution;
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
starbucks.getQueue().forEach(o -> System.out.println(o.getID()));
break;
case "4":
break loop;
default:
System.out.println(MESSAGE_ERROR);
}
}

br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
45 changes: 45 additions & 0 deletions Lab5/solution/WagnerFischer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package solution;
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());
}

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You need to have some backgrounds on Java/Database/git in order to complete the
| | 5 | No lab at week 5 | |
| 4 | 6 | [UML](./lab_UML.pdf) | [Solution](./uml_lab_sample_answer.pdf) |
| | 7 | No lab at week 7 | |
| 5 | 8 | [Design Pattern](./lab6/lab_design_pattern.pdf) |
| 5 | 8 | [Design Pattern](./Lab5/lab_design_pattern.pdf) |[Solution](./Lab5/solution/)|
| 6 | 9 | [Refactoring](./refactoring-lab/README.md) |


Expand Down