I am new to OOP, i am not sure if this is the correct approach or not. Need some insight.
Assignment 3
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.
Bonus: additional fields that you can think of.
In addition, the class should have the following member functions:
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 functions should be created to allow values to be retrieved from an object's member variables.
purchase. This function should add the amount specified as a parameter to the balance member variable each time it is called.
increaseSpendingLimit. This function should add 500 to the spendingLimit member variable each time it is called.
payBill. This function should reset the balance to 0.
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 function the appropriate number of times.
[CODE]import java.io.*;
import java.util.Scanner;
public class CreditCard
{
Scanner input = new Scanner(System.in);
//data members
private String holderName;
private int cardNumber;
private int accountBalance;
private double spendingLimit;
private int accountLevel;
//constructors
public CreditCard()
{
cardNumber=937282;
accountBalance = 0;
spendingLimit = 2000;
}
public CreditCard(String name, int card, int balance, double limit, int level)
{
holderName = name;
cardNumber= card;
accountBalance = balance;
spendingLimit = limit;
accountLevel =level;
}
//accessor methods
public String getName()
{
return holderName;
}
public int getCreditCardNumber()
{
return cardNumber;
}
public int getBalance()
{
return accountBalance;
}
public double getSpendingLimit()
{
return spendingLimit;
}
public double getAccountLevel()
{
return accountLevel;
}
//mutator methods
public void SetCardNumber(int c)
{
System.out.print("Enter card number:");
String cardNum = input.next();
if (cardNum.length() >= 13 && cardNum.length() <= 16)
{
System.out.println("Valid Card number");
cardNumber = c;
}
else
{
System.out.println("Error: Invalid card number");
}
}
public void Purchase(int cost)
{
if ((spendingLimit-cost)>0)
{
accountBalance += cost;
}
else
System.out.println("Charge Denied");
return spendingLimit;
}
public void increaseSpendingLimit(int s)
{
spendingLimit = (spendingLimit + 500);
spendingLimit = s;
}
public void payBill(int p)
{
accountBalance = 0;
accountBalance = p;
}
public void SetAccountLevel(int l)
{
if (spendingLimit > 2000)
accountLevel =1;
else if (spendingLimit > 3000)
accountLevel =2;
else if (spendingLimit > 4000)
accountLevel =3;
}
return accountLevel;
}