-2

所以我在编写代码时遇到问题,这样我就可以创建一个 equals 方法,如果 2 张信用卡具有相同的安全码、公司和帐号,则该方法将返回 true。

到目前为止,这是我的代码。

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

}

然后是使用 CreditCard 类的类。

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

    }

}

}

因此,如果有人能告诉我如何在 CreditCard 类中创建一个方法,让我可以检查 firstCard 和 secondCard,那就太好了。非常感谢:)

4

2 回答 2

1

如果您使用 NetBeans,您可以简单地自动生成 equals 函数(不确定 Eclipse)。除此之外,它归结为覆盖 的equals函数Object

确保检查两者是否属于同一类并确保检查 null。Java 建议也覆盖 hashCode 函数,但这取决于您的用例。

于 2013-10-06T21:05:00.267 回答
-1

首先,我在 java 方面不是那么先进,但这似乎仍然很简单。这里的问题似乎永远不会保存安全代码(getSecurityCode方法是无效的,所有变量都是本地的)公司也是如此

尽管如此,这里有一个示例,假设您修复了该问题并创建了一个返回代码(作为 int)的方法 getCode 和一个返回该数字(作为 int)的方法 getAccountNumber。(假设公开这些方法没有问题)

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

将变量设为私有并创建一些 getter 是一种很好的做法,但这取决于您。

于 2013-10-06T21:11:33.723 回答