/* Punters Lounge JAVA programming course * * SysCalc.java * * Author : Erik Datapunter * Date : 12 july 2004 * * Bet system calculation program, * Calculates actual betting result as well as the theoretical result from level stake betting * */ // import the library holding all the Input/Output objects import java.io.*; // our class definition class Lesson85 { // variables defined as public for the whole class static String WinLose = ""; // variable to hold Lose or Win static float Odds = 0; // variable to hold the Odds static float Stake = 0; // variable to hold the Stake // main method, the words "throws Exception" are required when using Input/Output objects // they are part of the error handling system, we will talk about that later public static void main(String[ ] arguments) throws Exception { // this bracket is the start of the main method inside of our program SysCalc // Defining variables for the main method int Winners = 0; // number of winning bets int Losers = 0; // number of losing bets float TotOdds = 0; // total of all odds float AvrOdds = 0; // Average of odds float LsBank = 100; // level stakes bank float LsStaked = 0; // total staked at level stakes float LsReturned = 0; // total returned at level stakes int LsBankrupt = 0; // level stakes Bankruptcy after howmany bets float LsProfit = 0; // level stakes profit on a bet float MyBank = 100; // total bank at actual stakes float MyStaked = 0; // total staked at actual stakes float MyReturned = 0; // total returned at actual stakes int MyBankrupt = 0; // actual stakes Bankruptcy after howmany bets float MyProfit = 0; float FpBank = 100; // total bank at fixed profit stakes float FpStaked = 0; // total staked at fixed profit stakes float FpReturned = 0; // total returned at fixed profit stakes int FpBankrupt = 0; // fixed profit stakes Bankruptcy after howmany bets float FpProfit = 0; float PbBank = 100; // total bank at percentile bank stakes float PbStaked = 0; // total staked at percentile bank stakes float PbReturned = 0; // total returned at percentile bank stakes int PbBankrupt = 0; // percentile bank stakes Bankruptcy after howmany bets float PbProfit = 0; float Commission = 0.95F; // 5% comission to be paid on all winnings // note : 0.95 will be seen as a DOUBLE not a FLOAT by adding an F we // tell the compiler that this is a FLOAT // The file objects BufferedReader BetRecord = new BufferedReader(new FileReader(new File("Placemarket.txt"))); BufferedWriter SysResult = new BufferedWriter(new FileWriter(new File("PMresult.csv"))); /********************************************************************************************** * Main loop reading and processing each line of the betting record. * */ String aBet = ""; // to hold a single line from the input file while ( ( (aBet = BetRecord.readLine()) != null ) & ( (LsBankrupt == 0) | (MyBankrupt == 0) | (FpBankrupt == 0) | (PbBankrupt == 0) ) ) { // while (there is a next line to read) AND ((levelstakes is not bankrupt) OR (actualstakes is not bankrupt)) /* using the method ParseLine inside SysCalc we convert the information * from a line in the file into the variables WinLose, Odds and Stake */ ParseLine(aBet); // to calculate the average odds add all the odds, later divide by the number of bets TotOdds = TotOdds + Odds; // number of winners and losers is the same for all staking plans if ( WinLose.compareTo("Lose") == 0) { Losers = Losers + 1; } else { Winners = Winners + 1; } // calculating level stakes ************************************************************************** if ( LsBankrupt == 0) { // Stake is always 1 LsStaked = LsStaked + 1; // Level stakes : total staked if ( WinLose.compareTo("Lose") == 0) { // loser LsBank = LsBank - 1; // Bank at level stakes if ( LsBank <= 0 ) { LsBankrupt = Winners + Losers; // number of bets before bancruptcy } } else { // winner LsProfit = ( Odds - 1 ) * Commission; // profit on bet LsBank = LsBank + LsProfit; // Bank at level stakes LsReturned = LsReturned + LsProfit + 1; // Return at level stakes } } // calculating actual stakes ************************************************************************* if ( MyBankrupt == 0) { // stake comes from the bet record if ( Stake > MyBank ) { // Bank does not cover next stake MyBankrupt = Winners + Losers - 1; // number of bets before bankruptcy } else { MyStaked = MyStaked + Stake; // actual stakes : total staked if ( WinLose.compareTo("Lose") == 0) { // loser MyBank = MyBank - Stake; // Bank at actual stakes } else { // winner MyProfit = ( ( Odds * Stake ) - Stake ) * Commission; // profit on bet MyBank = MyBank + MyProfit; // Bank at actual stakes MyReturned = MyReturned + MyProfit + Stake; // Return at actual stakes } } } // calculating fixed profit stakes ******************************************************************** if ( FpBankrupt == 0) { Stake = 1 / ( Odds - 1 ); // stake is set to make a profit of 1 if ( Stake > FpBank ) { // Bank does not cover next stake FpBankrupt = Winners + Losers - 1; // number of bets before bankruptcy } else { FpStaked = FpStaked + Stake; // fixed profit stakes : total staked if ( WinLose.compareTo("Lose") == 0) { // loser FpBank = FpBank - Stake; // Bank at fixed profit stakes } else { // winner FpProfit = ( ( Odds * Stake ) - Stake ) * Commission; // profit on bet FpBank = FpBank + FpProfit; // Bank at fixed profit stakes FpReturned = FpReturned + FpProfit + Stake; // Return at fixed profit stakes } } } // calculating percentile bank stakes ****************************************************************** if ( PbBankrupt == 0) { Stake = PbBank / 100; // Stake is 1 percent of current bank if ( Stake < 0.1 ) { // Bank down to less than 1% we declare bankruptcy PbBankrupt = Winners + Losers - 1; // number of bets before bankruptcy } else { PbStaked = PbStaked + Stake; // fixed profit stakes : total staked if ( WinLose.compareTo("Lose") == 0) { // loser PbBank = PbBank - Stake; // Bank at fixed profit stakes } else { // winner PbProfit = ( ( Odds * Stake ) - Stake ) * Commission; // profit on bet PbBank = PbBank + PbProfit; // Bank at fixed profit stakes PbReturned = PbReturned + PbProfit + Stake; // Return at fixed profit stakes } } } } /* end of WHILE loop, reading a line from the input file ******************************************************************************************/ // we have read all the lines from the file so we can close it. BetRecord.close(); if ( LsBankrupt == 0) { // write the calculations to the screen, level stakes System.out.println("Level stakes calculation"); System.out.println("Winning bets = " + Winners); System.out.println("Losing bets = " + Losers); System.out.println("Strike rate = " + (Winners / (Winners+Losers))*100); System.out.println("Average odds = " + TotOdds / (Winners+Losers)); System.out.println("Start Bank = 100"); System.out.println("End bank = " + LsBank); System.out.println("Profit/loss = " + (LsBank - 100)); System.out.println("Staked = " + LsStaked); System.out.println("Return = " + LsReturned); System.out.println("Yield = " + ((LsReturned/LsStaked)-1)*100); System.out.println(""); // write the calculations to out output file SysResult.write("Level stakes calculation"); SysResult.newLine(); SysResult.write("Winning bets," + Winners); SysResult.newLine(); SysResult.write("Losing bets," + Losers); SysResult.newLine(); SysResult.write("Strike rate," + (Winners / (Winners+Losers))*100); SysResult.newLine(); SysResult.write("Average odds," + TotOdds / (Winners+Losers)); SysResult.newLine(); SysResult.write("Start Bank,100"); SysResult.newLine(); SysResult.write("End bank," + LsBank); SysResult.newLine(); SysResult.write("Profit/loss," + (LsBank - 100)); SysResult.newLine(); SysResult.write("Staked," + LsStaked); SysResult.newLine(); SysResult.write("Return," + LsReturned); SysResult.newLine(); SysResult.write("Yield," + ((LsReturned/LsStaked)-1)*100); SysResult.newLine(); SysResult.write(""); SysResult.newLine(); } else { System.out.println("Level stakes went bankrupt after " + LsBankrupt + " bets."); System.out.println(""); SysResult.write("Level stakes went bankrupt after " + LsBankrupt + " bets."); SysResult.newLine(); } if ( MyBankrupt == 0) { // write the calculations to the screen, actual stakes System.out.println("Actual stakes calculation"); System.out.println("Winning bets = " + Winners); System.out.println("Losing bets = " + Losers); System.out.println("Strike rate = " + (Winners/(Winners+Losers))*100); System.out.println("Average odds = " + TotOdds / (Winners+Losers)); System.out.println("Start Bank = 100"); System.out.println("End bank = " + MyBank); System.out.println("Profit/loss = " + (MyBank - 100)); System.out.println("Staked = " + MyStaked); System.out.println("Return = " + MyReturned); System.out.println("Yield = " + ((MyReturned/MyStaked)-1)*100); System.out.println(""); SysResult.write("Actual stakes calculation"); SysResult.newLine(); SysResult.write("Winning bets," + Winners); SysResult.newLine(); SysResult.write("Losing bets," + Losers); SysResult.newLine(); SysResult.write("Strike rate," + (Winners/(Winners+Losers))*100); SysResult.newLine(); SysResult.write("Average odds," + TotOdds / (Winners+Losers)); SysResult.newLine(); SysResult.write("Start Bank,100"); SysResult.newLine(); SysResult.write("End bank," + MyBank); SysResult.newLine(); SysResult.write("Profit/loss," + (MyBank - 100)); SysResult.newLine(); SysResult.write("Staked," + MyStaked); SysResult.newLine(); SysResult.write("Return," + MyReturned); SysResult.newLine(); SysResult.write("Yield," + ((MyReturned/MyStaked)-1)*100); SysResult.newLine(); SysResult.write(""); SysResult.newLine(); } else { System.out.println("Actual stakes went bankrupt after " + MyBankrupt + " bets."); System.out.println(""); SysResult.write("Actual stakes went bankrupt after " + MyBankrupt + " bets."); SysResult.newLine(); } if ( FpBankrupt == 0) { // write the calculations to the screen, fixed profit stakes System.out.println("Fixed profits calculation"); System.out.println("Winning bets = " + Winners); System.out.println("Losing bets = " + Losers); System.out.println("Strike rate = " + (Winners/(Winners+Losers))*100); System.out.println("Average odds = " + TotOdds / (Winners+Losers)); System.out.println("Start Bank = 100"); System.out.println("End bank = " + FpBank); System.out.println("Profit/loss = " + (FpBank - 100)); System.out.println("Staked = " + FpStaked); System.out.println("Return = " + FpReturned); System.out.println("Yield = " + ((FpReturned/FpStaked)-1)*100); System.out.println(""); SysResult.write("Fixed profits calculation"); SysResult.newLine(); SysResult.write("Winning bets," + Winners); SysResult.newLine(); SysResult.write("Losing bets," + Losers); SysResult.newLine(); SysResult.write("Strike rate," + (Winners/(Winners+Losers))*100); SysResult.newLine(); SysResult.write("Average odds," + TotOdds / (Winners+Losers)); SysResult.newLine(); SysResult.write("Start Bank,100"); SysResult.newLine(); SysResult.write("End bank," + FpBank); SysResult.newLine(); SysResult.write("Profit/loss," + (FpBank - 100)); SysResult.newLine(); SysResult.write("Staked," + FpStaked); SysResult.newLine(); SysResult.write("Return," + FpReturned); SysResult.newLine(); SysResult.write("Yield," + ((FpReturned/FpStaked)-1)*100); SysResult.newLine(); SysResult.write(""); SysResult.newLine(); } else { System.out.println("Actual stakes went bankrupt after " + FpBankrupt + " bets."); System.out.println(""); SysResult.write("Actual stakes went bankrupt after " + FpBankrupt + " bets."); SysResult.newLine(); } if ( PbBankrupt == 0) { // write the calculations to the screen, percentile bank stakes System.out.println("Percentile bank calculation"); System.out.println("Winning bets = " + Winners); System.out.println("Losing bets = " + Losers); System.out.println("Strike rate = " + (Winners/(Winners+Losers))*100); System.out.println("Average odds = " + TotOdds / (Winners+Losers)); System.out.println("Start Bank = 100"); System.out.println("End bank = " + PbBank); System.out.println("Profit/loss = " + (PbBank - 100)); System.out.println("Staked = " + PbStaked); System.out.println("Return = " + PbReturned); System.out.println("Yield = " + ((PbReturned/PbStaked)-1)*100); SysResult.write("Percentile bank calculation"); SysResult.newLine(); SysResult.write("Winning bets," + Winners); SysResult.newLine(); SysResult.write("Losing bets," + Losers); SysResult.newLine(); SysResult.write("Strike rate," + (Winners/(Winners+Losers))*100); SysResult.newLine(); SysResult.write("Average odds," + TotOdds / (Winners+Losers)); SysResult.newLine(); SysResult.write("Start Bank,100"); SysResult.newLine(); SysResult.write("End bank," + PbBank); SysResult.newLine(); SysResult.write("Profit/loss," + (PbBank - 100)); SysResult.newLine(); SysResult.write("Staked," + PbStaked); SysResult.newLine(); SysResult.write("Return," + PbReturned); SysResult.newLine(); SysResult.write("Yield," + ((PbReturned/PbStaked)-1)*100); SysResult.newLine(); } else { System.out.println("Percentile bank went bankrupt after " + PbBankrupt + " bets."); SysResult.write("Percentile bank went bankrupt after " + PbBankrupt + " bets."); SysResult.newLine(); } // all written to the output file so we can close it SysResult.close(); } // this bracket is the end of the main method inside of our program SysCalc /* * This part is a method called ParseLine * * it requires a String as input and then * sets the variables WinLose, Odds, Stake * */ static void ParseLine(String PL) { // this bracket is the start of method ParseLine int start; int end; String temp; start = PL.indexOf(","); // search for the first comma end = PL.indexOf(",",start+1); // search for the second comma, // starting after the position of the first comma WinLose = PL.substring(start+1,end); // put the part of the string // between the first and second comma into WinLose // note : we are completely skipping everything before the first comma // being the description of our bet. // number between second and third comma is Odds, converting from String to Float start = end+1; end = PL.indexOf(",",start); temp = PL.substring(start,end); Odds = Float.parseFloat(temp); // number between third comma and end of the string is Stake, converting from String to Float start = end+1; temp = PL.substring(start); Stake = Float.parseFloat(temp); } // this bracket is the end of method ParseLine } // this bracket is the end of our program SysCalc