当前位置: 首页 > 知识库问答 >
问题:

从 main 方法中的类调用方法

堵凯
2023-03-14

我在这个上面有点卡住了。我正在编写一个包含两个类的java程序,然后是一个测试程序来测试类中的方法。我被困在主方法中调用下面的两个方法上。所有的类文件(测试程序类和另外两个类)都在编译,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个答案

楚俊迈
2023-03-14

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

壤驷高洁
2023-03-14

您的数字可能看起来总是相同的,因为在打印之前未更新局部变量。

确保在调用 System.out.printf(“%.2f%n”,balance);之前更新余额值。

类似的东西:

< code > balance = account . get balance();

 类似资料:
  • 问题内容: 我有 但后来当我打电话从通过运行以下命令在命令行上,JAVA抱怨说,你不能调用从静态功能的方法。 所以我的问题是:如何从主方法调用方法,如果不可能,使用java命令从命令行运行程序后,有哪些替代策略可以调用方法。 问题答案: 您只能针对类的实例调用类似的实例方法(顺便说一下,这是一个非法的方法名称): 另外,如果对您的设计有效,则也使它成为静态。

  • 问题内容: 尝试在Parent类中创建1个接口和2个具体类。这将使封闭类成为内部类。 现在,我真的不确定如何在静态main()方法中创建C类的对象并调用C类的call()方法。现在我遇到了问题: 问题答案: 这里的内部类不是静态的,因此您需要创建一个外部类的实例,然后调用new, 但是在这种情况下,您可以将内部类设为静态, 那就可以使用了

  • Java,我们可以从另一个类调用main()方法吗?例如,ClassA应该在命令行上运行,因为它定义了main(String[]args)方法。 我想把这个叫做ClassA,在ClassB里面。是否初始化类的新对象,如 或者是否有一种方法可以在ClassB中传递ClassA的命令行选项。 谢啦

  • 日志输出: 线程“Thread-13”java.lang.noClassDeffounder中的异常错误:org/springframework/boot/springapplication at hello.application.main(application.java:11)at org.clinton.openfire.plugin.fetchnewsplugin$1.run(fetch

  • 问题内容: 我需要使用反射从另一个主要方法调用Java类的主要方法。 必须使用反射,以消除被调用主类的编译时依赖性。直截了当的方法并没有产生效果,因为它只识别“公共”和“非静态”方法。有什么建议吗? 问题答案: 不应比调用任何其他函数更复杂: 但是我真的看不到有什么用途,只要您不使用特定的代码路径,就可以买到该程序,而无需链接另一个程序,这是它唯一能为您带来的好处,但是如果这是您需要的,在这里走

  • 我的作业是为一家正在补货的珠宝店计算税金和附加费,我遇到了一点小麻烦。我三次使用一种叫做calcExtraTax的方法来计算劳动率以及州税和联邦税。然后,我需要获取该方法的每个实例的结果,并将值传递给我的main方法中的适当变量。这是我的代码现在的样子(显然不完整): 我想弄清楚的是,我还需要在我的第二种方法中添加什么,以便每次都能够根据公式中使用的税率变量将结果传递到不同的税收成本变量中。