Question
java Please write junit test for the generateRandomValues(),canBet() and addMoney() that will include @Before annotation and methods: assertEquals, assertTrue, and assertFalse. Thank you The code:
java
Please write junit test for the generateRandomValues(),canBet() and addMoney() that will include @Before annotation and methods: assertEquals, assertTrue, and assertFalse.
Thank you
The code:
import java.util.*;
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;
interface IRandomValueGeneratorInterface {
// function defined in interface to generate random values in between l and r range
int generateRandomValues(int l, int r);
}
public class Main implements IRandomValueGeneratorInterface {
// data variable minB, declared inorder to store the minimum balance
double minB;
double currentBalance;
// constructor defined with two parameters, one minBalance and other instance of interface defined
Main(double minBalance) {
minB = minBalance;
}
// function defined in interface to generate random values in between l and r range
public int generateRandomValues(int l, int r) {
return ThreadLocalRandom.current().nextInt(l, r+1);
}
// funtion defined to get the input of current balance from user
double getCurrentBalance() {
Scanner scn = new Scanner(System.in);
System.out.print(" Enter Current Balance: ");
double x = scn.nextDouble();
currentBalance = x;
System.out.println("Current Balance is: " + currentBalance);
return x;
}
// function defined to check if user can bet with given amount
boolean canBet(double amnt) {
if(currentBalance - amnt >= minB) {
return true;
}
return false;
}
// function defined to add money to Balance
void addMoney(double amnt) {
// amount must be positve, then only add it to current balance
if(amnt >= 0) {
currentBalance+=amnt;
}
System.out.println("Current Balance is: " + currentBalance);
}
// function defined to bet on a number in range
void betOnANumber(double amnt, int min, int max, int selectedNumber) {
// checking if user can bet or not
if(canBet(amnt)) {
// generating random number in range
int randomNumber = generateRandomValues(min, max);
// checking if user win
if(randomNumber == selectedNumber) {
System.out.println(" You Won!");
int range = max - min + 1;
currentBalance += (range - 1) * amnt;
System.out.println("Current Balance is: " + currentBalance);
}
// else lose
else {
System.out.println(" You Lose");
currentBalance -= amnt;
System.out.println("Current Balance is: " + currentBalance);
}
}
else {
System.out.println("User can't Bet with this amount on number.");
}
}
// function defined to implement betting on probability
void betOnProbability(double amnt, double p) {
// checking if user can bet or not
if(canBet(amnt)) {
// we are taking here if probability is > 70% then win
if(p > 0.7) {
System.out.println(" You Won!");
currentBalance += (p - 1 - 1) * amnt;
System.out.println("Current Balance is: " + currentBalance);
}
// else lose
else {
System.out.println(" You Lose");
currentBalance -= amnt;
System.out.println("Current Balance is: " + currentBalance);
}
}
else {
System.out.println(" User can't Bet with this amount on probability.");
}
}
// Main method
public static void main(String[] args) {
// creating objects to implement the function defined
Main obj1 = new Main(-1050.6);
// getting the current balance from User
obj1.getCurrentBalance();
// adding money to balance
obj1.addMoney(5000.0);
// checking if he can bet with current balance for some amount
if(obj1.canBet(5000.0))
System.out.println(" yes you can bet");
else
System.out.println(" No you cannot bet");
// betting with amount
obj1.betOnANumber(5000, 1, 100, 36); // that user is betting on 34 in range [1,100]
// betting on probability
obj1.betOnProbability(5000, 0.8);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started