Answer:
I solved this program using C# and Visual Studio. The output of this program is given attached image. I initialized the start balance with 300$.
the code of this program is given in explanation section
Step-by-step explanation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftCard
{
class Program
{
static void Main(string[] args)
{
Program a = new Program();
//Console.WriteLine("Please enter the starting balance");// you can uncomment this statement if you enter startbalance at run time
//double startingBalance = Convert.ToDouble(Console.ReadLine());//initializing starting balance at run time
double startingBalance = 300; //let suppose we enter 300
a.GiftCard(startingBalance);//balance is set to starting blance value i.e. enter by user
Console.ReadLine();// here we pause, and check variables values from GiftCard constructor
a.spendFunds(100);// here we assume to spend 100$
Console.ReadLine();//here we check the spendFund function and about its values
a.addFunds(600);// here we enter fund 600$
Console.ReadLine();
a.checkBalance(); //here we are checking balance;
}
// declare instance variables
// precondition: the starting balance is not negative
// postcondition: all instance variables are initialized
private double balance=0;// keep the balance
private double purchase;//how much you spend or purchasing value
public void GiftCard(double startBal)
{
balance = startBal;
if (balance < 0)
{
Console.WriteLine("Balance is negative, Please enter positive balance");
balance = 0;
}
else
{
balance = startBal;
Console.WriteLine(balance);
}
}
// postcondition: amount is subtracted from balance
// if there are insufficient funds, the balance is set to zero and a message
// is displayed to indicate how much of the transaction is still owed
public void spendFunds(double amount)
{
purchase = balance - amount;// suppose you spend 300 that mean you have purchased of 300
if (purchase <0) //if your spending is more than your balance
{
double owed = amount - balance;// how much amount you need more for this transaction
balance = 0;
Console.WriteLine("The transaction is still owed "+owed+"$");
}
else if(purchase==0) // if after doing purchase, your balance is equal to zero
{
Console.WriteLine("you have no balance after this transaction");
}
else
{
balance = balance - amount;// if your purchase is less than the balance
Console.WriteLine("your balance after spending " + amount +"$ is " + balance+"$");
}
}
// postcondition: amount is added to balance, if the amount is at least 100 dollars,
// 10 dollars is added
public void addFunds(double amount)
{
if (amount >= 100)// if you add fund more than 100$
{
balance = balance + amount + 10; //then add the amount + 10$ as bonus into already existing balance
Console.WriteLine("balance is after adding funds i.e " + amount +"$ is "+ balance+"$");
}
else// if added fund is less than 100$
{
balance = balance + amount;
}
}
// postcondition: the current balance is returned
public double checkBalance()//check balance anytime
{
Console.WriteLine("balance is "+balance+"$");
Console.ReadLine();
return balance;
}
}
}