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;
}

Edited 1 Year Ago by moe11223

Hi

I'm not a Java Programmer so can't comment on the specifics of your code. However, you should not have any UI code in such a class as it is then tightly coupled to that particular UI.

UI operations should be done outside of this class and you should interact with instances of this class to set and read data.

Edited 1 Year Ago by DaveAmour

well ... I would not recommend handing this in just yet. An example:

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");
         }
   }

let's discuss what you do here.

you have a SetCardNumber (following naming conventions, this should be setCardNumber (or setCardnumber, depending on whether you consider it to be card number or cardnumber).

You pass one parameter, c, which, I assume, is the number to set.
Then you call:

System.out.print("Enter card number:");
          String cardNum = input.next();

and this is where it all goes wrong. Why do you read a card number here ? What do you need this String cardNum for ?

To verify that c is correct ? There is no relation between cardNum and c.

if (cardNum.length() >= 13 && cardNum.length() <= 16)
          {
             System.out.println("Valid Card number");
             cardNumber = c;
          }

All you have verified here is the length of the String cardNum. If you were to enter here: "abcdefghijklmno", your validation would consider it to be a valid cardnumber, which, it obviously isn't.
Then you print "Valid card number" and assign the value of c to cardNumber. (Even if the value of c would be -15, which is not a valid card number)

         else 
         {
             System.out.println("Error: Invalid card number");
         }

Here, all you say is that the length of the cardNum String doesn't meet the requirements.

You don't need to ask a new String, you need to validate it on c.

Problem number two: in order to validate the String, you need to check that c is:

c <= 9999999999999999
AND
c => 1000000000000

Unfortunately, even the smallest of these values, does exceed the limitations of an int, so, you'll need an other datatype. If you want to stick to primitive types, float would be sufficient. However, since there is additional validation possible on cardnumbers, you may also want to create a seperate datatype and implement it all in there (just a thought).

But, are you sure you were supposed this in Java ? Java doesn't have functions, it has methods. Are you using another language in your course, or is this just an oversight of the lecturer?

This article has been dead for over six months. Start a new discussion instead.