Michael Mansour Michael Mansour - 2 months ago 6
Java Question

update a variable in a different class

How do I make

amount()
in class
Casino.java
store the total value and allow it to be returned to the class
Roulette.java
.

When I use:

int amount = Casino.amount();


It gives me several hundred lines of errors.

What I want done is to run the game
number()
and store the value into
Casino.amount()


Please note in class
Roulette.java
there is a function called
amountUpdate()
which should update
Casino.amount()
.

This is class
Casino.java


package Casino;

import java.util.*;

public class Casino
{
static String player = "";
static int playAmount = 0;
public static void main(String[] args)
{

System.out.println("Welcome to Michael & Erics Casino!");
player();
ask();
start();

}

public static String player()
{
if (player.equals(""))
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your name : ");
player = sc.nextLine();
}
return player;
}

public static void ask()
{
Scanner sc = new Scanner(System.in);

System.out.println("How much would you like to play with? ");
playAmount = sc.nextInt();
if (playAmount < 1)
{
System.out.println("Please enter a value that is more than $1");
playAmount = 0;
}
System.out.println("You are playing with: $" + playAmount + "\n");

}

public static int amount()
{
int amount = playAmount;
if (Roulette.amountUpdate() >= 1)
amount += Roulette.amountUpdate();
return amount;
/*if (Blackjack.amountUpdate() >= 1)
amount += Blackjack.amountUpdate();
return amount;*/
}

public static void start()
{
System.out.println("Which table would you like to play at?");
System.out.println("1: Blackjack");
System.out.println("2: Roulette");
System.out.println("3: Check Balance");
System.out.println("4: Exit");
Scanner sc = new Scanner(System.in);
int table = sc.nextInt();


switch (table)
{
case 1:
//blackjack.main(new String[]{});
break;
case 2:
Roulette.main(new String[]{});
break;
case 3:
System.out.println("You have : $" + playAmount);
start();
break;
case 4:
System.exit(0);
break;
}
}

}


This is class
Roulette.java


package Casino;

import java.util.*;
import java.lang.*;


public class Roulette
{

public static void main(String[] args)
{
System.out.println("Welcome to the roulette table " + Casino.player());
placeBet();
amountUpdate();
//number();
}

public static int amountUpdate()
{
int amount = 0;
if (number() > 0)
amount += number();
if (br() > 0)
amount += br();
if (oe() > 0)
amount += oe();
if (third() > 0)
amount += third();
return amount;
}


public static void placeBet()
{
Scanner sc_Choice = new Scanner(System.in);


System.out.println("What would you like to bet on?");
System.out.println("1: Numbers");
System.out.println("2: Black or Red");
System.out.println("3: Odd or Even");
System.out.println("4: One Third");
System.out.println("5: Count Chips");
System.out.println("6: Restart");

int choice = sc_Choice.nextInt();

if (choice > 0 && choice < 7)
{
switch (choice)
{
case 1:
number();
break;
case 2:
br();
break;
case 3:
oe();
break;
case 4:
third();
break;
case 5:
System.out.println(Casino.amount());
break;
case 6:
Casino.main(new String[]{});
break;
}
}
else
System.out.println("You must choose between 1 and 6");
}

public static int number()
{
Boolean betting = true;
//int amount = Casino.amount();
int amount = 5000;
int number;
int winnings;
String reply;
int betX;
int betAgain = 1;
Scanner sc_Number = new Scanner(System.in);

ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bet = new ArrayList<Integer>();

while (betAgain == 1)
{
System.out.println("What number would you like to place a bet on?");
int listCheck = sc_Number.nextInt();

if (listCheck >= 0 && listCheck <= 36)
list.add(listCheck);
else
System.out.println("You must choose a number between 0 and 36");

System.out.println("How much do you want to bet?");
betX = sc_Number.nextInt();

if (betX > amount)
{
System.out.println("You have insufficient funds to make that bet");
number();
}
else if (betX < 1)
System.out.println("You must bet more than 1$");

else
{
bet.add(betX);
amount = amount - betX;
}

System.out.println("Do you want to bet on more numbers?");
reply = sc_Number.next();

if (reply.matches("no|No|false|nope"))
{
betAgain = betAgain - 1;
//No - Don't bet again
}
else
{
betAgain = 1;
//Yes - Bet again
}

int result = wheel();
System.out.println("Spinning! .... The number is: " + result);

for (int i = 0 ; i < bet.size() ; i++)
{
if (list.get(i) == result)
{
winnings = bet.get(i) * 35;

System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);
betAgain = betAgain - 1;

}
else
{
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
betAgain = betAgain - 1;
}
}
betAgain = betAgain - 1;
}
return amount;
}



public static int wheel()
{
Random rnd = new Random();

int number = rnd.nextInt(37);
return number;
}

//NOT WORKING - AFTER MAKING A BET IT RUNS Number()
public static int br()
{
Scanner sc_br = new Scanner(System.in);
int amount = Casino.amount();
int winnings;
System.out.println("Would you like to bet on Black or Red?");
String replyBR = sc_br.nextLine();
boolean black;
//****************
if (replyBR.matches("black|Black"))
black = true;
else
black = false;

System.out.println("How much would you like to bet?");
int betBR = sc_br.nextInt();
if (betBR > amount)
{
System.out.println("You have insufficient funds to make that bet");
br();
}
else if (betBR < 1)
{
System.out.println("You must bet more than 1$");
br();
}
else
amount = amount - betBR;
//*****************
boolean resultColour = colour();
if (resultColour == black)
{
winnings = betBR * 2;
//PRINT OUT WHAT COLOUR!!!
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);

}
else
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
return amount;
}

public static boolean colour()
{
Random rnd = new Random();

boolean colour = rnd.nextBoolean();
return colour;
}
//NOT WORKING - AFTER MAKING A BET IT RUNS Number()
public static int oe()
{
Scanner sc_oe = new Scanner(System.in);
int amount = Casino.amount();
int winnings;
System.out.println("Would you like to bet on Odd or Even?");
String replyOE = sc_oe.next();
System.out.println("How much would you like to bet?");
int betOE = sc_oe.nextInt();
if (betOE > amount)
{
System.out.println("You have insufficient funds to make that bet");
oe();
}
amount = amount - betOE;

boolean resultOE = oddOrEven();
//PRINT OUT IF IT WAS ODD OR EVEN
if (resultOE == true)
{
winnings = betOE * 2;
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);

}
else
{
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
}
return amount;
}

public static boolean oddOrEven()
{
Random rnd = new Random();

boolean num = rnd.nextBoolean();
return num;
}
//NOT WORKING - AFTER MAKING A BET IT RUNS Number()
public static int third()
{
Scanner sc_Third = new Scanner(System.in);
int amount = Casino.amount();
int winnings;
System.out.println("Would you like to bet on 1st, 2nd or 3rd third?");
String replyT = sc_Third.next();
System.out.println("How much would you like to bet?");
int betT = sc_Third.nextInt();
if (betT > amount)
{
System.out.println("You have insufficient funds to make that bet");
third();
}
amount = amount - betT;

boolean resultT = thirdResult();
//PRINT OUT WHAT NUMBER IT WAS AND IF IT WAS IN WHICH THIRD
if (resultT == true)
{
winnings = betT * 3;
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);

}
else
{
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
}
return amount;
}

public static boolean thirdResult()
{
Random rnd = new Random();

int num = rnd.nextInt(2);
if (num == 0)
return true;
else
return false;
}

}

Answer

Looks like you're probably running into a StackOverflowException. When you call Casino.amount() inside of Roulette.number(), it then calls Roulette.amountUpdate(), which then calls Roulette.number(). Your methods are stuck in an infinite loop like this. You'll need to redesign your code such that these 3 functions are not all dependent on each other.

Your code is terse, so it's hard to help you fully solve the problem, but I believe you would benefit from splitting up your "amount" variable into separate entities. Keep things like bet amount, winnings, and such separate until you need to combine them.

Another issue you may run into is thatRoulette.amountUpdate() is called twice in Casino.amount(), but Roulette.amountUpdate() will not necessarily return the same thing both times. Consider storing the return value from the first call instead of calling it twice.