Help with Balance

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 39
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster

Help with Balance

 
0
  #1
Nov 22nd, 2008
Hi Guys, I am trying to make a console based ATM Machine simulator, I have removed all the code that was not relevant to this challenge I am having.

What I am trying to do in display the balance which I can do, opening balance is 94.37, then I need to either do a Deposit or a Withdraw and have that amount change when i redisplay the balance.

I know the problem is that when I display the balance whether it be before or after I make a deposit or withdraw it takes the balance from the global variable, what I don't know is how not to use a global variable but still get a starting value and then return the Deposit/Withdraw balance to the existing balance and overwrite it.

I have no experience with classes/methods in C#, I called everything static cause thats how it was done in a book I started to read.

I have done a kpl paper so do know about methods/functions but not in this context.

Someone recommended making another class and calling that or making my methods private and using those values, I tryed the latter but it threw up errors in my menu switch statement because it didn't recognise the:
WithDraw(balance) and the Deposit(balance) in the switch statment because they were variables not declared in global yet i was trying to run/call them in global/main method,

Hope you can understand this, I spent about 2/3 hours writing the main bits of code and over 6 hours trying to get this 1 bit working, any help would be much appreciated.

thank you.




  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace ATMMachine
  7. {
  8. class MyClass
  9. {
  10. static void Main(string[] args)
  11. {
  12.  
  13. int option = 0;
  14. string output = "";
  15. bool done = false;
  16. int choice = 0;
  17. double balance = 94.37;
  18. //double balance = startBalance;
  19. //double withDrawBalance = 0;
  20. Console.Write("This an Atm Machine simulation program\n");
  21.  
  22. while (!done)
  23. {
  24. if (choice == 5)
  25. {
  26. option = 5;
  27. }
  28. else if (choice == 7)
  29. {
  30. option = AtmInterface();
  31. }
  32. else
  33. {
  34. option = AtmInterface();
  35. }
  36.  
  37.  
  38. switch (option)
  39. {
  40. case 1:
  41. ChangePin();
  42. break;
  43. case 2:
  44. //output = " Display Balance";
  45. //UnderConstruction(output);
  46. Console.WriteLine("Balance is: $" + AccountBalance(newBalance) + "c\n");
  47. System.Threading.Thread.Sleep(2000);
  48. break;
  49. case 3:
  50. // output = " \tWithdraw";
  51. //UnderConstruction(output);
  52. WithDraw();
  53. break;
  54. case 4:
  55. //output = " \t\tDeposit";
  56. //UnderConstruction(output);
  57. Deposit();
  58. break;
  59. case 5:
  60. Console.WriteLine("\nTransaction ended, card is been returned");
  61. done = true;
  62. break;
  63. case 6:
  64. output = " \tTest Pin";
  65. UnderConstruction(output);
  66. break;
  67. default:
  68. AtmInterface();
  69. break;
  70. }// end switch
  71. } // end while
  72.  
  73.  
  74. // pause the console window
  75. Console.WriteLine("\nPress any key to continue...");
  76. Console.WriteLine("Program complete");
  77. Console.ReadLine();
  78.  
  79. }// end static
  80.  
  81.  
  82.  
  83.  
  84.  
  85. static double WithDraw()
  86. {
  87. double withDrawAmount = 0;
  88. //double withDrawBalance = 0;
  89. bool done = false;
  90. bool zero = false;
  91. double balance = 94.37;
  92. double newBalance = 0;
  93.  
  94. while (!zero)
  95. {
  96. if (balance == 0)
  97. {
  98. Console.WriteLine("Insufficient funds to withdraw, withdraw cancelled\n");
  99. System.Threading.Thread.Sleep(4000);
  100. zero = true;
  101. }
  102. else
  103. {
  104. Console.WriteLine("Withdraws must be $20 or multiples of $20\n");
  105. while (!done)
  106. {
  107. Console.Write("Amount to Withdraw: $");
  108. try
  109. {
  110. withDrawAmount = int.Parse(Console.ReadLine());
  111. }
  112. catch (FormatException)
  113. {
  114. }
  115. if (withDrawAmount % 20 != 0.00 || withDrawAmount == 0)
  116. {
  117. Console.WriteLine("\nAmount of Withdraw must in multiples of $20\n");
  118.  
  119. string keyEntered = "n";
  120. bool correctKey = false;
  121.  
  122. while (!correctKey)
  123. {
  124. Console.Write("Do you wish to continue 'y' or 'n': ");
  125. try
  126. {
  127. keyEntered = Console.ReadLine();
  128. }
  129. catch (FormatException)
  130. {
  131. }
  132. keyEntered = keyEntered.ToLower();
  133. if (keyEntered == "n")
  134. {
  135. Console.WriteLine("\nReturning to Main Options Menu");
  136. done = true;
  137. zero = true;
  138. correctKey = true;
  139. System.Threading.Thread.Sleep(4000);
  140. }
  141. else if (keyEntered != "n" && keyEntered != "y")
  142. {
  143. Console.WriteLine("Invalid selection");
  144. }
  145. else
  146. {
  147. correctKey = true;
  148. }
  149. }// end while
  150.  
  151.  
  152. }
  153. else if (withDrawAmount < balance)
  154. {
  155. Console.WriteLine("\nWithdraw was successful\n");
  156. balance -= withDrawAmount;
  157. newBalance = balance;
  158. // System.Threading.Thread.Sleep(4000);
  159. done = true;
  160. zero = true;
  161. }
  162. else
  163. {
  164. Console.WriteLine("\nInsufficient funds available, withdraw cancelled\n");
  165. System.Threading.Thread.Sleep(4000);
  166. done = true;
  167. zero = true;
  168. }
  169. }// end while
  170. }// end outer if
  171. }// end out while
  172. return newBalance;
  173. }// end static void withDraw
  174.  
  175.  
  176.  
  177.  
  178.  
  179. static double Deposit()
  180. {
  181. double deposit_Amount = 0;
  182. bool done = false;
  183. double balance = 94.37;;
  184. double newBalance;
  185.  
  186. Console.WriteLine("Deposits must be $10 or in multilpes of $10\n");
  187.  
  188. while (!done)
  189. {
  190. Console.Write("How much do you wish to deposit: $");
  191. try
  192. {
  193. deposit_Amount = double.Parse(Console.ReadLine());
  194. }
  195. catch (FormatException)
  196. {
  197. }
  198. if (deposit_Amount % 10 != 0 || deposit_Amount ==0)
  199. {
  200. Console.WriteLine("\nDeposits must be in multiples of $10\n");
  201. Console.WriteLine("Deposit transaction failed, returning to main menu\n");
  202. System.Threading.Thread.Sleep(4000);
  203. done = true;
  204. }
  205. else
  206. {
  207. Console.WriteLine("\nDeposit of $" + deposit_Amount + " was successful\n");
  208. balance += deposit_Amount;
  209. System.Threading.Thread.Sleep(4000);
  210. done = true;
  211. }
  212.  
  213. }// ends while
  214. return balance;
  215. }// ends static double deposit
  216.  
  217. static double AccountBalance(double newBalance)
  218. {
  219. return newBalance;
  220. }
  221.  
  222. }
  223. }

Some of the code has been butchered abit due to my many attempts to fix the problem, Static Deposit has not really been worked too much because I know if I fix Withdraw then the same solution can be used for Deposit also.

Basically I need to go, Display Balance of 94.37 > withdraw any amount less then Balance> then display new balance and have the new balance display correctly.

cheers
Last edited by mr_scooby; Nov 22nd, 2008 at 4:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,956
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 283
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Help with Balance

 
0
  #2
Nov 22nd, 2008
case 3:
// output = " \tWithdraw";
//UnderConstruction(output);
WithDraw();
break;

WithDraw should return something to a "global" variable. I know the compiler should give you an error here, but it doesn't...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Help with Balance

 
0
  #3
Nov 22nd, 2008
Everytime you enter the Deposit() method you are setting the deposit amount to 94.37 :. its not really ever going to change. I mean you will return a different balance from that method but as soon as you attempt to deposit again you will be starting at 94.37$ again.

You have to pass around the deposit amount as a parameter to the method

  1. static double Deposit(double balance)

do that and keep track of the balance in the main method, Is
perhaps the simplest way.

What you really should do is split the problem up into classes etc. but anyhow.

Also there are no global variables in c# but you can create a class static public class var and then use that ( MyClassName.staticVarName)

But it is nicer ( i think ) to pass it around.

For multiple reasons, One being that the problem its self does not lend its self IMHO to a static var, If you assume that more then one person will use your ATM then you will be managing multiple balances :. you wouldn't expect that the balanace would be one global var but would be a variables attached to a class that contains that persons account information.

So in conclusion you could say
  1.  
  2. namespace ATMMachine
  3. {
  4. class MyClass
  5. {
  6. public static double balance; //each time i use this now I have to type MyClass.balance;
  7. ......
  8. }
  9. }

then only set it to 94 in the main method (before its first used), not in the withdraw and deposit methods.
Last edited by Paul.Esson; Nov 22nd, 2008 at 8:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 39
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster

Re: Help with Balance

 
0
  #4
Nov 22nd, 2008
heres the 2 bits that are causing the problem

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace Sec05ATMMoneyOnly
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12.  
  13. int option = 0;
  14. string output = "";
  15. bool done = false;
  16. int choice = 0;
  17. //double newBalance = 0;
  18. double balance = 94.37;
  19. //double balance = startBalance;
  20. //double withDrawBalance = 0;
  21. Console.Write("This an Atm Machine simulation program\n");
  22.  
  23.  
  24. while (!done)
  25. {
  26. if (choice == 5)
  27. {
  28. option = 5;
  29. }
  30. else if (choice == 7)
  31. {
  32. option = AtmInterface();
  33. }
  34. else
  35. {
  36. option = AtmInterface();
  37. }
  38.  
  39.  
  40. switch (option)
  41. {
  42. case 1:
  43. //ChangePin();
  44. break;
  45. case 2:
  46. //output = " Display Balance";
  47. //UnderConstruction(output);
  48. Console.WriteLine("Balance is: $" + AccountBalance(balance) + "c\n");
  49. System.Threading.Thread.Sleep(2000);
  50. break;
  51. case 3:
  52. // output = " \tWithdraw";
  53. //UnderConstruction(output);
  54. WithDraw(balance);
  55. break;
  56. case 4:
  57. //output = " \t\tDeposit";
  58. //UnderConstruction(output);
  59. Deposit();
  60. break;
  61. case 5:
  62. Console.WriteLine("\nTransaction ended, card is been returned");
  63. done = true;
  64. break;
  65. case 6:
  66. output = " \tTest Pin";
  67. //UnderConstruction(output);
  68. break;
  69. default:
  70. AtmInterface();
  71. break;
  72. }// end switch
  73. } // end while
  74.  
  75.  
  76. // pause the console window
  77. Console.WriteLine("\nPress any key to continue...");
  78. Console.WriteLine("Program complete");
  79. Console.ReadLine();
  80.  
  81. }// end static
  82.  
  83.  
  84.  
  85. static double WithDraw(double balance)
  86. {
  87. double withDrawAmount = 0;
  88. //double withDrawBalance = 0;
  89. bool done = false;
  90. bool zero = false;
  91. //double balance = 94.37;
  92. double newBalance = 0;
  93.  
  94. while (!zero)
  95. {
  96. if (balance == 0)
  97. {
  98. Console.WriteLine("Insufficient funds to withdraw, withdraw cancelled\n");
  99. System.Threading.Thread.Sleep(4000);
  100. zero = true;
  101. }
  102. else
  103. {
  104. Console.WriteLine("Withdraws must be $20 or multiples of $20\n");
  105. while (!done)
  106. {
  107. Console.Write("Amount to Withdraw: $");
  108. try
  109. {
  110. withDrawAmount = int.Parse(Console.ReadLine());
  111. }
  112. catch (FormatException)
  113. {
  114. }
  115. if (withDrawAmount % 20 != 0.00 || withDrawAmount == 0)
  116. {
  117. Console.WriteLine("\nAmount of Withdraw must in multiples of $20\n");
  118.  
  119. string keyEntered = "n";
  120. bool correctKey = false;
  121.  
  122. while (!correctKey)
  123. {
  124. Console.Write("Do you wish to continue 'y' or 'n': ");
  125. try
  126. {
  127. keyEntered = Console.ReadLine();
  128. }
  129. catch (FormatException)
  130. {
  131. }
  132. keyEntered = keyEntered.ToLower();
  133. if (keyEntered == "n")
  134. {
  135. Console.WriteLine("\nReturning to Main Options Menu");
  136. done = true;
  137. zero = true;
  138. correctKey = true;
  139. System.Threading.Thread.Sleep(4000);
  140. }
  141. else if (keyEntered != "n" && keyEntered != "y")
  142. {
  143. Console.WriteLine("Invalid selection");
  144. }
  145. else
  146. {
  147. correctKey = true;
  148. }
  149. }// end while
  150.  
  151.  
  152. }
  153. else if (withDrawAmount < balance)
  154. {
  155. Console.WriteLine("\nWithdraw was successful\n");
  156. balance -= withDrawAmount;
  157. newBalance = balance;
  158. // System.Threading.Thread.Sleep(4000);
  159. done = true;
  160. zero = true;
  161. }
  162. else
  163. {
  164. Console.WriteLine("\nInsufficient funds available, withdraw cancelled\n");
  165. System.Threading.Thread.Sleep(4000);
  166. done = true;
  167. zero = true;
  168. }
  169. }// end while
  170. }// end outer if
  171. }// end out while
  172. balance = newBalance;
  173. return newBalance;
  174. }// end static void withDraw
  175.  
  176.  
  177.  
  178. static double AccountBalance(double balance)
  179. {
  180. return balance;
  181. }
  182. }
  183. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: Help with Balance

 
0
  #5
Nov 22nd, 2008
  1. case 3:
  2. // output = " \tWithdraw";
  3. //UnderConstruction(output);
  4. WithDraw(balance);

should read

  1. case 3:
  2. // output = " \tWithdraw";
  3. //UnderConstruction(output);
  4. balance = WithDraw(balance);
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 39
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster

Re: Help with Balance

 
0
  #6
Nov 22nd, 2008
Originally Posted by Paul.Esson View Post
Everytime you enter the Deposit() method you are setting the deposit amount to 94.37 :. its not really ever going to change. I mean you will return a different balance from that method but as soon as you attempt to deposit again you will be starting at 94.37$ again.

You have to pass around the deposit amount as a parameter to the method

  1. static double Deposit(double balance)

do that and keep track of the balance in the main method, Is
perhaps the simplest way.

What you really should do is split the problem up into classes etc. but anyhow.

Also there are no global variables in c# but you can create a class static public class var and then use that ( MyClassName.staticVarName)

But it is nicer ( i think ) to pass it around.

For multiple reasons, One being that the problem its self does not lend its self IMHO to a static var, If you assume that more then one person will use your ATM then you will be managing multiple balances :. you wouldn't expect that the balanace would be one global var but would be a variables attached to a class that contains that persons account information.

So in conclusion you could say
  1.  
  2. namespace ATMMachine
  3. {
  4. class MyClass
  5. {
  6. public static double balance; //each time i use this now I have to type MyClass.balance;
  7. ......
  8. }
  9. }

then only set it to 94 in the main method (before its first used), not in the withdraw and deposit methods.
Thanks heaps dude, I didn't even really know about declaring before method main.

problem solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC