Skip to content

Commit

Permalink
Merge pull request khwang0#7 from gzwongkk/master
Browse files Browse the repository at this point in the history
Add Lab 5
  • Loading branch information
khwang0 authored Oct 21, 2017
2 parents 08487ac + 0cb6f7f commit 3898cc0
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ You need to have some backgrounds on Java/Database/git in order to complete the
| 3 | 4 | [Chatbot with SQL Database](./lab3.md)| Released |
| | 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) |


# How does the Lab works
Expand Down
19 changes: 19 additions & 0 deletions lab6/Adapter.java
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
}
}
28 changes: 28 additions & 0 deletions lab6/Observer.java
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.
}
}
46 changes: 46 additions & 0 deletions lab6/Subject.java
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;
}
}
72 changes: 72 additions & 0 deletions lab6/Test.java
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();
}
}

}
45 changes: 45 additions & 0 deletions lab6/WagnerFischer.java
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 added lab6/lab_design_pattern.pdf
Binary file not shown.

0 comments on commit 3898cc0

Please sign in to comment.