在main方法中使用另一种方法

时间:2015-02-03 01:16:36

标签: java

大家好,我需要一些帮助。首先让我提供一些代码:

public class AccountTest {
public static void main(String[]args){
    Account account1 = new Account("Jane Green", 50.00);
    Account account2 = new Account("John Blue", -7.53);

    System.out.printf("%s balance: $%.2f%n", account1.getName(), account1.getBalance); //13-14 
    System.out.printf("%s balance: $%.2f%n", account2.getName(), account2.getBalance);

    Scanner input = new Scanner(System.in);

    System.out.print("Enter deposit amount for account1: ");
    double depositAmount = input.nextDouble();
    System.out.printf("%nadding %.2f to account1 balance%n%n", depositAmount);
    account1.deposit(depositAmount);

    System.out.printf("%s balance: $%.2f%n", displayAccount(account1)); //28-29
    System.out.printf("%s balance: $%.2f%n", displayAccount(account2)); //30-31

    System.out.print("Enter deposit amount for account2: ");
    double depositAmount2 = input.nextDouble(); //had to change depositAmount to depositAmount2 so it would compile (it would not allow a duplicate), it still displays just as the example in the book did however
    System.out.printf("%nadding %.2f to account2 balance%n%n", depositAmount2);
    account2.deposit(depositAmount2);

    System.out.printf("%s balance: $%.2f%n", displayAccount(account1)); //40-41
    System.out.printf("%s balance: $%.2f%n", displayAccount(account2)); //42-43
}
public static void displayAccount(Account accountToDisplay){
    System.out.printf("%s balance: $%.2%n", accountToDisplay.getName(), accountToDisplay.getBalance());
}
}

好的,所以我的目标是获取它,以便displayAccount方法显示其名称和平衡,如上所示(使用给定的数据),然后可以在System.out.printf语句中使用,而不是.getName和。 getBalance方法。我的意思是,我不是使用这两种方法,而是使用.displayAccount的account1(或2)(因此它将是account1.displayAccount,并产生与两种方法相同的结果)。请帮忙,因为我被困在如何去做这件事。

2 个答案:

答案 0 :(得分:1)

听起来你正在寻找这样的东西:

public class AccountTest {

  public static void main(String[] args) {
    Account account1 = new Account("Jane Green", 50.00);
    Account account2 = new Account("John Blue", -7.53);

    displayAccount(account1);
    ...
  }

  public static void displayAccount(Account accountToDisplay) {
    System.out.printf("%s balance: $%.2f%n",
      accountToDisplay.getName(), accountToDisplay.getBalance());
}

答案 1 :(得分:0)

这是我创建的一个简单的帐户类,包含两个变量以及setter和getter方法。我还写了/重写了toString方法。现在为什么toString?它用于获取对象的字符串表示。在这种特殊情况下,将覆盖toString方法,以便显示帐户名称和余额。

public class Account {
int balance;
String name;

public int getBalance() {
    return balance;
}
public void setBalance(int balance) {
    this.balance = balance;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {

return "Name "+this.name+" "+"Balance "+this.balance;
}

**UPDATED CODE**
public String displayAccount(){
    return "Name "+this.name+" "+"Balance "+this.balance;
}


}

客户代码

package com.StackOverFlow.Doubts4;

public class Dbt7 {
public static void main(String[] args) {
Account acc1= new Account();
Account acc2= new Account();
acc1.name="accountHolder1 ";
acc1.balance=111;
acc2.name="accountHolder2 ";
acc2.balance=222;

System.out.println(acc1);
System.out.println(acc2);

**UPDATED**
Dbt7.displayAccount(acc1);
Dbt7.displayAccount(acc2);    
}

**UPDATED**
public static void displayAccount(Account accountToDisplay) {
    System.out.printf("\n\nAccount name "+ accountToDisplay.getName()+ "\nAccount Balance "+ accountToDisplay.getBalance());
}

}

代码的输出将是

Name accountHolder1  Balance 111
Name accountHolder2  Balance 222

我希望这符合您的要求。如果您需要更多信息,请告诉我。

注意:我故意参加了另一课。

<强>更新