-2

so I'm having an issue writing my code such that i will be able to create an equals method that will return true if 2 credit cards are equal if they have the same Security code, company and account number.

Heres my code so far.

public class CreditCard {

private double balance;
public static double interestRate;
public static String personname;
public static String company;
public static double creditLine;

public CreditCard ()
{
    balance = 0;

}
public static void setIntRate (double rate)
{
    interestRate = rate;
    System.out.println("The Interest rate for this card is : " + interestRate);
}
public static double getIntRate ()
{
    return interestRate;
}

public static void setPersonName (CreditCard card ,String pName)
{
    personname = pName;
    System.out.println("Name on card: " + personname);
}

public static void setCompany (CreditCard card, String compName)
{
    company =compName;
    System.out.println("The Company name is : "+ company);
}
//creates new card number
public static void CardNum (CreditCard card)
{
    int[] accountnumber = new int [16];
    Random generator = new Random ();
     for (int i =0; i<16; i++)
         accountnumber [i] = (int)(Math.random()*10);
    System.out.println ("The Account number for this card is: " + (java.util.Arrays.toString(accountnumber))+"");

     }

//Creates new securitycode
public static void getSecurityCode (CreditCard card)
{
    int[] securitycode = new int [3];
    Random generator = new Random ();
     for (int i =0; i<3; i++)
         securitycode [i] = (int)(Math.random()*10);
    System.out.println ("The security code for this card is: " + (java.util.Arrays.toString(securitycode))+"");
}

public static void setexpirationdate(int MM, int YY)
{
    System.out.println("The expiration date for this card is: " + MM + "/"+ YY + "\n");
}
public static void setCreditLine (int cLine){
    creditLine =cLine;

}
public static void getCreditLine (CreditCard card)
{
    System.out.println( " CreditLine is : $" + creditLine);
}
// buys something
public void buyWithCreditCard (double amount)
{
    balance = balance + amount;

}
//Inserts money to reduce balance
public double paybalance (double amount)
{
    if (balance >= amount){
        balance = balance - amount;
    roundBalance();}
    else{ 
        creditLine = creditLine + (amount - balance);
        balance = 0;
        System.out.println("Your new CreditLine is: "+creditLine);
        roundBalance();
    }

    return amount;
}

// adds interest to balance
public void addInterest ()
{

    double interest = balance * getIntRate ();
    balance = balance + interest;
    roundBalance ();    
}

private void roundBalance ()
{
    balance = (double)(Math.round(balance*100))/100;

}
public double checkBalance (){
    return balance;
}
//Shows Credit Card Debt
public static void showBalance (CreditCard card)
{
    System.out.print(card.balance);
}

}

and then the class that utilizes the CreditCard Class.

public class CreditCardDemo {
public static void main (String [] args)
{

    //Creates cards 1 and 2
    CreditCard firstCard = new CreditCard ();
    CreditCard secondCard = new CreditCard ();
    //Calls for card info 1 
    System.out.println("First card Information is:");
    CreditCard.setPersonName(firstCard,"John White");
    //CreditCard.getName(firstCard);
    CreditCard.setCreditLine(600);
    CreditCard.getCreditLine(firstCard);    
    CreditCard.setCompany(firstCard,"Visa");
    CreditCard.setIntRate(0.02);
    CreditCard.CardNum(firstCard);
    CreditCard.getSecurityCode(firstCard);
    CreditCard.setexpirationdate(11, 17);
    //call for card info 2

    System.out.println("Second card Information is:");
    CreditCard.setPersonName(secondCard,"Jack Black");
    CreditCard.setCreditLine(2600);
    CreditCard.getCreditLine(secondCard);
    //CreditCard.getName(secondCard);
    CreditCard.setCompany(secondCard,"Discover");
    CreditCard.setIntRate(0.02);
    CreditCard.CardNum(secondCard);
    CreditCard.getSecurityCode(secondCard);
    CreditCard.setexpirationdate(10, 19);

    //Purchases
    System.out.println("\nYou bought something for $5.00");
    firstCard.buyWithCreditCard (5.00);
    System.out.println("You bought another item for $12.00");
    firstCard.buyWithCreditCard(12.00);
    System.out.println("You bought another item for $15.00");
    firstCard.buyWithCreditCard(15.00);
    System.out.println("You bought another item for $33.42");
    firstCard.buyWithCreditCard(33.42);

    //Display Current Balance
    System.out.print("You currently owe: $");
    CreditCard.showBalance(firstCard);


    //Interest Adds onto it
    if (firstCard.checkBalance () > 50.00){
    System.out.println("\nInterest has been added");
    firstCard.addInterest ();
    System.out.print("Your new balance is : $");
    CreditCard.showBalance(firstCard);
    System.out.println("");
    //Payment
    System.out.println("You have overpaid your balance.");
    firstCard.paybalance (70);
    System.out.print("Your new balance is : $");

    CreditCard.showBalance(firstCard);

    }

}

}

So if anyone could show me how to create a method in the CreditCard class that would allow me to check if the firstCard and secondCard, that would be great. Thanks a bunch :)

user2852630
  • 39
  • 1
  • 8
  • 1
    What have you tried? Anyway, do not forget to implement also `hashCode()` so that it verifies the `equals` contract. – SJuan76 Oct 06 '13 at 20:57
  • 2
    why did you set your methods **static** in **CreditCard** class –  Oct 06 '13 at 21:01

2 Answers2

1

If you use NetBeans, you can simply auto-generate the equals function (not sure about Eclipse). Other than that it boils down to overwriting the equals function of Object.

Make sure to check that both are of the same class and make sure to check for null. Java recommends to as well overwrite the hashCode function, however that depends on your use-case.

TwoThe
  • 13,879
  • 6
  • 30
  • 54
-1

first of all, i'm not that advanced in java, but this seems simple enough still. The problem here seems that the security code is never saved (the method getSecurityCode is void and all variables are only local) Same goes for the company

nonetheless, here's an example, assuming you fixed that and made a method getCode that returns the code (as int) and a method getAccountNumber, that returns that number (as int). (and assuming it's no problem to make those methods public)

public boolean equals(CreditCard creditCard1, CreditCard creditCard2){
if (creditCard1 == null || creditCard2 == null)
    return creditCard1 == creditCard2;
boolean equalCode = (creditCard1.getCode() == creditCard2.getCode());
boolean equalCompany = creditCard1.company.equals(creditCard2.company);
boolean equalAccountNumber = (creditCard1.getAccountNumber() == creditCard2.getAccountNumber());
return equalCode && equalCompany && equalAccountNumber;
}

It would be good practice to make the variables private and make some getters, but that's up to you.

Wouter
  • 192
  • 12
  • While this code would work, it would break the Java definition of equals and render the class unusable in sorted lists or similar. Aaand... no null check. ;) – TwoThe Oct 06 '13 at 21:14
  • Yeah, I did it since that's the way he defined the condition for creditcards to be equal. It would be a much better idea to take a different name, if that's exactly how you want to check for equal credit cards. (i'll add some null checks) – Wouter Oct 06 '13 at 21:20
  • No you didn´t ... because you used object identity-check instead equality-check... http://stackoverflow.com/questions/1692863/what-is-the-difference-between-identity-and-equality-in-oop – cljk Oct 06 '13 at 21:23
  • @cljk I didn't... break his class or answer his question correctly? I'm always assuming someone pointing out a mistake of mine is correct, but it does seem strange since i've seen equals being used in similar ways frequently. – Wouter Oct 06 '13 at 21:47
  • If you want to implement an equality-check you can use "==" for simple types (int, byte ...) but you have to use "equals" for Objects. I assume that "code" is a String which is an object - so.... would be smth. like "booelan eq = creditCard1.getCode().equals(creditCard2.getCode())" - but then you have to assume that code is always != null. Also as "TheTwo" stated a null-check would normally be needed in real life in most cases... – cljk Oct 06 '13 at 21:48
  • I want to add: "(creditCard1 == null || creditCard2 == null)" is a bit too simple, because if both fields are null, the CreditCard-instances could be equal. – cljk Oct 06 '13 at 21:51
  • wouter, i appreciate the help though. Your advice regarding my methods was very helpful. :] – user2852630 Oct 06 '13 at 22:20
  • @user2852630 could you than accept my (or just an) answer? (It would be my first accepted answer ever :p) Or are there some things still unanswered? – Wouter Oct 06 '13 at 22:36