class - How to create an equals Method in Java for 2 credit cards -


so i'm having issue writing code such able create equals method return true if 2 credit cards equal if have same security code, company , account number.

heres code 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 card : " + 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 : "+ company); } //creates new card number public static void cardnum (creditcard card) {     int[] accountnumber = new int [16];     random generator = new random ();      (int =0; i<16; i++)          accountnumber [i] = (int)(math.random()*10);     system.out.println ("the account number 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 ();      (int =0; i<3; i++)          securitycode [i] = (int)(math.random()*10);     system.out.println ("the security code card is: " + (java.util.arrays.tostring(securitycode))+""); }  public static void setexpirationdate(int mm, int yy) {     system.out.println("the expiration date card is: " + mm + "/"+ yy + "\n"); } public static void setcreditline (int cline){     creditline =cline;  } public static void getcreditline (creditcard card) {     system.out.println( " creditline : $" + creditline); } // buys public void buywithcreditcard (double amount) {     balance = balance + amount;  } //inserts money 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 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 class utilizes creditcard class.

public class creditcarddemo { public static void main (string [] args) {      //creates cards 1 , 2     creditcard firstcard = new creditcard ();     creditcard secondcard = new creditcard ();     //calls 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 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 $5.00");     firstcard.buywithcreditcard (5.00);     system.out.println("you bought item $12.00");     firstcard.buywithcreditcard(12.00);     system.out.println("you bought item $15.00");     firstcard.buywithcreditcard(15.00);     system.out.println("you bought item $33.42");     firstcard.buywithcreditcard(33.42);      //display current balance     system.out.print("you owe: $");     creditcard.showbalance(firstcard);       //interest adds onto     if (firstcard.checkbalance () > 50.00){     system.out.println("\ninterest has been added");     firstcard.addinterest ();     system.out.print("your new balance : $");     creditcard.showbalance(firstcard);     system.out.println("");     //payment     system.out.println("you have overpaid balance.");     firstcard.paybalance (70);     system.out.print("your new balance : $");      creditcard.showbalance(firstcard);      }  } 

}

so if show me how create method in creditcard class allow me check if firstcard , secondcard, great. bunch :)

first of all, i'm not advanced in java, seems simple enough still. problem here seems security code never saved (the method getsecuritycode void , variables local) same goes company

nonetheless, here's example, assuming fixed , made method getcode returns code (as int) , method getaccountnumber, returns number (as int). (and assuming it's no problem make 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 practice make variables private , make getters, that's you.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -