java - 输入/输出 Java 方法

标签 java graphics exception io output

说明是:

To process the transactions, you will need to read one line at a time from the transactions.txt file and parse the String that you retrieve. You can use the Scanner class for this. The delimiter will be a colon. Then process the transaction; you do not need to check the type of transaction. Just add the amount of the transaction to the checkbook balance. Adding a negative transaction amount will decrease the balance as expected. Be sure to use try/catch blocks where appropriate.

After you have processed each transaction, call the animate method. This method belongs to the Accounting class, so you will call animate without using an object reference. The API if the animate method is the following

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

As you can see, the animate method takes three arguments: currentTransaction is the transaction name ("Deposit," for example), currentAmount is the amount of the transaction (-45.00, for example), and currentBalance is the current balance of the checkbook. Assuming that you have a String variable called transactionName, a double variable called amount, and another double called balance, a call to animate will look like the following:

animate( transactionName, amount, balance);

When you call the animate, the window will display the current transaction geographically. It will also display the transaction amount (red, if negative, blue if positive), and the current checkbook balance (in black). By adding the previous checkbook balance to the current amount, you will be able to compute in your head what the current checkbook valance should be and determine if your program is working correctly.

When you reach the end of the file, print the final balance and write it to a file named balance.txt

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

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

最佳答案

使用扫描仪是在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);

}    

关于java - 输入/输出 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282692/

相关文章:

java - 抛出和捕获异常

java - 如何在 Mac OS 上设置 JButton 的背景颜色

c++ - 来自 C++ 程序的动画

delphi - 简单的 Delphi 3d 函数

c# - 在 C# 中使用 BeginRead 捕获异常

javax.swing.JPanel 无法转换为 javax.swing.JLayeredPane

java - Location Manager.RequestLocationUpdates 使用 pendingIntent 始终广播相同的位置,当 gps 提供商需要很长时间才能获取位置时

java - JSF - session 范围的托管 bean 没有在 session 反序列化时重新注入(inject)的依赖项

java - 为 Mac OS X 安装 Java JDK 7 后 - mvn -version 仍然显示 java 版本 1.6.0_31

graphics - 抗锯齿替代品