Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created December 16, 2013 04:38
Show Gist options
  • Save dtinth/7982395 to your computer and use it in GitHub Desktop.
Save dtinth/7982395 to your computer and use it in GitHub Desktop.
Example of Interface
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