gpt4 book ai didi

java - 使用修改器/访问器读取文件并写入 cmd 窗口

转载 作者:行者123 更新时间:2023-12-01 17:03:26 25 4
gpt4 key购买 nike

我正在为学校做一个项目,但似乎给自己挖了一个兔子洞。我需要从文件中读取仅包含正数和负数的文件,并将它们与日期一起显示到命令窗口。我能够得到最终结果,但无法列出边际结果。提前致谢!

    class Account{

private int id = 0; //private int data field named id for the account (default 0).
private double balance = 0.0; //private double data field named balance for the account (default 0)
private static double annualInterestRate = 0.0; //private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.

private java.util.Date dateCreated; //private Date data field named dateCreated that stores the date when the account was created.

//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Account() { //no-arg constructor that creates a default account.

dateCreated = new java.util.Date();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Account(int id, double balance) { //constructor that creates an account with the specified id and initial balance.
this();
this.id = id;
this.balance = balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public int getId() {//accessor and mutator methods for id, balance, and annualInterestRate.

return this.id;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getBalance() {//accessor and mutator methods for id, balance, and annualInterestRate.
return this.balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getAnnualInterestRate() {//accessor and mutator methods for id, balance, and annualInterestRate.
return annualInterestRate;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public String getDateCreated() {//accessor method for dateCreated
return this.dateCreated.toString();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setId(int id) { //mutator for id
this.id = id;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setBalance(double balance) { //mutator for balance
this.balance = balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setAnnualInterestRate(double annualInterestRate) { //mutator annual interest rate
this.annualInterestRate = annualInterestRate;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getMonthlyInterestRate() { //method named getMonthlyInterestRate() that returns the monthly interest rate.
return (annualInterestRate / 100) / 12 ;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getMonthlyInterest() { //method named getMonthlyInterest() that returns the monthly interest.
return balance * getMonthlyInterestRate();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void withdraw(double amount) { //method named withdraw that withdraws a specified amount from the account.
this.balance -= amount;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void deposit(double amount) { //method named deposit that deposits a specified amount to the account.

this.balance += amount;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
}//end class account

我很确定这门课没问题。这是我最需要帮助或指导的主要方法。

import java.util.Scanner;
import java.io.*;

public class AccountHomework{
public static void main(String[] args)throws IOException{

File fn = new File("transactions.txt");
Scanner dataIn = new Scanner(fn);

Account account = new Account(1122, 20000);

double[] transactions = new double[10];
account.setAnnualInterestRate(4.5);
account.withdraw(2500.0);
account.deposit(3000.0);

printMethod(account);
}

public static void printMethod(Account acct){
System.out.printf(" %8s %17s %13s","Balance","Monthly Interest","Date Created\n");
System.out.printf(" $%6.2f $%6.2f %s\n",acct.getBalance(),acct.getMonthlyInterest(),acct.getDateCreated());

}

public static void fillTransactions(Scanner dataIn, Stock[] stocks){

double transactions;

for(int indx = 0; indx<stocks.length;indx++){
transactions = dataIn.nextDouble();
dataIn.nextLine(); // read the extra carriage return
if (transactions < 0)
dataIn.withdraw();
else
dataIn.deposit();

account[indx] = new Account();
account[indx].populateStockData(transactions);

}//end for
}//end fillStockArray
}

最佳答案

我注意到两件事:1.扫描仪dataIn不用于读取数据,我看到你有一个方法fillTransaction,但它不是从main调用的2. 在 fillTransactions 中,您在扫描仪上使用方法withdraw()和deposit(),而这些方法实际上是在类Account上定义的

关于java - 使用修改器/访问器读取文件并写入 cmd 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61483981/

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