Cannot get field to show in inspectpr box
So im supposed to create a credit card class where if i want to create an
object, i have to enter the account number( which the credit card number
and it is a long. for example: 1234123123123L) and the expiry date( which
is a String. for example "09/2013") in the parameters. Once i create the
object, the inspect box should show the account number, expiry date,
expiry month, expiry year and its validity. For now, im just concerned
about the expiry month. i have the code which extracts the month from the
date. but i cant seem to get the month to show in the inspector box; so
when i click inspect on the object, it shows the account number and expiry
date, but for the month it says zero.
how do i get the expiry month to show in the inspector box?
import java.io.*;
import java.util.Calendar;
/**
* A credit card issued by a recognized financial institution.
*
* @author Janarthanan Manoharan
* @version 2013-09-10
*/
public class CreditCard
{
// instance fields
private long accountNumber;
private String expiryDate;
private int expiryMonth;
private int expiryYear;
private boolean isValid;
/**
* Constructs a simple credit card.
*
* @param number the account number
* @param expiryDate the expiry date in the form MM/YYYY where MM is a
2-digit month
* and YYYY is a 4-digit year
*/
public CreditCard(long number, String expiryDate)
{
accountNumber = number;
this.expiryMonth = expiryMonth;
this.expiryDate = expiryDate;
// expiryYear = expiryYear;
// isValid = false;
}// end of constructor CreditCard(long number, String expiryDate)
public void monthExtract()
{
// convert "int" month to "string" month.
// String monthAsString = String.valueOf(expiryMonth);
//Extract the month from the date.
String monthAsString = expiryDate.substring(0, 2);
System.out.println(monthAsString);
// convert "string" month to "int" month.
int expiryMonth = Integer.parseInt(monthAsString);
System.out.println(expiryMonth);
// the S.O.P is to make sure that the code is actually working.
}
public void main(String [] args)
{
CreditCard credit1 = new CreditCard(5588456789672L, "09/2013");
System.out.println(credit1);
}
/**
* Returns this card's account number.
*
* @return the account number of this card
*/
public long getAccount()
{
return accountNumber;
}// end of method long getAccount()
/**
* Returns this card's expiry month.
*
* @return the 2-digit month this card expires
*/
public int getExpiryMonth()
{
return expiryMonth;
}// end of method int getExpiryMonth()
/**
* Returns this card's expiry year.
*
* @return the 4-digit year this card expires
*/
public int getExpiryYear()
{
return expiryYear;
}// end of method int getExpiryYear()
/**
* Returns true if this card is valid, else false.
*
* @return true if this card is valid, else false
*/
public boolean isValid()
{
return isValid;
}// end of method boolean isValid()
/**
* Returns a string representation of this credit card.
*
* @return a string representing this credit card
*/
public String toString()
{
return "["
+ "Account Number: " + accountNumber
+ "\n Expiry Date: " + expiryDate
+ "\n Exipry Month: " + expiryMonth
+ "]";
}// end of method0. String toString()
}// end of class CreditCard
No comments:
Post a Comment