Use C# to help manage your credit card debt. The Main method is to repeatedly prompt the user to enter a transaction code (char) which could be 'B' or 'b' to buy an item using the credit card, 'c' or c' to make a cash withdrawal, 'd' or d' to display the balance, and 'q' or Q' to quit. A void method called Buy An Item first applies the HST (13%) to the amount of the item and then determines if there is enough credit remaining on the card to allow the purchase to complete. If not, it will print an error message indicating that the purchase could not complete.
You can assume that the initial balance on the credit card is $0.00.Using a switch statement in the do-while loop on Main, select the appropriate action and call (invoke) the appropriate user-defined method. The Main method should continue accepting transactions until the user enters a ‘Q’ or ‘q’.
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':
amount = ReadAmount();
BuyAnItem(amount, ref ccBalance);
break;
case 'C':
case 'c':
break;
case 'P':
case 'p':
break;
case 'D':
case 'd':
break;
case 'Q':
case 'q':
break;
}
} while (code != 'Q' && code != 'q');
Console.ReadLine();
}
/// <summary>
/// A void method called BuyAnItem which accepts one call by value formal parameter of type double
/// (the amount of the item) and one call by reference formal parameter of type double (the balance on the credit card).
/// BuyAnItem first applies the HST (13%) to the amount of the item and then determines
/// if there is enough credit remaining on the card to allow the purchase to complete.
/// If there is, add the amount of the purchase (plus HST) to update the balance of the credit card.
/// If not, it will print an error message indicating that the purchase could not complete as it would put
/// the credit card over its limit. Assume that the limit on the credit card is $2000 (a constant).
/// </summary>
/// <param name="itemAmt"></param>
/// <param name="ccBalance"></param>
public static void BuyAnItem(double itemAmt, ref double ccBalance) {
double HST = 0.13 * itemAmt;
const double CARD_LIMIT = 2000.0;
if ((ccBalance + (itemAmt + HST)) <= CARD_LIMIT)
{
ccBalance += itemAmt + HST;
}
else {
Console.WriteLine("The purchase could not complete as it would put the credit card over its limit");
}
}
/// <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;
}
}
}
Comments
Leave a comment