Skip to content

Commit

Permalink
Singleton Pattern - Ball Selector
Browse files Browse the repository at this point in the history
Creates a Ball class as a singleton to ensure that there is only one instance of each of the different colors.
  • Loading branch information
diegogarciacs authored Jan 2, 2022
1 parent 1f22946 commit 22fc87f
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 0 deletions.
Binary file added Lab 11/CECS277-Lab11.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions Lab 11/Lab 11.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added Lab 11/out/production/Lab 11/Ball.class
Binary file not shown.
Binary file added Lab 11/out/production/Lab 11/CheckInput.class
Binary file not shown.
Binary file added Lab 11/out/production/Lab 11/Main.class
Binary file not shown.
58 changes: 58 additions & 0 deletions Lab 11/src/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Ball class modification of a Singleton object
*/

import java.util.*;

public class Ball{
private static HashMap<String, Ball> colors = new HashMap<String, Ball>();
private String color;
private int bounces;
private int rolls;

/* Ball constructor
*/
public Ball(String c){
this.color = c;
this.bounces = 0;
this.rolls = 0;
colors.put(c, this);
}
/* Accceses the hashmap and checks to see if the object already exists, otherwise constructs the object and returns it to be inserted.
*
* @param c the key of the object to be checked.
* @return the object which to be modified.
*/
public static Ball getInstance(String c) {

if (colors.containsKey(c)) {
return colors.get(c);
}
else{
return new Ball(c);
}
}

/* Increment the bounce count of a ball
*
*/
public void bounce(){
this.bounces += 1;
}

/* Increment the roll count of a ball
*/
public void roll(){
this.rolls += 1;
}

/* String representation of a ball object
* @return returns a string detailing the current status of the users ball
*/
@Override
public String toString(){
return String.format("%s Ball:\n--------\nRolls: %d\nBounces: %d\n",
this.color,
this.rolls,
this.bounces);
}
}
133 changes: 133 additions & 0 deletions Lab 11/src/CheckInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import java.util.Scanner;

/**
* Static functions used to check console input for validity.
*
* Use: Place CheckInput class in the same project folder as your code.
* Call CheckInput functions from your code using "CheckInput."
*
* Example: int num = CheckInput.getInt();
*
* @author Shannon Cleary 2020
*/
public class CheckInput {

/**
* Checks if the inputted value is an integer.
* @return the valid input.
*/
public static int getInt() {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is an integer and
* within the specified range (ex: 1-10)
* @param low lower bound of the range.
* @param high upper bound of the range.
* @return the valid input.
*/
public static int getIntRange( int low, int high ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input <= high && input >= low ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is a non-negative integer.
* @return the valid input.
*/
public static int getPositiveInt( ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input >= 0 ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is a double.
* @return the valid input.
*/
public static double getDouble() {
Scanner in = new Scanner( System.in );
double input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextDouble() ) {
input = in.nextDouble();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Takes in a string from the user.
* @return the inputted String.
*/
public static String getString() {
Scanner in = new Scanner( System.in );
String input = in.nextLine();
return input;
}

/**
* Takes in a yes/no from the user.
* @return true if yes, false if no.
*/
public static boolean getYesNo(){
boolean valid = false;
while( !valid ) {
String s = getString();
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) {
return true;
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) {
return false;
} else {
System.out.println( "Invalid Input." );
}
}
return false;
}
}
40 changes: 40 additions & 0 deletions Lab 11/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Alfredo Sequeida
* Diego Garcia
* 11/9/2021
* Group 5
* Lab 11
* This program takes user input and asks the user what they want to do with a ball.
* It then keep track of that action using a singleton-like Ball object.
*/
class Main {
public static void main(String[] args) {

boolean keepAsking = true;

while (keepAsking){
System.out.println("Choose a ball color. Type it in string format. (q to quit):");
String color = CheckInput.getString();
if (color.equals("q")){
keepAsking = false;
break;
}
System.out.println("1. Roll ball\n2. Bounce ball");
int option = CheckInput.getIntRange(1,2);

Ball ball = Ball.getInstance(color);

switch(option){
case (1):
ball.roll();
break;
case (2):
ball.bounce();
break;
}

System.out.println(ball);
}
}
}

0 comments on commit 22fc87f

Please sign in to comment.