Last active
December 13, 2015 22:39
-
-
Save argan/4986058 to your computer and use it in GitHub Desktop.
Try to pat the Unicorns from http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
This file contains 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.OutputStream; | |
import java.io.PrintStream; | |
public class Unicorn { | |
private static String str1 = "UNICORN #1: PAT THIS UNICORN ONCE"; | |
private static String str2 = "UNICORN #2: PAT THIS UNICORN ONCE"; | |
static class Printer extends PrintStream { | |
private PrintStream origin; | |
private boolean first = false; | |
private boolean second = false; | |
public Printer(OutputStream out) { | |
super(out); | |
this.origin = new PrintStream(out); | |
} | |
@Override | |
public void println(String x) { | |
if (x.equals(str1)) { | |
if (!first) { | |
origin.println(x); | |
first = true; | |
} | |
} else if (x.equals(str2)) { | |
if (!second) { | |
origin.println(x); | |
second = true; | |
} | |
} else { | |
origin.println(x); | |
} | |
} | |
} | |
static { | |
PrintStream origin = System.out; | |
System.setOut(new Printer(origin)); | |
} | |
public static boolean pat() { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment