Program Specifications: You are to DESIGN and WRITE a new Aunt Vera goes to Vega
ID: 3735588 • Letter: P
Question
Program Specifications: You are to DESIGN and WRITE a new Aunt Vera goes to Vegas program. The new program has completely different rules and guidelines. This program will have multiple classes. The program will contain a SlotMachine, Player, Name, Date, TestClass, and possibly other classes. A SlotMachine will have storage locations for name of slot, current slot balance, number of jack pots that have been paid out, how much the jackpot pays, the number of regular wins, the regular win payout, plus any other fields you consider necessary The Player will have a Name, dob, and moneyBalance. The Name class will include first, middle initial, and last name. The Date class will include month, day, and year. You can have additional fields should you find it necessary The program will validate the date. The first name and last name and middle initial of a name will be in sentence case. It will cost a play 1 dollar to play any slot machine. You will create aunt Vera and allow the user to create up to another 50 players. Standard rules, no money, no playing. You will create some type of menu system. Besides creating players and machines, the program will allow slot play Slot Play:Explanation / Answer
CODE:
public class Exam3Main {
public static void main(String[] args) {
// Variables
SlotMachine[] machines = new SlotMachine[3];
Player[] players = new Player[50];
int currentPlayer = 0; // Set this initially to an invalid value to test when a player has been created
// Initialize Variables for use
players[0] = initializeVera();
int playerCount = 1;
initializeMachines(machines);
char menuChoice = '0';
do { // Start Main Loop
menuChoice = runMenu(players[currentPlayer].toString());
switch (menuChoice) {
case '1': // Create a New Player
if (playerCount < players.length) { // if room in players array
players[playerCount] = Player.createPlayer();
currentPlayer = playerCount; // sets current player to the newly created player
playerCount++;
}
else
JOptionPane.showInputDialog("Player Array Full");
break;
case '2': // Select a Player
currentPlayer = Player.selectPlayer(players, playerCount);
case '3': // Play a Machine
SlotMachine.playTheSlots(machines, players[currentPlayer]);
break;
case '4': // Statistics
SlotMachine.statisticsMenu(machines);
break;
case 'X': // Exit
break;
default:
break;
}
} while (menuChoice != 'X'); // End Main Loop
} // END public static void main(String[] args)
/* Method : public static void initializeMachines(SlotMachine[] machines)
* Data Required : An array of SlotMachines
* Data Returned : none
* Purpose : To initialize the SlotMachines used in this program
* Description : We initialize the reference variables with the initial conditions for
* our three slotmachines.
*/
public static void initializeMachines(SlotMachine[] machines) {
machines[0] = new SlotMachine("Lucky 777", 5000, 10000, 5000, 10, 5);
machines[1] = new SlotMachine("Lucky Lotto", 55000, 100000, 75000, 50, 25);
machines[2] = new SlotMachine("Purple People Eater", 1000, 50, 40, 5, 2);
} // END public static void initializeMachines(SlotMachine[] machines)
/* Method : private static Player initializeVera()
* Data Required : none
* Data Returned : A Player Variable
* Purpose : To initialize the first Player variable
* Description : The first Player is created.
*/
private static Player initializeVera() {
Name name = new Name("Vera", 'D', "Hunchuck", "Aunt");
Date dob = new Date(11, 5, 1955);
int balance = 100;
Player result = new Player(name, dob, balance);
return result;
} // END private static Player initializeVera()
/* Method : public static char runMenu()
* Data Required : none
* Data Returned : a character representing the user's menu choice
* Purpose : To output the menu to the user, and receive an input
* Description : see Purpose
*/
public static char runMenu() {
char result = '0';
String output = " Main Menu 1. Create New Player" + " 2. Select a Player"
+ " 3. Play a Machine" + " 4. View Stats" + " X. Exit Current Player NONE";
result = JOptionPane.showInputDialog(output, "Enter a Menu Selection").toUpperCase().charAt(0);
return result;
} // END public static char runMenu()
/* Method : public static char runMenu(String playerStats)
* Data Required : a String message
* Data Returned : a character representing the user's menu choice
* Purpose : To output the menu to the user, and receive an input
* Description : The menu displays, and the stats of the current player
* are displayed at the bottom.
*/
public static char runMenu(String playerStats) {
char result = '0';
String output = " Main Menu 1. Create New Player" + " 2. Select a Player"
+ " 3. Play a Machine" + " 4. View Stats" + " X. Exit * Current Player * " + playerStats;
result = JOptionPane.showInputDialog(output, "Enter a Menu Selection").toUpperCase().charAt(0);
return result;
} // END public static char runMenu(String playerStats)
} // END public class Exam3Main
Player.java
import javax.swing.JOptionPane;
/* Player Class
* This class will allow for the creation of Date objects.
*/
public class Player {
// Fields
private Name name;
private Date dob;
private int currentBalance;
// Behaviors
// No arg Constructor
public Player() {
}
// All field Constructor
public Player(Name name, Date dob, int currentBalance) {
setName(name);
setDob(dob);
setCurrentBalance(currentBalance);
}
// returns the Name of the Player
public Name getName() {
return name;
}
// sets the Name of the Player
public void setName(Name name) {
this.name = name;
}
// returns the dob of the Player
public Date getDob() {
return dob;
}
// sets the dob of the Player
public void setDob(Date dob) {
this.dob = dob;
}
// returns the currentBalance of the Player
public int getCurrentBalance() {
return currentBalance;
}
// sets the currentBalance of the Player
public void setCurrentBalance(int currentBalance) {
this.currentBalance = currentBalance;
}
/* Method : public static Player createPlayer()
* Data Required : none
* Data Returned : a type Player variable
* Purpose : To create, new player objects using the user's input
* Description : This method prompts the user to fill the fields of a Player
*/
public static Player createPlayer() {
Name name = Name.createName();
Date dob = Date.createDate("Enter Date of Birth");
int currentBalance = Integer
.parseInt(JOptionPane.showInputDialog("Enter Starting Casino Balance", "Enter a whole dollar amount"));
Player temp = new Player(name, dob, currentBalance);
JOptionPane.showMessageDialog(null, "New Player : " + temp.toString());
return temp;
} // END public static Player createPlayer()
/* Method : public static void statisticsMenu(SlotMachine[] slots)
* Data Required : 1) An array of type Player
* 2) The effective size of the array
* Data Returned : The index of the selected player
* Purpose : To output a numbered list of the players, and allow the user to
* select a player.
* Description :
*/
public static int selectPlayer(Player[] players, int playerCount) {
int result;
String output;
do {
output = "Players";
for (int i = 0; i < playerCount; i++)
output += " " + (i + 1) + ". " + players[i].name + " Balance: $" + players[i].currentBalance;
result = Integer.parseInt(JOptionPane.showInputDialog(output, "Enter a Number"));
} while (result > playerCount || result < 0);
result--; // Sets result to an index starting at 0 instead of 1
return result;
} // END public static int selectPlayer(Player[] players, int playerCount)
// toString() returns a String containing the relevant information a Player Contains
public String toString() {
return "Name : " + name + " Date of Birth : " + dob + " Balance $" + currentBalance;
}
} // END public class Player
Name.java
import javax.swing.JOptionPane;
/* Name Class
* This class will allow for the creation of Name objects.
*/
public class Name {
// Fields
private String firstName;
private char middleInitial;
private String lastName;
private String suffix = null;
// Behaviors
// No arg Constructor
public Name() {
}
// All field Constructor
public Name(String firstName, char middleInitial, String lastName, String suffix) {
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setSuffix(suffix);
}
// returns the firstName of the current Name
public String getFirstName() {
return firstName;
}
// sets the firstName of the current Name
public void setFirstName(String firstName) {
firstName.toUpperCase().charAt(0);
this.firstName = firstName;
}
// returns the middleInitial of the current Name
public char getMiddleInitial() {
return middleInitial;
}
// sets the middleInitial of the current Name
public void setMiddleInitial(char middleInitial) {
if (middleInitial != ' ')
while (!(Character.isAlphabetic(middleInitial))) {
middleInitial = JOptionPane.showInputDialog(
"Invalid Middle Initial Detected" + " Enter a Middle Initial", "Enter a Letter").charAt(0);
}
this.middleInitial = middleInitial;
}
// returns the lastName of the current Name
public String getLastName() {
return lastName;
}
// sets the lastName of the current Name
public void setLastName(String lastName) {
lastName.toUpperCase().charAt(0);
this.lastName = lastName;
}
// returns the suffix of the current Name
public String getSuffix() {
return suffix;
}
// sets the suffix of the current Name
public void setSuffix(String suffix) {
if (suffix.toUpperCase().equals("NONE"))
suffix = "NONE";
suffix.toUpperCase().charAt(0);
this.suffix = suffix;
}
/* Method : public static Name createName()
* Data Required : none
* Data Returned : a type Name variable
* Purpose : To create, new Name objects using the user's input
* Description : This method prompts the user to fill the fields of a Name
*/
public static Name createName() {
String firstName;
String middle;
char middleInitial;
String lastName;
String suffix = null;
firstName = JOptionPane.showInputDialog("Enter First Name");
middle = JOptionPane.showInputDialog("Enter Middle Initial");
if (!middle.isEmpty())
middleInitial = middle.charAt(0);
else
middleInitial = ' ';
lastName = JOptionPane.showInputDialog("Enter Last Name");
suffix = JOptionPane.showInputDialog("Enter Suffix, if N/A Enter NONE");
Name result = new Name(firstName, middleInitial, lastName, suffix);
return result;
} // public static Name createName()
// toSting() returns a string containing the relevant information of a Name
public String toString() {
String output = "";
if (!this.suffix.equals("NONE"))
output += this.suffix + ". ";
output += this.firstName + " ";
if (this.middleInitial != ' ')
output += this.middleInitial + ". ";
output += this.lastName;
return output;
}
} // END public class Name
Date.java
import java.time.LocalDate;
import javax.swing.JOptionPane;
/* Date Class
* This class will allow for the creation of Date objects.
*/
public class Date {
// Fields
private int month;
private int day;
private int year;
// Behaviors
// No arg Constructor
public Date() {
}
// The all date constructor
public Date(int month, int day, int year) {
setMonth(month);
setYear(year);
setDay(day);
}
// The pre-set year constructor
public Date(int month, int day) {
setMonth(month);
this.year = 2017;
setDay(day);
}
// returns the month of the current Date
public int getMonth() {
return month;
}
// sets the month of the current Date
public void setMonth(int month) {
String input;
while (month < 0 || month > 12) {
input = JOptionPane.showInputDialog(
"Invalid month Detected Recieved Value : " + month + " Enter a new month",
"Enter an Integer (1-12)");
month = Integer.parseInt(input);
}
this.month = month;
}
// returns the day of the current Date
public int getDay() {
return day;
}
// sets the day of the current Date
public void setDay(int day) {
String input;
int maxDay = 0;
if (this.month == 2) {
if (this.year % 4 == 0) // tests if leap year
maxDay = 29;
else
maxDay = 28;
}
else {
if (this.month < 8) { // for the first 7 months
if (this.month % 2 == 0) // even is 30 days
maxDay = 30;
else
maxDay = 31; // odd is 31 days
}
else { // rest of the months
if (this.month % 2 == 0)
maxDay = 31; // even is 31 days
else
maxDay = 30; // odd is 30 days
}
}
while (day < 1 || day > maxDay) {
input = JOptionPane.showInputDialog("Invalid Day Detected" + " Recieved Value : " + day + " Most Days in "
+ this.month + " : " + maxDay + " Enter a new day", "Enter an Integer (1-" + maxDay + ")");
day = Integer.parseInt(input);
}
this.day = day;
}
// returns the year of the current Date
public int getYear() {
return year;
}
// sets the year of the current Date
public void setYear(int year) {
String input;
while (year < LocalDate.now().getYear() - 150 || year > LocalDate.now().getYear()) {
input = JOptionPane.showInputDialog(
"Invalid year Detected Recieved Value : " + year + " Enter a new year", "Enter a 4 digit year");
month = Integer.parseInt(input);
}
this.year = year;
}
/* Method : public static Date createDate()
* Data Required : none
* Data Returned : a type Date variable
* Purpose : To create, new Date objects using the user's input
* Description : This method prompts the user to fill the fields of a Date
*/
public static Date createDate() {
int month = Integer.parseInt(JOptionPane.showInputDialog(" month/day/year " + "Enter the Month"));
int day = Integer.parseInt(JOptionPane.showInputDialog(" " + month + "/day/year " + "Enter the Day"));
int year = Integer
.parseInt(JOptionPane.showInputDialog(" " + month + "/" + day + "/year " + "Enter the Year"));
Date temp = new Date(month, day, year);
return temp;
} // END public static Date createDate()
/* Method : public static Date createDate(String messege)
* Data Required : a String messege
* Data Returned : a type Date variable
* Purpose : To create, new Date objects using the user's input
* Description : This method outputs a messege to the user then prompts the user to fill the fields of a Date
*/
public static Date createDate(String messege) {
int month = Integer.parseInt(JOptionPane.showInputDialog(messege + " month/day/year " + "Enter the Month"));
int day = Integer
.parseInt(JOptionPane.showInputDialog(messege + " " + month + "/day/year " + "Enter the Day"));
int year = Integer.parseInt(
JOptionPane.showInputDialog(messege + " " + month + "/" + day + "/year " + "Enter the Year"));
Date temp = new Date(month, day, year);
return temp;
} // END public static Date createDate(String messege)
// toString() returns the relevant data of the Date type
public String toString() {
return month + "/" + day + "/" + year;
}
} // END public class Date
SlotMachine.java
import java.time.LocalDate;
import javax.swing.JOptionPane;
/* Class SlotMachine
* This class will allow the user to create SlotMachine objects which will allow the user
* to simulate gambling.
*/
public class SlotMachine {
// Class Variables
// These first variables keep track of statistics for when any instance variable is used.
private static int totalBalance = 0;
private static int totalPayout = 0;
private static int allPlays = 0;
// Fields
private String machineName;
private int machineBalance;
private int numJackpotsPaid;
private int jackpotOdds;
private int jackpotPayAmount;
private int numRegularWinsPaid;
private int regularWinOdds;
private int regularPayout;
private int totalPlays;
// Behaviors
// No-Arg Constructor
public SlotMachine() {
}
// The go-to constructor for this class. It creates SlotMachine's based off passed values
public SlotMachine(String machineName, int machineBalance, int jackpotOdds, int jackpotPayAmount,
int regularWinOdds, int regularPayout) {
setMachineName(machineName);
setMachineBalance(machineBalance);
this.numJackpotsPaid = 0; // set to 0 this is a counter
setJackpotOdds(jackpotOdds);
setJackpotPayAmount(jackpotPayAmount);
this.numRegularWinsPaid = 0; // set to 0 this is a counter
setRegularWinOdds(regularWinOdds);
setRegularPayout(regularPayout);
this.totalPlays = 0; // set to 0 this is a counter
SlotMachine.totalBalance += this.machineBalance;
}
// get private static int allplays
public static int getAllPlays() {
return allPlays;
}
// get private static int totalBalance
public static int getTotalBalance() {
return totalBalance;
}
// get private static int totalPayout
public static int getTotalPayout() {
return totalPayout;
}
// get this.machineName
public String getMachineName() {
return machineName;
}
// set this.machineName
public void setMachineName(String machineName) {
this.machineName = machineName;
}
// get this.machineBalance
public int getMachineBalance() {
return machineBalance;
}
// set this.machineBalance
public void setMachineBalance(int machineBalance) {
while (machineBalance < 0) {
machineBalance = Integer.parseInt(JOptionPane
.showInputDialog("INVALID BALANCE DETECTED Enter a new balance", "Enter the machine's balance"));
}
this.machineBalance = machineBalance;
}
// get this.jackpotOdds
public int getJackpotOdds() {
return jackpotOdds;
}
// set this .jackPotOdds
public void setJackpotOdds(int jackpotOdds) {
this.jackpotOdds = jackpotOdds;
}
// get this.jackpotPayAmount
public int getJackpotPayAmount() {
return jackpotPayAmount;
}
// set this.jackpotPayAmount
public void setJackpotPayAmount(int jackpotPayAmount) {
this.jackpotPayAmount = jackpotPayAmount;
}
// get this.regularWinOdds
public int getRegularWinOdds() {
return regularWinOdds;
}
// set this.regularWinOdds
public void setRegularWinOdds(int regularWinOdds) {
this.regularWinOdds = regularWinOdds;
}
// get this.regularWinPayout
public int getRegularPayout() {
return regularPayout;
}
// set this.regularPayout
public void setRegularPayout(int regularPayout) {
this.regularPayout = regularPayout;
}
/* Method : public static String getAllMachineStats(SlotMachine[] slots)
* Data Required : An array of type SlotMachine
* Data Returned : a String containing the stats across all machines
* Purpose : This method returns a String containing the statistics of the
* slot machines.
* Description :
*/
public static String getCasinoStats(SlotMachine[] slots) {
String result = " Total Stats" + " Total Balance : $" + SlotMachine.getTotalBalance();
if (SlotMachine.getTotalPayout() > 0)
result += " The Casino has lost $" + SlotMachine.getTotalPayout();
else
result += " The Casino has made $" + (SlotMachine.getTotalPayout() * -1);
result += " Machines have been played " + SlotMachine.getAllPlays() + " times";
return result;
} // END public static String getCasinoStats(SlotMachine[] slots)
/* Method : public static String getAllMachineStats(SlotMachine[] slots)
* Data Required : An array of type SlotMachine
* Data Returned : A string containing the machine stats
* Purpose : This method returns a String containing the statistics of the
* slot machines.
* Description :
*/
public static String getAllMachineStats(SlotMachine[] slots) {
String result = "";
for (int i = 0; i < slots.length; i++)
result += slots[i].toString() + " ";
return result;
} // END public static String getAllMachineStats(SlotMachine[] slots)
public void playSlotMachine(Player player) {
int playerBalance = player.getCurrentBalance(); // here the users wallet is stored within the scope of the method.
int payout = 0;
String output = player.getName() + " is playing " + this.machineName + " " + "Starting Balance : $"
+ playerBalance + " ";
if (this.machineBalance > this.regularPayout) {
// This next section changes statistics relevant to each play
playerBalance--;
this.machineBalance++;
SlotMachine.totalBalance++;
SlotMachine.totalPayout--;
this.totalPlays++;
SlotMachine.allPlays++;
int slotRoll = (int) (Math.random() * this.jackpotOdds) + 1; // random number between 1 and jackPotOdds is generated
if (slotRoll == this.jackpotOdds) { // jackpot win
payout = this.jackpotPayAmount + 1;
this.numJackpotsPaid++;
output += " ******************* JACKPOT WON! ******************* ";
}
else if (slotRoll % this.regularWinOdds == 0) { // regular sin
payout = this.regularPayout + 1;
this.numRegularWinsPaid++;
output += " ************** You WON! ************** ";
}
else {
output += " You Lost Try again! "; // loss
payout = 0;
}
if (payout > this.machineBalance) { // If the machine has less money than the payout the machine pays its balance
output += " SORRY, The machine only has $" + this.machineBalance + " Winnings will be "
+ this.machineBalance + " ";
payout = this.machineBalance;
}
playerBalance += payout; // This will be positive when they win and 0 when they lose
player.setCurrentBalance(playerBalance); // This updates the balance with the balance after
// using the slotMachine
output += " Current Balance : $" + player.getCurrentBalance();
// The next two lines adjust the SlotMachines balances.
this.machineBalance -= payout;
SlotMachine.totalBalance -= payout;
JOptionPane.showMessageDialog(null, output); // output the results of the play to the user.
}
} // END public void playSlotMachine(Player player)
public static void playTheSlots(SlotMachine[] slots, Player currentPlayer) {
String output;
int slotNum;
boolean continueSlots = true;
do { // Start Loop
continueSlots = validPlayer(currentPlayer);
if (continueSlots) { // if the Player has money
output = " Slot Machines";
for (int i = 0; i < slots.length; i++)
output += " " + (i + 1) + ". " + slots[i].getMachineName();
output += " 0. Leave Slots";
slotNum = Integer.parseInt(JOptionPane.showInputDialog(output, "Choose a Slot to play"));
if (slotNum > 0 && slotNum <= slots.length)
slots[slotNum - 1].playSlotMachine(currentPlayer);
else
continueSlots = false;
}
} while (continueSlots); // While the player keeps playing slots
} // END public static void playTheSlots(SlotMachine[] slots, Player currentPlayer)
/* Method : public static void statisticsMenu(SlotMachine[] slots)
* Data Required : an array of type SlotMachine
* Data Returned : none
* Purpose : to allow the user to view statistics about the SlotMachines
* Description : output menu choices to the user, then take input of which option they wish to view
*/
public static void statisticsMenu(SlotMachine[] slots) {
int userChoice;
String output = " Statistics Menu";
for (int i = 0; i < slots.length; i++)
output += " " + (i + 1) + ". " + slots[i].getMachineName();
output += " 4. View all Machines" + " 5. View Total Stats" + " X. Exit";
userChoice = Integer.parseInt(JOptionPane.showInputDialog(output));
if (userChoice > 0 && userChoice <= slots.length)
JOptionPane.showMessageDialog(null, slots[userChoice - 1].toString());
else if (userChoice == slots.length + 1)
JOptionPane.showMessageDialog(null, SlotMachine.getAllMachineStats(slots));
else if (userChoice == slots.length + 2)
JOptionPane.showMessageDialog(null, SlotMachine.getCasinoStats(slots));
} // END public static void statisticsMenu(SlotMachine[] slots)
public static boolean validPlayer(Player person) {
boolean result = true;
int yearToGamble = LocalDate.now().getYear() - 18;
if (person.getCurrentBalance() < 1) { // test for zero/negative balance
result = false;
JOptionPane.showMessageDialog(null, "Invalid Funds Balance : $" + person.getCurrentBalance());
}
if (person.getDob().getYear() > yearToGamble) { // test if the user is old enough to gamble
result = false;
JOptionPane.showMessageDialog(null, "This player is too young to gamble.");
}
return result;
} // END public static boolean validPlayer(Player person)
// toString() returns a string of relevant information for the user
public String toString() {
return " SlotMachine Name : " + machineName + " Balance $" + machineBalance + " Jackpots Paid : "
+ numJackpotsPaid + " Jackpot Amount $" + jackpotPayAmount + " Regular Wins : " + numRegularWinsPaid
+ " Regular Payout $" + regularPayout + " This machine has been played " + totalPlays + " times.";
} // END public String toString()
} // END public class SlotMachine
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.