Reputation: 49
Problem: If you deposit 1000-->then borrow 40000-->then withdraw 43000 and get the error "insufficient balance" --> then try again and deposit 28000. The issue here is that the balance is -31000 the second time you try instead of something around 12000. Anyone that knows why?
I've tried moving the capital "statement" into the else statement but that just made things worse. I've also tried moving it to the account stats section but that didn't work either. I'm new to this and im not really that good at problem solving so sorry if it's something obvious:/
using System;
namespace atmtest
{
class Program
{
public static void Main()
{
int balance = 0, deposit = 0, withdraw = 0, choice, loan = 0, kredit = 0, debt = 0, capital = 0;
while (true)
{
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Clear();
try
{
Console.WriteLine("Deposit, enter amount : ");
deposit = int.Parse(Console.ReadLine());
if (deposit > 500000)
{
Console.WriteLine("Error, max amount 500000");
}
else if (deposit < 0)
{
Console.WriteLine("Error, the amount has to be positive");
}
else
{
capital = balance + deposit;
Console.WriteLine("You have deposited " + deposit + " and your balance is now: " + capital);
}
}
catch
{
Console.WriteLine("Error, enter numbers only.");
}
break;
case 2:
Console.Clear();
Console.WriteLine("Withdraw, enter amount: ");
withdraw = int.Parse(Console.ReadLine());
capital = kredit + balance - withdraw;
balance = balance - withdraw;
if ((withdraw > balance) && (withdraw > kredit))
{
Console.WriteLine("Error, insufficent balance.");
capital = deposit;
Console.WriteLine("Try again");
}
else
{
debt = balance;
Console.WriteLine("You have withdrawn " + withdraw);
Console.WriteLine("Your current balance is " + capital);
}
break;
case 3:
Console.Clear();
Console.WriteLine("Account stats: ");
Console.WriteLine("Balance: " + capital);
Console.WriteLine("Amount of money borrowed ( loan ): " + kredit);
Console.WriteLine("Debt: " + debt);
Console.WriteLine("You have used " + debt );
break;
case 4:
Console.Clear();
Console.WriteLine("how much do you want to borrow? Max amount is 50000.");
Console.WriteLine("Enter amount: ");
loan = int.Parse(Console.ReadLine());
kredit = loan + kredit;
if (kredit > 50000)
{
Console.WriteLine("You cannot borrow this amount, max amount is 50000");
kredit = kredit - loan;
}
else if (kredit <= 50000)
{
Console.WriteLine("You have been granted " + loan);
}
break;
}
}
}
}
}
Upvotes: 2
Views: 266
Reputation: 1
I changed a few code from the original code, and I noticed that on your switch statement case 2, even there is withdraw > balance, your program will still makes balance -= withdraw first because the balance = balance - withdraw is coded outside of the if statement and that will produce your error.
using System;
namespace simpleAtm
{
class Program
{
static void Main()
{
int balance = 0, deposit = 0, withdraw = 0, choice, loan = 0, kredit = 0, debt = 0, capital = 0;
while (true)
{
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Clear();
try
{
Console.WriteLine("Deposit, enter amount : ");
deposit = int.Parse(Console.ReadLine());
if (deposit > 500000)
{
Console.WriteLine("Error, max amount 500000");
}
else if (deposit < 0)
{
Console.WriteLine("Error, the amount has to be positive");
}
else
{
capital += balance + deposit;
Console.WriteLine("You have deposited " + deposit + " and your balance is now: " + capital);
}
}
catch
{
Console.WriteLine("Error, enter numbers only.");
}
break;
case 2:
Console.Clear();
balance = capital;
Console.WriteLine(capital);
Console.WriteLine("Withdraw, enter amount: ");
withdraw = int.Parse(Console.ReadLine());
if (withdraw > balance + kredit)
{
Console.WriteLine("Error, insufficent balance.");
capital = deposit;
Console.WriteLine("Try again");
}
else
{
balance -= withdraw;
if(balance - withdraw < 0){
balance *= -1;
kredit -= balance;
balance = 0;
capital = kredit;
Console.WriteLine(balance+" "+kredit);
debt += withdraw;
Console.WriteLine("You have withdrawn " + withdraw);
Console.WriteLine("Your current balance is " + capital);
}
else{
capital = balance + kredit;
debt += withdraw;
Console.WriteLine("You have withdrawn " + withdraw);
Console.WriteLine("Your current balance is " + capital);
}
}
break;
case 3:
Console.Clear();
Console.WriteLine("Account stats: ");
Console.WriteLine("Balance: " + capital);
Console.WriteLine("Amount of money borrowed ( loan ): " + kredit);
Console.WriteLine("Debt: " + debt);
Console.WriteLine("You have used " + debt );
break;
case 4:
Console.Clear();
Console.WriteLine("how much do you want to borrow? Max amount is 50000.");
Console.WriteLine("Enter amount: ");
loan = int.Parse(Console.ReadLine());
kredit = loan + kredit;
if (kredit > 50000)
{
Console.WriteLine("You cannot borrow this amount, max amount is 50000");
kredit = kredit - loan;
}
else if (kredit <= 50000)
{
Console.WriteLine("You have been granted " + loan);
}
break;
}
}
}
}
}
Upvotes: 0
Reputation: 94
using System;
namespace atmtest
{
class Program
{
static void Main(string[] args)
{
int balance = 0,
deposit = 0,
withdraw = 0,
choice,
loan = 0,
kredit = 0,
debt = 0,
capital = 0,
repay = 0;
while (true)
{
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("5) Return borrowed money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Clear();
try
{
Console.WriteLine("Deposit, enter amount : ");
deposit = int.Parse(Console.ReadLine());
if (deposit > 500000)
{
Console.WriteLine("Error, max amount 500000");
}
else if (deposit < 0)
{
Console.WriteLine("Error, the deposit has to be positive.");
}
else
{
balance = balance + deposit;
Console.WriteLine($"You have deposited {deposit} and your balance is now: {balance}.");
}
}
catch
{
Console.WriteLine("Error, enter numbers only.");
}
break;
case 2:
Console.Clear();
try
{
Console.WriteLine("Withdraw, enter amount: ");
withdraw = int.Parse(Console.ReadLine());
if (withdraw < 0)
{
Console.WriteLine("The amount must be positive number.");
}
else if (withdraw > balance)
{
Console.WriteLine($"Error, insufficent balance {balance}.\nTry again.");
}
else
{
balance = balance - withdraw;
Console.WriteLine($"You have withdrawn {withdraw}. Your current balance is {balance}.");
}
}
catch
{
Console.WriteLine("Error. Enter numbers only.");
}
break;
case 3:
Console.Clear();
Console.WriteLine("Account stats: ");
Console.WriteLine("Balance: " + balance);
Console.WriteLine("Debt: " + debt);
break;
case 4:
Console.Clear();
try
{
Console.WriteLine("how much do you want to borrow? Max amount is 50000.");
Console.WriteLine("Enter amount: ");
loan = int.Parse(Console.ReadLine());
if (loan < 0)
{
Console.WriteLine("Loan cannot be negative.");
}
else if (loan > 50000)
{
Console.WriteLine("You cannot borrow this amount, max amount is 50000");
}
else if (debt + loan > 50000)
{
Console.WriteLine(
$"You cannot borrow this amount it makes your debt {debt + loan} > max debt amount 50000.");
}
else
{
debt += loan;
balance += loan;
Console.WriteLine($"You borrowed {loan} and your kredit now is {kredit}.");
}
}
catch
{
Console.WriteLine("Error. Enter numbers only.");
}
break;
case 5:
Console.Clear();
try
{
Console.WriteLine("how much do you want to repay? Max amount is 50000.");
Console.WriteLine("Enter amount: ");
repay = int.Parse(Console.ReadLine());
int maxRepay = (debt < 50000) ? debt : 50000;
if (repay < 0)
{
Console.WriteLine($"You cannot repay negative amount {repay}");
}
else if (repay > maxRepay)
{
Console.WriteLine($"Repay cannot be more than {maxRepay}.");
}
else
{
debt -= repay;
balance -= repay;
Console.WriteLine($"You repaied {repay} of the debt. You debt now is {debt}.");
}
}
catch
{
Console.WriteLine("Error. Enter numbers only.");
}
break;
}
}
}
}
}
Upvotes: 0
Reputation: 407
I just run the code. You are constantly mixing different terms like capital and balance. Try to use only one of the variables in your code. In addition, since you are new, it is a good habit to extract lines of code to methods to make everything more structured and better readable. This is quite important for bigger applications. Something like this in a new class file:
public class BankingConsole
{
private int Balance { get; set; } = 0;
private int Deposit { get; set; } = 0;
private int Withdraw { get; set; } = 0;
private int Loan { get; set; } = 0;
private int Credit { get; set; } = 0;
private int Debt { get; set; } = 0;
private int Choice { get; set; } = 0;
public BankingConsole()
{
StartSimulation();
}
public void StartSimulation()
{
while (true)
{
Console.WriteLine("Balance: " + Balance.ToString());
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
Choice = int.Parse(Console.ReadLine());
Console.Clear();
switch (Choice)
{
case 1:
DepositMoney();
break;
case 2:
WithdrawMoney();
break;
case 3:
ShowAccountStats();
break;
case 4:
BorrowMoney();
break;
default:
break;
}
}
}
private void DepositMoney()
{
try
{
Console.WriteLine("Deposit, enter amount : ");
Deposit = int.Parse(Console.ReadLine());
if (Deposit > 500000)
{
Console.WriteLine("Error, max amount 500000");
}
else if (Deposit < 0)
{
Console.WriteLine("Error, the amount has to be positive");
}
else
{
Balance += Deposit;
Console.WriteLine("You have deposited " + Deposit + " and your balance is now: " +
Balance);
}
}
catch
{
Console.WriteLine("Error, enter numbers only.");
}
}
private void WithdrawMoney()
{
Console.WriteLine("Withdraw, enter amount: ");
Withdraw = int.Parse(Console.ReadLine());
if (Withdraw > Balance)
{
Console.WriteLine("Error, insufficent funds.");
}
else
{
Balance -= Withdraw;
Debt += Withdraw;
Console.WriteLine("You have withdrawn " + Withdraw);
Console.WriteLine("Your current balance is " + Balance);
}
}
private void ShowAccountStats()
{
Console.WriteLine("Account stats: ");
Console.WriteLine("Balance: " + Balance);
Console.WriteLine("Amount of money borrowed ( loan ): " + Credit);
Console.WriteLine("Debt: " + Debt);
}
private void BorrowMoney()
{
Console.WriteLine("how much do you want to borrow? Max amount is 50000.");
Console.WriteLine("Enter amount: ");
Loan = int.Parse(Console.ReadLine());
if (Credit + Loan > 50000)
{
Console.WriteLine("You cannot borrow this amount, max amount is 50000");
}
else
{
Credit += Loan;
Balance += Loan;
Console.WriteLine("You have been granted " + Loan);
}
}
}
And you call this code in your main like this:
static void Main(string[] args)
{
new BankingConsole().StartSimulation();
}
Upvotes: 1
Reputation: 667
Try this little change to track your variable values, actually the message you are getting is correct. These are based on your values assignment only.
Please change the snippet and try to run :
int balance = 0, deposit = 0, withdraw = 0, choice, loan = 0, kredit = 0, debt = 0, capital = 0;
StringBuilder audit = new StringBuilder();
while (true)
{
Console.WriteLine("----------------------------");
Console.WriteLine("Welcome");
Console.WriteLine("----------------------------");
Console.WriteLine("Audit");
audit.Append($" balance = {balance}, deposit = {deposit}, withdraw = {withdraw}, loan = {loan}, kredit = {kredit}, debt = {debt}, capital = {capital} \n");
Console.WriteLine(audit.ToString());
Console.WriteLine("----------------------------");
Console.WriteLine("1) Deposit");
Console.WriteLine("2) Withdraw");
Console.WriteLine("3) Account stats");
Console.WriteLine("4) Borrow money");
Console.WriteLine("--------------------------------");
Console.WriteLine("Enter one of the options ");
....
....
It is clearly showing after withdraw balance not reduced, hence need programmatic correction in logic.
Upvotes: 1