i219 Software Design Methodology
8. Dynamic modeling 1
Kazuhiro Ogata (JAIST)

2

Outline of lecture• Use case
• Use case diagram• State (machine) diagram• Sequence diagram• Class diagram of vending machine• Vending machine in Java3

Use case (1)Let us design a vending machine that sells milk, juice & coffee
whose costs are 40Yen, 80Yen & 120Yen, and that accepts
10Yen, 50Yen & 100Yen coins.
First think about some of its use cases.
Each use case is a collection of scenarios that describe inwhich way users interact with a system to be developed toachieve some purpose.

4use case name

Use case (1)one main success & two
exceptional scenarios

Insert Coin

Select Item

Main Success Scenario:
1. User inserts 10Yen, 50Yen or100Yen coin
2. System records it

Main Success Scenario:
1. User selects milk, juice or coffee
2. System delivers the item andsubtracts its cost from the coinsinserted

Extensions:
2a: The inserted one is different
from those three coins.1: System lets user knowwhat coins can be accepted
one main success & one
exceptional scenarios

Extensions:
2a: The selected one is different fromthose three items.1: System lets user know whatitems can be selected
2b: The coins inserted are notenough for the item
.1: System lets user know it5Use case (2)Show Items

Show Usage

Main Success Scenario:
1. User asks system to show
items bought
2. System displays themMain Success Scenario:
1. User asks system to let him/herknow how to use system
2. System displays the usage

Show Coins

Stop Buying

Main Success Scenario:
1. User asks system to show
coins inserted
2. System displays themMain Success Scenario:
1. User lets system know he/shewill stop buying
2. System displays items bought &returns change

6

Use case diagram
name of system to be developed

actor (role)use caseuse casesystemboundary

7State diagram (1)A state (machine) diagram describes the behavior of (a singleobject of) a classevent (that triggers the state transition); option
state transition can be taken if guard holds; option
activities (that are executedduring state transition); option

state1

trigger‐signature [guard] / activity

state2state transition

state (which may represent multiple concrete states)

8State diagram (2)Initial pseudostate final state

state in which a user is prompted
to input some commands

event‐trigger

activity (the items bought are displayed and the change is returned)

9State diagram (3)the number of 100Yen coins in Cash Box is incremented
coin ≠ 10 & coin ≠ 50 & coin ≠ 100

the event‐trigger has one
parameter coin

the two state transitions can be combined:[coin=50] / #50++

10State diagram (4)if the cash in the cash box is greater than or equal to120Yen, then the number of coffee bought isincremented & the cash is subtracted by 120Yen
not enough cash for milk

can be combined:[cashBox < 40Yen] / not enough cash for milk msg is displayed

11State diagram (5)“Stop Buying” “Show Usage”
use caseuse case“Show Items”
use case“Show Coins” “Insert Coin” use caseuse case“Select Item” use case

12

Sequence diagram (1)A sequence diagram describes interaction among participants
such as objects such that a participant sends a message toanother participant and waits for some result.

13

Sequence diagram (2)(synchronous)
messageparticipantlife line

found message
returnactivation

14

Sequence diagram (3)alternative multiple
fragments; the 1st
fragment whose
condition holds is onlyexecuted

self callreturn from self callcondition

if t < cost holds, thenthe 1st fragment isexecuted; otherwise,
the 2nd one is executed

if b is true, then the 1st
fragment is executed;
otherwise, the 2nd one
is executedframeSequence
diagram (4)15loop; the fragment isrepeatedly executed while
the condition(#10YenCoin < c) holds 10*c is the cost of the item.
 if the number of 10Yen coins
is greater than or equal to c, wecan pay for the item.
 Otherwise, if there is a 50Yen(or 100Yen) coin, it is changed
into five 10Yen coins (or five10Yen & one 50Yen coins).
 Otherwise, we cannot.

16

Class diagram of vending machine

enumeration (enum type) in Java is a class and then it may have attributes(fields) and methods; 40 of MILK(40) is the attribute (cost) owned by theinstance (object) MILK of the class17

Vending machine in Java (1)package in which HashMap, ect. are declared
import java.util.*;package in which BufferedReader, etc. are declared
import java.io.*;package in which Pattern, etc. are declared
import java.util.regex.*;public class VMCLP {private static void showBanner() {…} it decodes bytes read from thestandard input into characters
private static void help() {…}public static void main(String[] args) throws IOException {VendingMachine vm = new VendingMachine();String line;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
showBanner(); it reads character from the standard input in a buffered way
while ((line = br.readLine()) != null) {…} }}it reads a line of text and returns the lineexcluding a line break as a String

will be explained in the following
pages

18

Vending machine in Java (2)it removes white spaces at both sides of the Stringline = line.trim();
if (line.equals("")) { // Nothing is done. }else if (line.equals("stop")) {“Stop Buying” use case
Map<Item,Integer> items = vm.getItemBox();
Map<Coin,Integer> coins = vm.getCashBox();
System.out.println("Items bought: " + items);
System.out.println("Change: " + coins);
System.out.println("bye bye!!"); break; }“Show Usage” use caseelse if (line.equals("help")) { help(); }“insert”
needs an argument
else if (line.equals("insert")) {System.err.println("Usage: insert [10|50|100]"); }“select” needs an argument
else if (line.equals("select")) {System.err.println("Usage: select [milk|juice|coffee]"); }else if (line.equals("showItems")) { System.out.println(vm.getItemBox()); }else if (line.equals("showCoins")) { System.out.println(vm.getCashBox()); }“Show Items” & “Show Coins” use cases

19

Vending machine in Java (3)checking if line starts with “insert”, followed by one
space or tab and then one or more characters
split into two strings such that the1st string is the one just before
else if (Pattern.matches("insert[ ¥t].*",line)) {the 1st space or tab, and the 2ndString[] la = line.split("[ ¥t]",2);string is the one just after the 1stif (la.length < 2) { // You never get here!
space or tab

assert false : "In insert command: " + line; }else { String a = la[1].trim();if (a.equals("10")) { vm.insert(Coin.TEN); }else if (a.equals("50")) { vm.insert(Coin.FIFTY); }else if (a.equals("100")) { vm.insert(Coin.HUNDRED); }else { System.err.println("Usage: insert [10|50|100]"); } } }will be explained later“Insert Coin” use case20

Vending machine in Java (4)else if (Pattern.matches("select[ ¥t].*",line)) {“Select Item” use case
String[] la = line.split("[ ¥t]",2);if (la.length < 2) {…} else { String a = la[1].trim();
if (a.equals("milk")) {if (vm.select(Item.MILK)) { System.out.println("you bought one milk.");} else { System.out.println("Not enough coins!"); } }else if (a.equals("juice")) {if (vm.select(Item.JUICE)) { System.out.println("you bought one juice.");} else { System.out.println("Not enough coins!"); } }else if (a.equals("coffee")) {if (vm.select(Item.COFFEE)) {System.out.println("you bought one coffee.");} else { System.out.println("Not enough coins!"); } }else { System.err.println("Usage: select [milk|juice|coffee]"); } }} else { System.err.println("No such a command!!!"); }System.out.print("VM> "); }21

Vending machine in Java (5)assert can be used to check something that must hold.
public class TestAssert {public static void main(String[] args) {Integer m = new Integer(3); Integer n = new Integer(3);assert m == n : "m & n refer to different objects"; } }Boolean expression

an expression such as a string (option)“-ea” option such as “java -ea TestAssert” should be used to makeassert effective. If so, this program throws an AssertionError
exception because m == n becomes false.
If m & n are reference types, m == n becomes true if and only if m& n refer to exactly the same object; otherwise, it becomes false.
m.equals(n) must be true in the above example.
checks if the contents are the same22

Vending machine in Java (6)40, 80 & 120 are attributes of MILK,JUICE & COFFEE, respectively

declaration of constants should ended
with ; if there are attributes/methods inan enum type

public enum Item {MILK(40), JUICE(80), COFFEE(120);
private final int cost;private Item(int c) { cost = c; }public int getCost() { return cost; }}Constructors of enum types are private
because any more instances cannot bemadeattributes (fields) are final andthen the attribute cost of each
item cannot be changed

23

Vending machine in Java (7)public class VendingMachine {private Map<Coin,Integer> cashBox;
private Map<Item,Integer> itemBox;
public VendingMachine() {cashBox = new HashMap<Coin,Integer>(); cashBox.put(Coin.TEN,0);
cashBox.put(Coin.FIFTY,0); cashBox.put(Coin.HUNDRED,0);
itemBox = new HashMap<Item,Integer>(); itemBox.put(Item.MILK,0);
itemBox.put(Item.JUICE,0); itemBox.put(Item.COFFEE,0); }public Map<Coin,Integer> getCashBox() { return cashBox; }public Map<Item,Integer> getItemBox() { return itemBox; }public void insert(Coin c) { int x = cashBox.get(c); cashBox.put(c,x+1); }private int total() { return cashBox.get(Coin.TEN)*10+cashBox.get(Coin.FIFTY)*50+cashBox.get(Coin.HUNDRED)*100; }private boolean pay(int c) {…}public boolean select(Item i) {…} }sequence diagram that starts with insert(c: Coin)

24

Vending machine in Java (8)sequence diagram that starts with select(i: item)

public boolean select(Item i) {int cost = i.getCost();if (total() < cost) return false;boolean b = pay(cost/10);
double check; there may be some flaws in
if (b) {the implementation of pay
int c = itemBox.get(i);itemBox.put(i,c+1);
return true;} else {// you never get here!
assert false : "cashBox: " + cashBox + ", " + "cost: " + cost;
return false;
}
}

25

Vending machine in Java (9)sequence diagram that starts with pay(c: int)

private boolean pay(int c) {50Yen coins is changed into
while (cashBox.get(Coin.TEN) < c) {five 10Yes coins
if (cashBox.get(Coin.FIFTY) > 0) {int t1 = cashBox.get(Coin.FIFTY); int t2 = cashBox.get(Coin.TEN);
cashBox.put(Coin.FIFTY,t1-1); cashBox.put(Coin.TEN,t2+5);} else if (cashBox.get(Coin.HUNDRED) > 0) {int t1 = cashBox.get(Coin.HUNDRED);
int t2 = cashBox.get(Coin.FIFTY); int t3 = cashBox.get(Coin.TEN);
cashBox.put(Coin.HUNDRED,t1-1);
cashBox.put(Coin.FIFTY,t2+1); cashBox.put(Coin.TEN,t3+5);} else { return false; } }not enough cash
int t = cashBox.get(Coin.TEN);
assert t >= c : "t >= c must hold but, " + "t=" + t + " & c=" + c;
cashBox.put(Coin.TEN,t-c);
100Yen coins is changed into five
return true; }10Yen coins & one 50Yen coinsubtract c 10Yen coins from the cash box

26

Summary• Use case
• Use case diagram• State (machine) diagram• Sequence diagram• Class diagram of vending machine• Vending machine in Java