Answer to Question #261590 in C# for Anino

Question #261590

The Main method is to repeatedly prompt the user to enter a transaction code (char). The codes could be 'B' or 'b' to buy an item using the credit card, 'c' or C' to make a cash withdrawal, 'p' or P for a payment, 'D' or D' to display the balance and 'q' or Q to quit.

There is a void function called CashWithdrawal which accepts one call by value formal parameter of type double (the amount of the cash withdrawal, plus a $3.50 service charge) to add to the credit card balance. If this amount (use a constant) that does not exceed the credit cards credit card limit ($2000), print an error message that the transaction could not be completed.The method header should look like: public static void BuyAnItem(double itemAmt, ref double ccBalance)


1
Expert's answer
2021-11-05T17:22:09-0400
using System;
using System.Collections.Generic;


namespace App
{
    
    class Program
    {
        static void Main(string[] args)
        {
            char code = ' ';
            double amount;
            double ccBalance = 0;
            do
            {
                Console.WriteLine("'B' or 'b' - to buy an item");
                Console.WriteLine("'C' or c' - to make a cash withdrawal");
                Console.WriteLine("'P' or p - for a payment");
                Console.WriteLine("'D' or 'd' - to display the balance");
                Console.WriteLine("'Q' or 'q' - to quit");
                Console.Write("Enter a transaction code: ");
                code = Console.ReadLine()[0];


                switch (code)
                {
                    case 'B':
                    case 'b':
                        break;
                    case 'C':
                    case 'c':
                        amount = ReadAmount();
                        Cashwithdrawal(amount, ref ccBalance);
                        break;
                    case 'P':
                    case 'p':
                        break;
                    case 'D':
                    case 'd':
                        break;
                    case 'Q':
                    case 'q':
                        break;


                }
            } while (code != 'Q' && code != 'q');


            
        }
        /// <summary>
        /// A void function called CashWithdrawal which accepts one call by value formal parameter of type 
        /// double (the amount of the cash withdrawal) and one call by reference formal parameter of type
        /// double (the credit card balance). CashWithdrawal adds the amount of the cash withdrawal 
        /// plus a $3.50 service charge (use a constant) to the credit card balance if this amount
        /// (including service charge) does not exceed the credit card limit ($2000). 
        /// If the amount of the cash withdrawal plus the service charge does exceed the credit limit, 
        /// print an error message that the cash withdrawal transaction could not be completed as 
        /// the limit on the credit card would be reached.
        /// </summary>
        /// <param name="cashAmt"></param>
        /// <param name="ceBalance"></param>
        public static void Cashwithdrawal(double cashAmt, ref double ceBalance)
        {
            const double SERVICE_CHARGE=3.5;
            const double CARD_LIMIT=2000.0;
            if ((ceBalance + (cashAmt + SERVICE_CHARGE)) <= CARD_LIMIT)
            {
                ceBalance += cashAmt + SERVICE_CHARGE;
            }
            else
            {
                Console.WriteLine("The the cash withdrawal transaction could not be completed as the limit on the credit card would be reached.");
            }
        }
        
        /// <summary>
        /// A method call ReadAmount which takes no formal parameters but returns a non-negative value of 
        /// type double representing the cost of an item, the amount of cash withdrawn, or the amount of a payment. 
        /// ReadAmount is to be invoked by Main() when the user chooses to buy an item (B' or 'b'), make a cash withdrawal ('c' or 'CP), 
        /// or make a payment (*p' or 'P'). The ReadAmount method is to prompt the user to enter an amount,
        /// Ovalidate that it is non-negative with a loop, and then return the non-negative value.
        /// </summary>
        /// <returns></returns>
        public static double ReadAmount() {
            double amount=-1;
            while (amount < 0) {
                Console.Write("Enter amount: ");
                if (!double.TryParse(Console.ReadLine(), out amount) || amount<0)
                {
                    Console.WriteLine("Wrong amount. Try again.");
                    amount = -1;
                }
            }
            return amount;
        }
       
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS