locked
how to write a code for withdrawal using C# RRS feed

  • Question

  • i am confused i don't know what to do after this place i want to withdraw cash from the atm and my bank balance is 10000

                    // if Y or y, program should continue

                    Console.WriteLine("how much would you like to withdraw");

                    Console.ReadLine();

    Saturday, August 2, 2014 4:14 PM

Answers

  • char choice='y', r_choice;

    int balance, draw, cur_balance;

    balance = 10000;

    Console.WriteLine("how much would you like to withdraw");

    draw= Console.ReadLine();

    cur_balance = balance - draw;

    do{

    Console.WriteLine("Your new balance is: " + cur_balance);

    }

    Console.WriteLine("Would you like to make another withrawal? Y/N");

    while(choice == 'y' || choice == 'Y')

       Console.WriteLine("how much would you like to withdraw");


     

    • Proposed as answer by Ioana Vasilescu Saturday, August 2, 2014 6:02 PM
    • Marked as answer by Kristin Xie Wednesday, August 27, 2014 3:20 AM
    Saturday, August 2, 2014 5:54 PM
  • i tried it but it say i can't convert type string to int

    You parse it. e.g.

    string s = "1000.0";
    double d = double.Parse (s);

    Also, see double.TryParse();
    • Marked as answer by Kristin Xie Wednesday, August 27, 2014 3:19 AM
    Sunday, August 3, 2014 4:30 AM

All replies

  • Is this a school assignment? What is the full question? What are you expected to code?
    Saturday, August 2, 2014 4:55 PM
  • please type here complete question, what do you want from this code?
    Saturday, August 2, 2014 5:07 PM
  • ts a practice question i have done 1,2 but i don't understand or don't knw how to do no 
    1. It should then ask how much they would like to withdraw and then display the new balance and ask if they would like to make another withdrawal.
    Saturday, August 2, 2014 5:40 PM
  • char choice='y', r_choice;

    int balance, draw, cur_balance;

    balance = 10000;

    Console.WriteLine("how much would you like to withdraw");

    draw= Console.ReadLine();

    cur_balance = balance - draw;

    do{

    Console.WriteLine("Your new balance is: " + cur_balance);

    }

    Console.WriteLine("Would you like to make another withrawal? Y/N");

    while(choice == 'y' || choice == 'Y')

       Console.WriteLine("how much would you like to withdraw");


     

    • Proposed as answer by Ioana Vasilescu Saturday, August 2, 2014 6:02 PM
    • Marked as answer by Kristin Xie Wednesday, August 27, 2014 3:20 AM
    Saturday, August 2, 2014 5:54 PM
  • Hello,

    The following is not meant to provide a solution for input but food for thought and might want to explore what happens when a balance before a withdraw is possible. So with that said it's best to think about creating a class such as the one below which I am not going to go over as this as said above is food for thought. If it's of interest either learn the about it or do your own.

    using System;
    
    namespace Sample
    {
        public class Account
        {
            private decimal mWarningLevel;
            private decimal mBalance;
    
            public event AccountBalanceWarningEventHandler AccountBalanceWarningEvent;
    
            public event AccountDenyingEventHandler AccountDenialEvent;
    
            public string Number { get; set; }
    
            public string FirstName { get; set; }
    
            public string LastName { get; set; }
    
            public Account()
            {
                mWarningLevel = 10M;
            }
    
            /// <summary>
            /// Warning level is when to send an alert via our delegate
            /// </summary>
            /// <param name="warningLevel"></param>
            /// <remarks></remarks>
            public Account(decimal warningLevel)
            {
                mWarningLevel = warningLevel;
                mInsufficientFunds = mBalance <= 0M;
            }
    
            /// <summary>
            /// Current balance of account
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            public decimal Balance
            {
                get
                {
                    return mBalance;
                }
            }
    
            /// <summary>
            /// Deposit money into account
            /// </summary>
            /// <param name="amount"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public decimal Deposit(decimal amount)
            {
                mBalance += amount;
                if (mBalance < mWarningLevel)
                {
                    if (AccountBalanceWarningEvent != null)
                        AccountBalanceWarningEvent(this, new AccountBalanceWarningEventArgs(Number, mWarningLevel, mBalance));
                }
    
                if (mBalance - amount < 0M)
                {
                    mInsufficientFunds = true;
                    if (AccountDenialEvent != null)
                        AccountDenialEvent(this, new AccountDenialEventArgs(DenialReasons.InsufficientFunds));
                }
                else
                {
                    mInsufficientFunds = false;
                }
    
                return mBalance;
            }
    
            /// <summary>
            /// Withdraw from account
            /// </summary>
            /// <param name="amount"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public decimal Debit(decimal amount)
            {
                if (mBalance - amount < 0M)
                {
                    // Deny withdraw
                    mInsufficientFunds = true;
                    if (AccountDenialEvent != null)
                        AccountDenialEvent(this, new AccountDenialEventArgs(DenialReasons.InsufficientFunds));
                    return mBalance;
                }
    
                mBalance -= amount;
    
                if (mBalance < mWarningLevel)
                {
                    if (AccountBalanceWarningEvent != null)
                        AccountBalanceWarningEvent(this, new AccountBalanceWarningEventArgs(Number, mWarningLevel, mBalance));
                }
    
                return mBalance;
            }
    
            private bool mInsufficientFunds;
    
            public bool InsufficientFunds
            {
                get
                {
                    return mInsufficientFunds;
                }
            }
    
            public override string ToString()
            {
                return Balance.ToString("c2");
            }
        }
    
        public class AccountBalanceWarningEventArgs : EventArgs
        {
            private decimal Level;
            private decimal Current;
            private string mAccountNumber;
    
            public AccountBalanceWarningEventArgs(string Number, decimal warningLevel, decimal currentBalance)
            {
                this.Level = warningLevel;
                this.Current = currentBalance;
                this.mAccountNumber = Number;
            }
    
            public string AccountNumber
            {
                get
                {
                    return mAccountNumber;
                }
            }
    
            public decimal WarningLevel
            {
                get
                {
                    return Level;
                }
            }
    
            public decimal Balance
            {
                get
                {
                    return Current;
                }
            }
        }
    
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// SuspiciousActivity is not used here yet
        /// in real life banks monitor activity and
        /// may flag an account inactive if say five
        /// purchases where made in a very short time
        /// period.
        /// </remarks>
        public enum DenialReasons
        {
            InsufficientFunds,
            SuspiciousActivity
        }
    
        public class AccountDenialEventArgs : EventArgs
        {
            private DenialReasons mReason;
    
            public AccountDenialEventArgs(DenialReasons Reason)
            {
                mReason = Reason;
            }
    
            public DenialReasons Reason
            {
                get
                {
                    return mReason;
                }
            }
        }
    
        public delegate void AccountBalanceWarningEventHandler(object sender, AccountBalanceWarningEventArgs e);
    
        public delegate void AccountDenyingEventHandler(object sender, AccountDenialEventArgs e);
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Saturday, August 2, 2014 6:17 PM
  • Just simple store the input which you have after readline and convert it into double and then subtratc it from the original balance and display the remaining balance.

    double OriginalBalance = 10000;

    Console.WriteLine ("How much you'd like to Withdraw");

    double AmmountWithdraw = double.Parse(Console.ReadLine());

    OriginalAmmount -= AmmountWithdraw;

    Console.WriteLine("Your Remaining balance:" + OriginalAmmount.ToString());

    If you'd like i have a C++ project which does the same i can share that with you.

    Saturday, August 2, 2014 8:29 PM
  • i tried it but it say i can't convert type string to int

    Saturday, August 2, 2014 10:15 PM
  • i tried it but it say i can't convert type string to int

    You parse it. e.g.

    string s = "1000.0";
    double d = double.Parse (s);

    Also, see double.TryParse();
    • Marked as answer by Kristin Xie Wednesday, August 27, 2014 3:19 AM
    Sunday, August 3, 2014 4:30 AM