0

指令是:

要处理交易,您需要从 transactions.txt 文件中一次读取一行并解析您检索到的字符串。您可以为此使用 Scanner 类。分隔符将是一个冒号。然后处理交易;您无需检查交易类型。只需将交易金额添加到支票簿余额中即可。添加负交易金额将按预期减少余额。确保在适当的地方使用 try/catch 块。

处理完每个事务后,调用 animate 方法。此方法属于 Accounting 类,因此您将在不使用对象引用的情况下调用 animate。如果 animate 方法如下所示的 API

public void animate { String currentTransaction, double currentAmount, double currentBalance, }

如您所见,animate 方法接受三个参数:currentTransaction 是交易名称(例如“Deposit”),currentAmount 是交易金额(例如-45.00),currentBalance 是支票簿的当前余额. 假设您有一个名为 transactionName 的 String 变量、一个名为 amount 的 double 变量和另一个名为 balance 的 double ,对 animate 的调用将如下所示:

animate( transactionName, amount, balance);

当您调用动画时,窗口将按地理位置显示当前事务。它还将显示交易金额(红色为负数,蓝色为正数)和当前支票簿余额(黑色)。通过将先前的支票簿余额添加到当前金额中,您将能够在脑海中计算出当前支票簿的余额应该是多少,并确定您的程序是否正常工作。

到达文件末尾时,打印最终余额并将其写入名为 balance.txt 的文件

到目前为止,我已经在会计类中编写了这么多代码:

import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.Graphics;
import java.util.*;
import java.io.*;

public class Accounting extends JFrame
{
 private BankAccount bankAccount;

 public Accounting( )
 {
   bankAccount = new BankAccount( getBackground( ) );
   setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   setSize( 300, 300 );
   setVisible( true );
 }

 public void balanceCheckBook( )
 {
     // ***** Write the body of this method *****
//
// Using a while loop, read the file transactions.txt
// The file transactions.txt contains
// transactions between you and your bank
//
// You will need to call the animate method inside
// the body of the loop that reads the file contents
//
// The animate method takes three arguments:
// a String, representing the type of transaction
// a double, representing the transaction money amount
// a double, representing the new checkbook balance
// So if these three variables are:
// transactionName, currentAmount, and balance,
// then the call to animate will be:
//
// animate( transactionName, currentAmount, balance );
//
// You should make that call in the body of your while
// loop, after you have updated the checkbook balance
//


    double balance = 0.00;
    double currentAmount;
    String nextLine;
    StringTokenizer st;
    String transactionName;
 }

 public void animate( String currentTransaction, double currentAmount, double currentBalance )
 {
   if ( currentTransaction.startsWith( "Ch" ) )
       bankAccount.setCurrentTransaction( new Check(currentAmount ) );
   else if ( currentTransaction.startsWith( "With" ) )
       bankAccount.setCurrentTransaction( new Withdrawal(currentAmount ) );
   else if ( currentTransaction.startsWith( "Dep" ) )
       bankAccount.setCurrentTransaction( new Deposit(currentAmount ) );
   else
       bankAccount.setCurrentTransaction( new UnknownTransaction(currentAmount ) );


   bankAccount.updateBalance( currentBalance );

   repaint( );
   try
   {
    Thread.sleep( 3000 );
   }
   catch ( Exception e )
   {
   }
 }

 public void paint( Graphics g )
 {
   super.paint( g );
   bankAccount.draw( g );
 }

 public static void main( String [] args )
 {
   Accounting app = new Accounting( );
   app.balanceCheckBook( );
 }
}
4

1 回答 1

1

使用扫描仪是在 java 中读取文件的最简单方法

import java.util.Scanner

然后

Scanner myScanner = new Scanner(new File("/Your/File/Path/Here/transactions.txt");

然后

String line;
while (myScanner.hasNextLine()) {
    line = myScanner.nextLine();
    //i think you'll call your animate function in here

}

根据您的文件结构,您可以使用 myScanner.next() 和 myScanner.nextInt() 分别获取标记和整数,标记通常是由空格分隔的单词

[编辑]

op想要解释如何扫描每一行,这里有一个演示

while (myScanner.hasNextLine()) {
    line = myScanner.nextLine();
    Scanner lineReader = new Scanner(line);
    String firstWord = lineReader.next();
    String secondWord = lineReader.next();
    double thirdWordValue = lineReader.nextDouble();
    double fourthWordValue = lineReader.nextDouble();

    animate(firstWord, thirdWordValue, fourthWordValue);

}    
于 2013-06-24T19:11:44.403 回答