Skip to content

Instantly share code, notes, and snippets.

@swarut
Created October 10, 2012 18:07
Show Gist options
  • Save swarut/3867252 to your computer and use it in GitHub Desktop.
Save swarut/3867252 to your computer and use it in GitHub Desktop.
Homework#1 Example
public class Mobile{
private Keyboard keyboard;
private Screen screen;
private Stack callStack;
private State phoneState;
private String currentNumber;
enum State{
on,
off,
onCall
}
public static void main(String[] args){
}
public Mobile(){
this.keyboard = new Keyboard();
this.screen = new Screen();
this.speaker = new Speaker();
}
private void onKeyPressed(){
Key key = keyboard.pressedKey();
// when telephone is standing by (at-home state)
if(phoneState == State.on){
if(key.keyType == Key.KeyType.number){
this.updateScreen(key.value);
this.updateCurrentNumber;
}
else{
if(key.value == "call"){
this.call(this.currentNumber);
}
else if(key.value == "end_call"){
this.endCall();
if(!callStack.isEmpty){
this.resumeCall();
}
}
else{
this.turnOff;
}
}
}
// when mobile is off
else if(phoneState == State.off){
if(key.value == "turn_on")
this.turnOn();
}
// when the phone is on a call
else if(phoneState == State.onCall){
if(key.value == "call"){
// pause current call, getting a new call
this.pauseCurrentCall();
this.receiveCall;
}
}
}
private turnOn(){}
private turnOff(){}
private call(String number){}
private pauseCurrentCall(){
this.callStack.push(this.currentNumber);
this.currenNumber = newNumber; // it's not defined here, but suppose this existed
}
private resumeCall(){}
private endCall(){
this.callStack.pop();
}
private receiveCall(){}
private void updateScreen(String value){
this.screen.append(value);
}
private void updateCurrentNumber(String value){
this.currentNumber = this.currentNumber + value;
}
}
public class Keyboard{
public Key[] keys;
public Keyboard(){
this.keys = {
new Key("1","1", Key.KeyType.number),
new Key("3", Key.KeyType.number),new Key("4", Key.KeyType.number),
new Key("5", Key.KeyType.number),new Key("6", Key.KeyType.number),
new Key("7", Key.KeyType.number),new Key("8", Key.KeyType.number),
new Key("9", Key.KeyType.number),new Key("0", Key.KeyType.number),
new Key("call", Key.KeyType.command), new Key("end_call", Key.KeyType.command)
}
}
public void pressedKey(){
// return instance of Key that is pressed
}
}
public class Key{
public String label;
public String value;
public KeyType keyType;
enum KeyType{
number,
command
}
public Key(String label, String value, KeyType keyType){
this.label = label;
this.value = value;
this.keyType = keyType;
}
public Key(String label, KeyType keyType){
this(label, label);
}
}
public class Screen{
private String output;
private Stack screenStack;
public void clear(){
screenStack.clear();
}
public void append(String text){
screenStack.push(text);
}
public void delete(){
screenStack.pop();
}
public void showOutput(String text){
//print output on real screen
}
}
public class Stack{
public Stack(){
}
public Object pop(){
}
public Object push(){
}
public Object topOfStack(){
}
public Object clear(){
}
public Object reversePopAll(){
}
public boolean isEmpty{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment