Credit Card Class Write a class named CreditCard that has (at least) the followi
ID: 3715870 • Letter: C
Question
Credit Card Class
Write a class named CreditCard that has (at least) the following member variables:
name. A String that holds the card holder’s name.
cardNumber. A field that holds the credit card number.
balance. A double that stores the current credit card balance.
spendingLimit. A double that stores the spending limit of the card holder.
additional fields that you can think of.
In addition, the class should have (at least) the following member methods:
Constructor. The constructor should accept the card holder’s name and card number and assign these values to the object's corresponding member variables. The constructor should initialize the spending limit to $2,000 and the balance to $0.
Accessors. Appropriate accessor methods should be created to allow values to be retrieved from an object's member variables.
purchase. This method should add the amount specified as a parameter to the balance member variable each time it is called, assuming the purchase does not cause the balance to exceed the customer’s spending limit.
increaseSpendingLimit. This method should increase the spending limit by $50 each time it is called.
payBill. This method should reset the balance to 0.
toString. to render the state of a CreditCard as a String.
Input validation: Whenever a credit card number is modified, verify that it is of reasonable length.
Demonstrate the class in a program that creates a CreditCard object and allows the user to change and view the state of the credit card with a menu driven program.
View Card Information.
Purchase an Item: ask the user the purchase amount and increase the card balance accordingly.
Pay Bill: call payBill method to set the balance to 0.
Increase Spending Limit: ask the user how much the spending limit should be, and call the increaseSpendingLimit method the appropriate number of times.
Keep in mind: input and output should not be performed directly in the CreditCard class. All input and output to the user should be done in the main method (or other methods that are in a separate class) to increase flexibility of the classes we will design later on in this project.
Explanation / Answer
Let me know if you have any doubt.
--------------------------- CreditCard.java ---------------------------
import java.util.*;
public class CreditCard
{
String name;
String cardNumber;
double balance;
double spendingLimit;
// constructor
CreditCard(String name, String cardNumber)
{
this.name = name;
// check if the length of card number is 16
if(cardNumber.length() == 16)
this.cardNumber = cardNumber;
else
{
System.out.println("Card Number is invalid. It should be 16 character long!");
System.exit(0);
}
this.balance = 0.0;
this.spendingLimit = 2000.0;
}
// accessor methods
public String getName()
{
return this.name;
}
public String getCardNumber()
{
return this.cardNumber;
}
public double getBalance()
{
return this.balance;
}
public double getSpendingLimit()
{
return this.spendingLimit;
}
// make a purchase
public void purchase(double amount)
{
this.balance += amount;
}
// increase Spending Limit by $50 each time it is called
public void increaseSpendingLimit()
{
this.spendingLimit += 50.0;
}
// reset balance to $0
public void payBill()
{
this.balance = 0;
}
public String toString()
{
String ans = "Name : " + this.name + " ";
ans += "Card Number : " + this.cardNumber + " ";
ans += "Balance : " + this.balance + " ";
ans += "Spending Limit : " + this.spendingLimit + " ";
return ans;
}
}
--------------------------- CardTest.java ---------------------------
public class CardTest
{
public static void main(String[] args)
{
// create a credit card object
CreditCard ob = new CreditCard("Chandler Bing", "1234567812345678");
System.out.println(ob);
System.out.println(" After purchasing item of $100 ... ");
ob.purchase(100);
System.out.println(ob);
System.out.println(" After paying bill ... ");
ob.payBill();
System.out.println(ob);
System.out.println(" After Increasing Spending Limit ... ");
ob.increaseSpendingLimit();
System.out.println(ob);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.