Moving Directory

master
sgoudham 4 years ago
parent ae0b52e58d
commit 5079003264
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -0,0 +1,7 @@
package src;
public interface Counter {
int countBy();
int getNextNum();
}

@ -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());
}
}
}

@ -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();
}
}
Loading…
Cancel
Save