Problem Description
Helen likes to have the occasional flutter (bet) on the horses. She likes to be able to see
what her winnings would be for various scenarios. You are to design and develop a bet
winnings calculation program for her.
The program should ask the user for the following information:
• The total stake (the amount bet)
• The name of the horse
• Whether the bet is for an outright win or it is an each-way bet
• The betting odds in this way:
o Enter the betting odds (X/Y)…
Enter X > 11
Enter Y > 10
o Odds are 11/10.
• The number of runners in the race
• The finishing position of the horse
There are some rules to consider when calculating the winnings:
• If there are 7 horses or less in the race, then only the first 2 places are considered for
each-way bets
• If there are between 8 and 20 horses in the race, then the first 3 places are
considered for each-way bets
• If there are more than 20, then the first 4 places are considered for each-way bets
Winnings are calculated as follows:
• Outright win bets: the winnings are the original stake + the odds multiplied by the
original stake
o Example: 5 euro win bet placed on a horse at 11/10
o = €5 + ( €5 * 11/10 ) = €10.50
• Each-way bets: Half of the stake is a standard outright win bet and is calculated as
above, e.g. a €5 each-way bet involves 2 x €5. The other half is applied in the
following way:
o If there are 2 places considered (see rules above), then half the odds are used
if the horse finishes first or second
o If there are 3 places considered, then a third of the odds are used if the horse
finishes first, second or third
o If there are 4 places considered, then a quarter of the odds are used if the
horse finishes first, second, third or fourth
o If the horses places, but does not win, then half the original stake is returned
along with the winnings
o If the horse wins, then the entire stake (i.e. both the win and place portions)
are returned along with the winnings
Additional Notes
• You must format your output, e.g. winnings of zero as €0.00 or 5.3 as €5.30.
• The horse’s name must be output in all capitals.
• You must display 1st, 2nd, 3rd, 4th, etc. 10 would be 10th, 20 would be 20th, etc.
• You should include basic validation for all input – see the last example for checking
the finishing position; should also check that bet type is ‘E’ or ‘W’; should ensure that
the finishing position is not greater than the number of horses in the race.
Everything I have so far:
import java.util.Scanner; public class part1 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double stake; String horsesName; double outrightWinOrEachWay; double outrightWin; double eachWay; double OddsX; double OddsY; double totalOdds; double eachWayOdds; double runners; double finishingPosition; double winnings; double win; double place; System.out.println("Please enter your stake:"); while (! keyboard.hasNextDouble() ) { keyboard.nextLine(); System.out.print("Please try again "); } stake = keyboard.nextFloat(); horsesName = keyboard.nextLine(); while ( horsesName.isEmpty() ) { System.out.print("What is your horses name?"); horsesName = keyboard.nextLine(); } System.out.println("Enter the betting odds (x/y)"); System.out.println("X="); while (! keyboard.hasNextDouble() ) { keyboard.nextLine(); System.out.print("Please try again "); } OddsX = keyboard.nextFloat(); System.out.println("Y="); while (! keyboard.hasNextDouble() ) { keyboard.nextLine(); System.out.print("Please try again "); } OddsY = keyboard.nextFloat(); totalOdds = (OddsX/OddsY); System.out.println("How many runners are in the race?"); while (! keyboard.hasNextDouble() ) { keyboard.nextLine(); System.out.print("Please try again "); } runners = keyboard.nextFloat(); System.out.println("What is your finishing position?"); while (! keyboard.hasNextDouble() ) { keyboard.nextLine(); System.out.print("Please try again "); } finishingPosition = keyboard.nextFloat(); System.out.println("Is your bet a:"); System.out.println("(1) Outright Win"); System.out.println("(2) Eachway"); while (! keyboard.hasNextDouble() ) { keyboard.nextLine(); System.out.print("Please try again "); } outrightWinOrEachWay = keyboard.nextFloat(); if (outrightWinOrEachWay == 1) { if (finishingPosition == 1) { winnings = stake + (totalOdds * stake); System.out.println(horsesName + " finished 1st out of " + runners + " runners"); System.out.println("You have won \u20ac" + winnings); } if (finishingPosition > 1) { System.out.println("Sorry you didn't win"); } } if (outrightWinOrEachWay == 2) { if (runners < 7) { eachWayOdds = (totalOdds/2); if (finishingPosition == 1) { win = stake + (totalOdds * stake); place = stake + (eachWayOdds * stake); winnings = win + place; System.out.println(horsesName + " finished 1st out of " + runners + " runners"); System.out.println("You have won \u20ac" + winnings); } if (finishingPosition ==2) { winnings = stake + (eachWayOdds * stake); System.out.println(horsesName + " finished 1st out of " + runners + " runners"); System.out.println("You have won \u20ac" + winnings); } if (finishingPosition> 2){ System.out.println("Sorry you didn't win"); } if (runners > 8) { } } } } }