Created
December 16, 2013 04:38
-
-
Save dtinth/7982395 to your computer and use it in GitHub Desktop.
Example of Interface
This file contains hidden or 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
import java.io.BufferedReader; | |
import java.util.Scanner; | |
import javax.swing.JOptionPane; | |
interface Dialog { | |
void say(String message); | |
String ask(String question); | |
} | |
class ConsoleDialog implements Dialog { | |
Scanner scanner = new Scanner(System.in); | |
@Override | |
public void say(String message) { | |
System.out.println(message); | |
} | |
@Override | |
public String ask(String question) { | |
System.out.print(question + " >> "); | |
return scanner.nextLine(); | |
} | |
} | |
class GUIDialog implements Dialog { | |
@Override | |
public void say(String message) { | |
JOptionPane.showMessageDialog(null, message); | |
} | |
@Override | |
public String ask(String question) { | |
return JOptionPane.showInputDialog(question); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Dialog d; | |
if (JOptionPane.showConfirmDialog( | |
null, "Do you want to use the GUI?", "Select Dialog", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { | |
d = new GUIDialog(); | |
} else { | |
d = new ConsoleDialog(); | |
} | |
d.say("Hello"); | |
String x = d.ask("What is your name?"); | |
d.say("Hello, " + x); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment