70.3k views
1 vote
Complete the GiftCard class shown below. A GiftCard keeps track of the current balance on the card. A gift card starts with a beginning balance. A GiftCard can be used to make a purchase, funds can be added to a GiftCard, and the balance can be checked at any time. The balance can never be negative. If the amount added to the card is $100 or more, a bonus of $10 is also added.

public class GiftCard
{
// declare instance variables

// precondition: the starting balance is not negative
// postcondition: all instance variables are initialized
public GiftCard(double startBal)
{

}

// 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)
{

}
// postcondition: amount is added to balance, if the amount is at least 100 dollars,
// 10 dollars is added
public void addFunds(double amount)
{

}

// postcondition: the current balance is returned
public double checkBalance()
{

}
}

User Jacob H
by
5.0k points

1 Answer

5 votes

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;

}

}

}

Complete the GiftCard class shown below. A GiftCard keeps track of the current balance-example-1
User Lao
by
5.1k points