输入/输出Java方法

时间:2013-06-24 18:42:00

标签: java graphics exception-handling io output

说明如下:

  

要处理事务,您需要从transactions.txt文件中一次读取一行并解析您检索的String。您可以使用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的双变量,另一个名为balance的双变量,对animate的调用将如下所示:

animate( transactionName, amount, balance);
     

当您调用animate时,窗口将按地理位置显示当前事务。它还将显示交易金额(红色,如果是负数,蓝色,如果是正数),以及当前支票簿余额(黑色)。通过将之前的支票簿余额添加到当前金额,您将能够在头脑中计算当前支票簿的价值,并确定您的程序是否正常运行。

     

当您到达文件末尾时,打印最终余额并将其写入名为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( );
 }
}

1 个答案:

答案 0 :(得分: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);

}