Reputation: 111
So, i'm building a bank simulator that includes everything i learned so far, i just wanna take that piece of code to another level, until its power get to over 9000. (Props for DBZ reference!) I'm looking for a way in my piece of code, to update the value of the balance after operation. Because right now, after an operation, my value is resetted and I don't want that. And also, by the same occasion, give me some pointers, tips to make the code shorter, more readable, to add more depth to it!
using System;
namespace Bank
{
public class Program
{
static void Main()
{
Console.ForegroundColor = ConsoleColor.Yellow;
double balance = 2000;
bool exit = true;
Console.WriteLine("|----------Welcome to the Nobody's Poor Bank!----------|");
Console.ReadLine();
Console.WriteLine("You actually have 2000$ in your bank account.");
Console.ReadLine();
Console.Clear();
while (exit==true)
{
Console.WriteLine("What operation would you like to do?\n");
Console.Write("(W) Withdraw (D) Deposit (E) Exit : ");
string choice = Console.ReadLine();
switch (choice)
{
case "W":
case "w":
Console.Write("\nHow much money would you like to withdraw? : ");
string WithdrawAmount = Console.ReadLine();
double WithdrawValue;
double.TryParse(WithdrawAmount, out WithdrawValue);
balance -= WithdrawValue;
Console.WriteLine("\nAfter the operation, your balance is now " + balance + "$");
Console.ReadKey();
Console.Clear();
break;
case "D":
case "d":
Console.Write("\nHow much money would you like to deposit? : ");
string DepositAmount = Console.ReadLine();
double DepositValue;
double.TryParse(DepositAmount, out DepositValue);
balance += DepositValue;
Console.WriteLine("\nAfter the operation, your balance is now " + balance + "$");
Console.ReadKey();
Console.Clear();
break;
case "E":
case "e":
Console.WriteLine("\nThank you for using our services!");
Console.ReadKey();
exit = false;
break;
default:
Console.WriteLine("\nThat is not a valid input...");
Console.ReadKey();
Console.Clear();
break;
}
}
}
}
}
Upvotes: 1
Views: 384
Reputation: 143
1) Declare your balance as static on the program class not on the main thread Like
Static double balance = 2000;
This will update the balance value for the lifetime of the session of the programm run
2) instead of using d and D for your switch cases you can use
Switch(choice.ToUpper())
Case : "D"
This will convert the choice to Uppercase so you will only test capital letter D or B or whatever. This means that if the user enters d or D it will still give the same output.
3) Using a switch statement means that you want the application to act in a certain way if ancondition is true.....so you dont need to have console, readkey on all the case because only one will be exectuted at a time
Rather put that code after the switch and display after that like this,
Switch(Choice.ToUpper())
{ case : "D"
//some code..........
break;
case : "F"
//some code..........
break;
}
Console.WriteLine(balance.ToString());
Console.ReadKey();
Upvotes: 0
Reputation: 2522
In your code below, you are just calculating what the balance result should be, instead of actually updating it.
Console.WriteLine(
"\nAfter the operation, your balance is now " +
(balance - WithdrawValue) + "$");
Try changing this to
balance -= WithdrawValue;
Console.WriteLine("\nAfter the operation, your balance is now " + balance + "$");
The first line updates the balance variable with the expected result. The second line now just outputs the new value of balance to the Console.
Upvotes: 1