简体   繁体   中英

How can I transfer a variable from one method to another without storing it in a class in Java?

I have a class with three methods in it for determining whether a credit card is valid or not, what the credit card type is, and for displaying the number with x's in place of all but the last four digits. This is for a class and my teacher does not want me to store the variable for the credit card in this class. So, in my other class I have to ask for the credit card number in a method and store it in that method. However, I want a second method that calls the methods for displaying the credit card type and displaying the formatted number, but I do not know how to transfer the variable from the first method to the next. Please help.

This is my readCreditCard method that asks for the credit card number and determines whether it is valid:

public void readCreditCard(CreditCard creditCard)
{
    System.out.print("\nPlease enter your Credit Card Number: ");
    String creditCardNum = keyboard.next();

    while (creditCard.isCardValid(creditCardNum) == false)
    {
        System.out.println("Invalid credit card number" + creditCardNum);
        System.out.println("Please try again.");

        System.out.print("\nPlease enter your Credit Card Number: ");
        creditCardNum = keyboard.next();
    }


}

I want a method called printCreditCard that allows me to use the variable creditCardNum from the readCreditCard method so that I can use my other two methods in the CreditCard class, but I'm not sure how to go about this.

Here is the contents of the CreditCard class:

public boolean isCardValid(String creditCard)
{
    boolean valid;
    String singleDigitPrefix = creditCard.substring(0, 1);
    String doubleDigitPrefix = creditCard.substring(0, 2);

    if ((creditCard.length() == 13 || creditCard.length() == 16) && singleDigitPrefix.equals("4"))
    {
        valid = true;
    }
    else if (creditCard.length() == 16 && (doubleDigitPrefix.equals("51") || 
            doubleDigitPrefix.equals("52") || doubleDigitPrefix.equals("53") ||
            doubleDigitPrefix.equals("54") || doubleDigitPrefix.equals("55")))
    {
        valid = true;
    }
    else if (creditCard.length() == 15 && doubleDigitPrefix.equals("37"))
    {
        valid = true;
    }
    else
    {
        valid = false;
    }

    return valid;
}

public String getCardType(String creditCard)
{
    String cardType = null;

    String singleDigitPrefix = creditCard.substring(0, 1);
    String doubleDigitPrefix = creditCard.substring(0, 2);

    if ((creditCard.length() == 13 || creditCard.length() == 16) && singleDigitPrefix.equals("4"))
    {
        cardType = "Visa";
    }
    else if (creditCard.length() == 16 && (doubleDigitPrefix.equals("51") || 
            doubleDigitPrefix.equals("52") || doubleDigitPrefix.equals("53") ||
            doubleDigitPrefix.equals("54") || doubleDigitPrefix.equals("55")))
    {
        cardType = "MasterCard";
    }
    else if (creditCard.length() == 15 && doubleDigitPrefix.equals("37"))
    {
        cardType = "American Express";
    }
    else
    {

    }

    return cardType;
}

public String maskCardNumber(String creditCard)
{
    String formattedNum = null;

    if (getCardType(creditCard).equals("Visa"))
    {
        if (creditCard.length() == 13)
        {
            formattedNum = "XXXXXXXXX" + creditCard.substring(8, 13);
        }
        else
        {
            formattedNum = "XXXXXXXXXXXX" + creditCard.substring(11, 16);
        }
    }
    else if (getCardType(creditCard).equals("MasterCard"))
    {
        formattedNum = "XXXXXXXXXXXX" + creditCard.substring(11, 16);
    }
    else if (getCardType(creditCard).equals("American Express"))
    {
        formattedNum = "XXXXXXXXXXX" + creditCard.substring(10, 15);
    }
    else;

    return formattedNum;




}

}

创建一个具有称为creditCardNum等成员变量的bean,该方法将该bean返回到需要的方法。

If i have understand your question correctly i see two option

Option 1: Return credit card number if it is single value you want.

public int readCreditCard(CreditCard creditCard)
{
    System.out.print("\nPlease enter your Credit Card Number: ");
    String creditCardNum = keyboard.next();

    while (creditCard.isCardValid(creditCardNum) == false)
    {
        System.out.println("Invalid credit card number" + creditCardNum);
        System.out.println("Please try again.");

        System.out.print("\nPlease enter your Credit Card Number: ");
        creditCardNum = keyboard.next();
    }


}

Option 2: If you want to pass/return multiple values from method, passing the credit card object (bean/model) will solve your problem.

Here is the example.

 public class CreditCardBean{


        private String creditCardNumber;
        private Date expirationDate;


        public String getCreditCardNumber() {
              return creditCardNumber;
        }

        public void setCreditCardNumber(String creditCardNumber) {
            this.creditCardNumber = creditCardNumber;
        }

        public Date getExpirationDate() {
           return expirationDate;
        }

        public void setExpirationDate(Date expirationDate) {
          this.expirationDate = expirationDate;
       }

    }




public CreditCardBean readCreditCard(CreditCard creditCard)
{
    CreditCardBean bean = new CreditCardBean();
    System.out.print("\nPlease enter your Credit Card Number: ");
    String creditCardNum = keyboard.next();

    while (creditCard.isCardValid(creditCardNum) == false)
    {
        System.out.println("Invalid credit card number" + creditCardNum);
        System.out.println("Please try again.");

        System.out.print("\nPlease enter your Credit Card Number: ");
        creditCardNum = keyboard.next();
        bean.setCreditCardNumber(creditCardNum);

    }
// Set other details which you want to pass.
// bean.setExpirationDate(<setDate here>); 
return bean;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM