• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

C# question ....

Hi, I'm pretty new to C# and I'm trying to get this code to work correctly.
After making the initial deposit, if i went ahead and made other deposits and checked the balance, it doesn't work  - it doesn't update.


Please help?

TB
using System;
using System.Collections.Generic;
using System.Text;
 
namespace banking_program
{
    class customer
    {
        //constructor
        customer()
        {
            
            Console.WriteLine("Please Input Your Name :");
            clientName = Console.ReadLine();
 
 
 
            Console.WriteLine("Please Input Your Account Number :");
            accountNum = int.Parse(Console.ReadLine());
            Console.WriteLine();
            Console.WriteLine("Welcome {0}, You have successfully logged into your Account.", clientName);
            Console.WriteLine();
            Console.WriteLine ("Please Enter Your Initial Deposit Amount :");
            startBalance = double.Parse (Console.ReadLine ());
            Console.WriteLine();
            // Console.WriteLine("Welcome {0}, You have successfully logged into your Account",clientName);
            Console.WriteLine();
            accountBal = startBalance;
            totalDeposits = 0;
            totalWithdrawals =0;
        }
        //methods
        public static void calcDeposit(double deposit)
        {
            accountBal = accountBal + totalDeposits;
            totalDeposits = deposit + totalDeposits;
        }
 
        public static void calcWithdrawal(double withDrawal)
        {
            if (accountBal > withDrawal)
            {
                accountBal = accountBal - withDrawal;
                totalWithdrawals = withDrawal + totalWithdrawals;
            }
            else Console.WriteLine("You do not have sufficient funds !!!");
            }
        public static void Balance() 
        { 
            Console.WriteLine("Your Account Balance is: " + accountBal); 
        }
        public static void Main(string[] args)
        {
            int choice;
            double deposit, withdrawal;
            bool dec = true;
            customer account = new customer();
            while (dec)
            {
                Console.WriteLine("Please Select a Transction Type:");
                Console.WriteLine("********************************");
                Console.WriteLine("\n 1 To Make a Deposit\n 2 For Withdrawal\n 3 For Balance Inquiry\n 9 To Terminate The Session:\n ");
                choice = int.Parse(Console.ReadLine());
 
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("What is the Amount of Your Deposit ?");
                        deposit = double.Parse(Console.ReadLine());
                        calcDeposit(deposit);
                                                
                        break;
                  
 
                    case 2:
                        Console.WriteLine("What is the Amount you would like to Withdraw ?");
                        withdrawal = double.Parse(Console.ReadLine());
                        calcWithdrawal(withdrawal);
 
 
                        break;
 
 
                    case 3:
               
                        Balance();
 
                        break;
 
                    case 9:
                        Console.WriteLine("Customer Name: " + clientName);
                        Console.WriteLine("Customer ID: " + accountNum);
                        Console.WriteLine("Starting Balance: " + startBalance); 
 
                        Console.WriteLine("Total Deposits: " + totalDeposits);
                        Console.WriteLine("Total Withdrawals: " + totalWithdrawals);
 
                        Console.WriteLine("Closing Balance: " + accountBal);
                        dec = false;
 
                        
                        break;
 
                        
                }
            
            }
        }
        // Atributes 
 
            static string clientName;
            static int accountNum;
            static double startBalance;
            static double accountBal;
            static double totalDeposits;
            static double totalWithdrawals;
            
 
 
    
 
    }
 
}

Open in new window

0
neverenough
Asked:
neverenough
  • 3
  • 2
1 Solution
 
Fernando SotoCommented:
Hi neverenough;

It seems you used the wrong variables in the calculations in the calcDeposit method, see code snippet.

Fernando
//methods
public static void calcDeposit(double deposit)
{
    // To get the total of all deposits do this
    totalDeposits = totalDeposits + deposit;
    // Add deposit to account balance
    accountBal = accountBal + deposit;
    // The following lines were removed
    //accountBal = accountBal + totalDeposits;
    //totalDeposits = deposit + totalDeposits;
}

Open in new window

0
 
neverenoughAuthor Commented:
Thanks Fernando !   worked like a charm!
sorry I'm new to this.
0
 
Fernando SotoCommented:
Not a problem, glad to help.  ;=)
0
 
neverenoughAuthor Commented:
I awarded you 500 points but i got a wired error message. Can you check? I may have messed up.
let me know...
0
 
Fernando SotoCommented:
Everything is fine. Thanks; Fernando
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now