Commit c57c1035 authored by saad.aswad's avatar saad.aswad

Final file, for submission.

parent 6d516c99
import java.util.Random;
public class Main {
public static final int MAX_PASSWORD = 9999;
public static void main(String[] args) {
Random random = new Random();
int password = random.nextInt(MAX_PASSWORD + 1);
Vault vault = new Vault(password);
System.out.println("Cops on the way !!!");
System.out.println("Password is: " + password);
Thread ascendingHacker = new AscendingHackerThread(vault);
ascendingHacker.setName("AscendingHacker");
Thread descendingHacker = new DescendingHackerThread(vault);
descendingHacker.setName("DescendingHacker");
Thread police = new PoliceThread();
police.setName("Cops");
ascendingHacker.setPriority(Thread.MAX_PRIORITY);
descendingHacker.setPriority(Thread.MAX_PRIORITY);
ascendingHacker.start();
descendingHacker.start();
police.start();
}
static class Vault {
private int password;
public Vault(int password) {
this.password = password;
}
public boolean isCorrectPassword(int guess) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return guess == this.password;
}
}
static abstract class HackerThread extends Thread {
protected Vault vault;
Thread th = new Thread(){
@Override
public void run(){
System.out.println("Now doing the thread");
System.out.println(Thread.currentThread().getName() + " Priority " + Thread.currentThread().getPriority());
public HackerThread(Vault vault) {
this.vault = vault;
}
@Override
public void run() {
hackVault();
}
protected abstract void hackVault();
protected void win(int guess) {
System.out.println("!!! " + this.getName() + " nailed it password was: " + guess + " !!!");
System.exit(0);
}
}
static class AscendingHackerThread extends HackerThread {
public AscendingHackerThread(Vault vault) {
super(vault);
}
@Override
protected void hackVault() {
for (int guess = 0; guess <= MAX_PASSWORD; guess++) {
if (vault.isCorrectPassword(guess)) {
win(guess);
}
}
};
}
}
static class DescendingHackerThread extends HackerThread {
public DescendingHackerThread(Vault vault) {
super(vault);
}
@Override
protected void hackVault() {
for (int guess = MAX_PASSWORD; guess >= 0; guess--) {
if (vault.isCorrectPassword(guess)) {
win(guess);
}
}
}
}
th.setName("New Thread");
th.setPriority(Thread.MAX_PRIORITY);
System.out.println("Thread will start");
th.start();
System.out.println("Thread has ended");
static class PoliceThread extends Thread {
@Override
public void run() {
for (int i = 10; i > 0; i--) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i + " Seconds left..");
}
System.out.println(">>> Time is up <<<");
System.exit(0);
}
}
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment