从main方法中的类调用方法

时间:2013-12-18 18:48:56

标签: java class methods

我有点卡在这个上面。我正在编写一个带有两个类的java程序,然后是一个测试程序来测试类中的方法。我坚持在main方法中调用下面的两个方法。所有类文件(测试程序类和其他两个类)都在编译,IDE没有给我任何错误消息,计算就没有发生......

- 主要方法代码:

//Call debit method
System.out.println("Please enter amount to be debited"); 
double amount=input.nextDouble(); 
account.debit(amount);   
System.out.printf("%.2f%n",balance); 

//Call credit method
System.out.println("Please enter amount to be credited"); 
amount=input.nextDouble(); 
account.credit(amount);   
System.out.printf("%.2f%n",balance);    

- 帐户类代码:

//Method for crediting account balance 
public void credit (double amount) {
  double newBalance=this.balance+amount;
  this.setBalance(newBalance);  
}

//Method for debiting account balance
public void debit (double amount) {
  if (amount<balance) {
  double newBalance=this.balance-amount;
  this.setBalance(newBalance); 
  } else {
  System.out.println("Insufficient Funds!"); 
} 

注意:天平设定器正在工作,因为它在测试程序中早先被调用...

任何帮助都非常感谢!!!

完整的帐户类代码:

public class Account {
private int accountId; 
private String accountName; 
private String accountAddress; 
private double balance; 
private Bank bank; 

//Default Constructor
public Account () {
}

//Getters
public int getAccountId () {
  return accountId; 
}

public String getAccountName () {
  return accountName; 
}

public String getAccountAddress () {
  return accountAddress; 
}

public double getBalance () {
  return balance; 
}

public Bank getBank () {
  return bank; 
}

//Setters
public void setAccountId (int accountId) {
  if (accountId <=10000000 || accountId >=99999999) {
    System.out.println("Invalid Account Id");
  } else {  
    this.accountId=accountId;
  }
}

public void setAccountName (String accountName) {
  if (accountName.length()>=10) {
    System.out.println("Too Long"); 
  } else {
    this.accountName=accountName;
  }
}

public void setAccountAddress (String accountAddress) { 
  this.accountAddress=accountAddress;
}

public void setBalance (double balance) {
  if (balance<0.0) {
    System.out.println("Invalid Balance");
  } else {  
    this.balance=balance; 
  }
}

public void setBank (Bank bank) {
  this.bank=bank; 
}

//Constructor to initialize accountId, accountName, accountAddress and Bank
public Account (int accountId, String accountName, String accountAddress, Bank bank) {
  this.setAccountId(accountId); 
  this.setAccountName(accountName); 
  this.setAccountAddress(accountAddress); 
  this.setBank(bank); 
}

//Method to print out account category based on balance
public void printAccountCategory () {
  if (balance<100.0) {
    System.out.println("Challenged Account"); 
  } else if (balance>=100.0 && balance<999.9) {
    System.out.println("Standard Account"); 
  } else if (balance>=1000.0 && balance<9999.9) {
    System.out.println("Promising Account"); 
  } else if (balance>=10000.0 && balance<99999.9) {
    System.out.println("Gold Star Account"); 
  } else {
    System.out.println("Super Duper Account"); 
  }
}

//Method to project balance based on compound interest and the number of years required
//Note: I took the formula using n (number of times the interest is compounded per year) as 1
public double projectNewBalance (int numberYears) {
  if (numberYears>0) {
    double interest=1;
    for (int i=1; i<=numberYears; i++) { 
      interest*=(1.0+bank.getInterestRate());  
    }
      double newBalance=balance*interest; 
      return newBalance;
    } else if (numberYears<0) {
      System.out.println("Invalid Value");
    } else {
      return balance;
    }
    return balance; 
  }

  //Method for crediting account balance
  public void credit (double amount) {
    double newBalance=this.balance+amount;
    this.setBalance(newBalance);  
  }

  //Method for debiting account balance
  public void debit (double amount) {
    if (amount<balance) {
      double newBalance=this.balance-amount;
      this.setBalance(newBalance); 
    } else {
      System.out.println("Insufficient Funds!"); 
    }
  }

  //toString method
  public String toString () {
    return "Account Id: "+accountId+", Account Name: " + accountName + ", Account Address: "+accountAddress+", Balance: "+balance+", Bank Details: "+bank.toString()+".";  
  }
}

主要方法完整代码:

import java.util.Scanner;

public class BankAccountTest {
  public static void main (String [ ] args) {
  //Create an instance of the Bank class
  Bank bank = new Bank ("WIT Bank", "Paddy Snow", 0.045);  

  //Create instance of Scanner class
  Scanner input=new Scanner(System.in);

  //Prompt user to input data to create an account
  System.out.println("Please enter an Account ID"); 
  int accountId=input.nextInt();

  System.out.println("Please enter an Account Name"); 
  String accountName=input.next(); 

  System.out.println("Please enter an Account Address"); 
  String accountAddress=input.next();

  //Create instance of the Account class
  Account account = new Account (accountId, accountName, accountAddress, bank); 

  //Print out details of account class
  System.out.println(account); 

  //Prompt user to enter balance for the account
  System.out.println("Please enter account balance"); 
  double balance=input.nextDouble(); 
  account.setBalance(balance); 

  //Use printAccountCategory method
  account.printAccountCategory(); 

  //Call projectNewBalance method
  // Note: Method tested with value of 10 years as mentioned in spec, 
  //    but user input also included I think it is more appropriate for the functionality of the program   
 //    int numberYears=10; 
 //    double newBalance1=account.projectNewBalance(numberYears); 
 //    System.out.println(""+newBalance1); 
   System.out.println("Please enter number of years"); 
   int numberYears=input.nextInt(); 
   double newBalance=account.projectNewBalance(numberYears); 
   System.out.printf("%.2f%n",newBalance);

    //Call debit method
    System.out.println("Please enter amount to be debited"); 
    double amount=input.nextDouble(); 
    account.debit(amount);   
    System.out.printf("%.2f%n",balance); 

    //Call credit method
    System.out.println("Please enter amount to be credited"); 
    amount=input.nextDouble(); 
    account.credit(amount);   
    System.out.printf("%.2f%n",balance);     
   }
}

2 个答案:

答案 0 :(得分:1)

您的数字可能看起来总是一样,因为在打印之前没有更新本地变量。

请确保在致电balance之前更新System.out.printf("%.2f%n",balance);的值。

类似的东西:

balance = account.getBalance();

答案 1 :(得分:1)

不应该打印您创建的对象的余额而不仅仅是“平衡”: 的System.out.println(account.getBalance())?

相关问题