使用mutatorsaccessors读取文件并写入cmd窗口。

问题描述 投票:0回答:1

我正在为学校做一个项目,似乎把自己挖进了一个兔子洞。 我需要从一个文件中读取一个只由正数和负数组成的文件,并将它们和日期一起显示在命令窗口中。 我能够得到最终结果,但不能列出边际结果。先谢谢你了!

    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
}
java arrays accessor mutators
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.