HELP PLEASE READ ALL INFO BEFORE RESPONDING TO QUESTION NEED
HELP! PLEASE READ ALL INFO BEFORE RESPONDING TO QUESTION. NEED FULL COMPLETE ANSWER. NEEDED IN BASIC BEGINNER JAVA AND BASIC FILE WRITE.
A slot machine is a gambling device that the user inserts money into then pulls a lever (or presses a button). The slot machine then displays a set of random images. If two or more of the images match, the user wins an amount of money that the slot machine dispenses back to the user.
Create a program that simulates a slot machine. The program should contain at least 2 methods (in addition to the main method) – one void method and one value-returning method – at least one of the methods should include arguments.
When the program runs, it should do the following: Ask the user to enter the amount of money he or she wants to enter into the slot machine. You should validate this input to make sure the user enters a positive value for the bid amount.
Instead of displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars. To select a word, the program will generate a random number in the range of 0 through 5. If the number is 0, the selected word is Cherries; if the number is 1, the selected word is Oranges; and so forth. The program should randomly select a word from this list three times and display all three of the words. If none of the randomly selected words match, the program will inform the user that he or she has won $0. If two of the words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered.
The program will ask whether or not the user wants to play again. If so, these steps are repeated. If not, the program displays the total amount of money entered into the slot machine for this session, the total amount won this session, and the net loss/gain.
In addition, the program will keep a record of the highest gain ever achieved while playing the game. The value will be written to a file between game sessions, so that it can be read from the file with each new session.
If the user exceeds the record gain, a message indicating this is a record gain should be displayed, the value of the previous record gain should be displayed, and the new record gain should be stored. (Hint: If the file doesn’t exist, no record has been set yet.)
Please remember to use good programming practices – naming conventions, indentation, comments, etc. All currency output should be formatted appropriately. Please attach the .java file for you program to your assignment submission in Blackboard.
Sample Program Output:
----jGRASP exec: java SlotMachine
Step Right Up and Try Your Luck at Kristin\'s Amazing Slot Machine!
What is the amount of your bet this round? $1
Oranges Cherries Bars Better luck next time!
You won $0.00.
Would you like to play again? (Y/N): y
What is the amount of your bet this round? $2
Cherries Bells Oranges Better luck next time! You won $0.00.
Would you like to play again? (Y/N): y
What is the amount of your bet this round? $3 Plums Oranges Melons Better luck next time! You won $0.00.
Would you like to play again? (Y/N): n
Total Bets: $6.00
Total Winnings: $0.00
That\'s a Net Loss of $6.00
----jGRASP: operation complete.
Solution
//Program is doning everything said in question except storing session of game in file.To check high score
import java.util.*;
import java.text.*;
public class Gambling {
//to read input(bet amount) from user and validate it is positive
public int getInput(){
int a;
Scanner scan = new Scanner(System.in);
System.out.println(\"Step Right Up and Try Your Luck at Kristin\'s Amazing Slot Machine!\");
System.out.println(\"What is the amount of your bet this round?\");
a= scan.nextInt();
while (a<0){ //Checks if input is Integer
System.out.println(\"enter positive integer\");
a= scan.nextInt();
}
//Assigns entered integer to a
return a; //replace with correct code
}
//this function will apply gambling logic and wil give the result of any instance of game played
public int Gamble( int betAmount){
StringBuilder sb = new StringBuilder(); // this object will be used to store intermediate result of playing the game. W ill use it to form final response of this function.
Random random = new Random(); // crreting onject of Random generator
int[] a = {0, 0, 0, 0, 0, 0}; // i will use this array to check.WHICH IMAGE(number) is occuring how many times.
for (int i = 0; i < 3; i++) { // will generate random number 3 times and loop for each
int num = random.nextInt(5); // will generate random number between 0 and 5
System.out.println(num);
if (num ==0){ //we will have condition for each numberr generated and will start forming our resultant string .Will append result coresponding to ech number in string.
a[0]= a[0] + 1; // for example number geneated is 0. We will increment a[0] to 1. If next time also comes in this if ,will increment to 2,maximum it can be 3
sb.append(\"Cherries \");
}
else if(num ==1){
a[1]= a[1] + 1;
sb.append(\"Oranges \");
}
else if(num ==2){
a[2]= a[2] + 1;
sb.append(\"Plums \");
}
else if(num ==3){
a[3]= a[3] + 1;
sb.append(\"Bells \");
}
else if(num ==4){
a[4]= a[4] + 1;
sb.append(\"Melons \");
}
else if(num ==5) {
a[5]= a[5] + 1;
sb.append(\"Bars \");
}
}
String result=\"\";
for (int k = 0; k < 5; k++) { //we will check which image(number)occured how many times. And form result string as per that.
if(a[k]==2)
{
betAmount=2*betAmount;
result= \"You won \" + \"$\" + betAmount;
break;
}
else if(a[k]==3)
{
betAmount=3*betAmount;
result= \"You won \" + \"$\" + betAmount;
break;
}
else{
result= \"Better luck next time!\";
}
}
sb.append(result); // final response of this function
String output = sb.toString();
System.out.println(output);
return betAmount; // amount won in this particular instance of game
}
public static void main(String[] args)
{
Gambling a = new Gambling(); //class object declared to call the functions
int k;
Scanner sc = new Scanner(System.in); //use to get user input
char choice=\'y\';
int Bets=0;
int Winnings=0;
while(choice==\'y\'){ //loop to input from user .if he want to play
k= a.getInput();
Bets= Bets + k; //each time user play we store total bet amount
Winnings= Winnings + a.Gamble(k); // each time user plays we store the won amount
System.out.println(\"do you want to play more:y/n\");
choice=sc.next().charAt(0); //input user choice for next game
}
System.out.println(\"Total Bets: \" + \"$\" +Bets);
System.out.println(\"Total Winnings: \" + \"$\" +Winnings);
if((Winnings- Bets)>0){
System.out.println(\"That\'s a Net profit of \" + (Winnings- Bets));
}
else{
System.out.println(\"That\'s a Net loss of \" + \"$\" + (Winnings- Bets));
}
}
}



