[英]Input/Output Java Method
这些说明是:
要处理交易,您需要一次从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的双精度变量和另一个称为balance的双精度变量,则对动画的调用将如下所示:
animate( transactionName, amount, balance);
调用动画时,窗口将在地理上显示当前交易。 它还将显示交易金额(红色,如果是负数,蓝色,如果是正数),以及当前的支票簿余额(黑色)。 通过将先前的支票簿余额添加到当前金额,您将能够在头脑中计算当前的支票簿价应为多少,并确定程序是否正常运行。
到达文件末尾时,打印最终余额并将其写入名为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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.