diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/ByFives.java b/Java Classes & Interfaces/Interfaces/Counter/src/ByFives.java new file mode 100644 index 0000000..13fd11e --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/ByFives.java @@ -0,0 +1,18 @@ +package src; + +public class ByFives implements Counter { + + private int num = 5; + private int nextNum; + + @Override + public int countBy() { + return num; + } + + @Override + public int getNextNum() { + return nextNum += num; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/ByFours.java b/Java Classes & Interfaces/Interfaces/Counter/src/ByFours.java new file mode 100644 index 0000000..559e878 --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/ByFours.java @@ -0,0 +1,18 @@ +package src; + +public class ByFours implements Counter { + + private int num = 4; + private int nextNum; + + @Override + public int countBy() { + return num; + } + + @Override + public int getNextNum() { + return nextNum += num; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/ByOnes.java b/Java Classes & Interfaces/Interfaces/Counter/src/ByOnes.java new file mode 100644 index 0000000..cf48515 --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/ByOnes.java @@ -0,0 +1,18 @@ +package src; + +public class ByOnes implements Counter { + + private int num = 1; + private int nextNum; + + @Override + public int countBy() { + return num; + } + + @Override + public int getNextNum() { + return nextNum += num; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/ByThrees.java b/Java Classes & Interfaces/Interfaces/Counter/src/ByThrees.java new file mode 100644 index 0000000..b0d8f7b --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/ByThrees.java @@ -0,0 +1,18 @@ +package src; + +public class ByThrees implements Counter { + + private int num = 3; + private int nextNum; + + @Override + public int countBy() { + return num; + } + + @Override + public int getNextNum() { + return nextNum += num; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/ByTwos.java b/Java Classes & Interfaces/Interfaces/Counter/src/ByTwos.java new file mode 100644 index 0000000..e78024e --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/ByTwos.java @@ -0,0 +1,18 @@ +package src; + +public class ByTwos implements Counter { + + private int num = 2; + private int nextNum; + + @Override + public int countBy() { + return num; + } + + @Override + public int getNextNum() { + return nextNum += num; + } + +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/Counter.java b/Java Classes & Interfaces/Interfaces/Counter/src/Counter.java new file mode 100644 index 0000000..2595bae --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/Counter.java @@ -0,0 +1,7 @@ +package src; + +public interface Counter { + + int countBy(); + int getNextNum(); +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/CounterHelper.java b/Java Classes & Interfaces/Interfaces/Counter/src/CounterHelper.java new file mode 100644 index 0000000..2acec0a --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/CounterHelper.java @@ -0,0 +1,34 @@ +package src; + +public class CounterHelper { + + private Counter[] counters; + private Counter userCounter = null; + + public CounterHelper(Counter... counters) { + this.counters = counters; + } + + public void doInteractive(int userInput) { + + // Find the correct counter to count by. + // When no counter is found, display error message to user and quit the program + for (Counter counter: counters) { + if (userInput == counter.countBy()) + this.userCounter = counter; + } + if (userCounter == null) { + System.out.println("Counter Not Found"); + System.exit(0); + } + } + + public void displayCounter(int iterations) { + + // Show the counter for the number of iterations that the user inputted + System.out.println(); + for (int i = 0; i < iterations; i++) { + System.out.println(userCounter.getNextNum()); + } + } +} diff --git a/Java Classes & Interfaces/Interfaces/Counter/src/CounterMain.java b/Java Classes & Interfaces/Interfaces/Counter/src/CounterMain.java new file mode 100644 index 0000000..f425d88 --- /dev/null +++ b/Java Classes & Interfaces/Interfaces/Counter/src/CounterMain.java @@ -0,0 +1,40 @@ +package src; + +import java.util.Scanner; + +public class CounterMain { + public static void main(String[] args) { + countUp(); + countUpInteractively(); + } + + private static void countUp() { + ByTwos twos = new ByTwos(); + ByFives fives = new ByFives(); + + // Display the first 5 numbers for each counter defined + for (int i = 0; i < 5; i++) { + System.out.println("Counting By " + twos.countBy()); + System.out.println(twos.getNextNum()); + System.out.println("Counting By " + fives.countBy()); + System.out.println(fives.getNextNum()); + } + } + + private static void countUpInteractively() { + Scanner in = new Scanner(System.in); + System.out.println("Enter In The Number That You Want To Count:\n"); + int userInput = in.nextInt(); + + CounterHelper helper = new CounterHelper(new ByOnes(), new ByTwos(), new ByThrees(), new ByFours(), + new ByFives()); + helper.doInteractive(userInput); + + System.out.println("Enter In The Number Of Times You Want To Count:\n"); + int userIterations = in.nextInt(); + + helper.displayCounter(userIterations); + in.close(); + + } +}