如何将变量从一个方法转移到另一个方法而不将其存储在Java中的类中?

时间:2015-03-18 03:05:49

标签: java

我有一个有三种方法的课程,用于确定信用卡是否有效,信用卡类型是什么,以及用x代替除了最后四位数以外的所有数字来显示数字。这是一个班级,我的老师不希望我在这个班级存储信用卡的变量。因此,在我的其他课程中,我必须在方法中询问信用卡号并将其存储在该方法中。但是,我想要第二种方法来调用显示信用卡类型和显示格式化数字的方法,但我不知道如何将变量从第一种方法转移到下一种方法。请帮忙。

这是我的readCreditCard方法,要求输入信用卡号并确定它是否有效:

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


}

我想要一个名为printCreditCard的方法,它允许我使用readCreditCard方法中的变量creditCardNum,以便我可以在CreditCard类中使用我的其他两个方法,但我不知道如何解决这个问题。 / p>

以下是CreditCard类的内容:

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;




}

}

2 个答案:

答案 0 :(得分:1)

使用名为creditCardNum等的成员变量创建一个bean。该方法将此bean返回给哪个方法。

答案 1 :(得分:0)

如果我正确理解了您的问题,我会看到两个选项

选项1:如果信用卡号是您想要的单个值,则返回信用卡号。

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


}

选项2:如果您想从方法传递/返回多个值,则传递信用卡对象(bean / model)将解决您的问题。

以下是示例。

 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;

}