gpt4 book ai didi

java - 在 main 方法中调用类的方法

转载 作者:行者123 更新时间:2023-12-02 06:24:43 25 4
gpt4 key购买 nike

我对这个有点卡住了。我正在编写一个包含两个类的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!");
}

注意:平衡 setter 正在工作,因为它在测试程序的前面被调用...

非常感谢任何帮助!!!

帐户类别的完整代码:

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

最佳答案

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

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

类似于:

余额 = account.getBalance();

关于java - 在 main 方法中调用类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20666061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com