// Fig. 14.14: CreditInquiry.java
// This program reads a file sequentially and displays the
// contents based on the type of account the user requests
// (credit balance, debit balance or zero balance).
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import com.deitel.jhtp7.ch14.AccountRecord;
public class CreditInquiry
{
private MenuOption accountType;
private Scanner input;
private MenuOption choices[] = { MenuOption.ZERO_BALANCE,
MenuOption.CREDIT_BALANCE, MenuOption.DEBIT_BALANCE,
MenuOption.END };
// read records from file and display only records of appropriate type
private void readRecords()
{
// object to be written to file
AccountRecord record = new AccountRecord();
try // read records
{
// open file to read from beginning
input = new Scanner( new File( "clients.txt" ) );
while ( input.hasNext() ) // input the values from the file
{
record.setAccount( input.nextInt() ); // read account number
record.setFirstName( input.next() ); // read first name
record.setLastName( input.next() ); // read last name
record.setBalance( input.nextDouble() ); // read balance
// if proper acount type, display record
if ( shouldDisplay( record.getBalance() ) )
System.out.printf( "%-10d%-12s%-12s%10.2f\n",
record.getAccount(), record.getFirstName(),
record.getLastName(), record.getBalance() );
} // end while
} // end try
catch ( NoSuchElementException elementException )
{
System.err.println( "File improperly formed." );
input.close();
System.exit( 1 );
} // end catch
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
System.exit( 1 );
} // end catch
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "File cannot be found." );
System.exit( 1 );
} // end catch
finally
{
if ( input != null )
input.close(); // close the Scanner and the file
} // end finally
} // end method readRecords
// use record type to determine if record should be displayed
private boolean shouldDisplay( double balance )
{
if ( ( accountType == MenuOption.CREDIT_BALANCE )
&& ( balance < 0 ) )
return true;
else if ( ( accountType == MenuOption.DEBIT_BALANCE )
&& ( balance > 0 ) )
return true;
else if ( ( accountType == MenuOption.ZERO_BALANCE )
&& ( balance == 0 ) )
return true;
return false;
} // end method shouldDisplay
// obtain request from user
private MenuOption getRequest()
{
Scanner textIn = new Scanner( System.in );
int request = 1;
// display request options
System.out.printf( "\n%s\n%s\n%s\n%s\n%s\n",
"Enter request", " 1 - List accounts with zero balances",
" 2 - List accounts with credit balances",
" 3 - List accounts with debit balances", " 4 - End of run" );
try // attempt to input menu choice
{
do // input user request
{
System.out.print( "\n? " );
request = textIn.nextInt();
} while ( ( request < 1 ) || ( request > 4 ) );
} // end try
catch ( NoSuchElementException elementException )
{
System.err.println( "Invalid input." );
System.exit( 1 );
} // end catch
return choices[ request - 1 ]; // return enum value for option
} // end method getRequest
public void processRequests()
{
// get user's request (e.g., zero, credit or debit balance)
accountType = getRequest();
while ( accountType != MenuOption.END )
{
switch ( accountType )
{
case ZERO_BALANCE:
System.out.println( "\nAccounts with zero balances:\n" );
break;
case CREDIT_BALANCE:
System.out.println( "\nAccounts with credit balances:\n" );
break;
case DEBIT_BALANCE:
System.out.println( "\nAccounts with debit balances:\n" );
break;
} // end switch
readRecords();
accountType = getRequest();
} // end while
} // end method processRequests
} // end class CreditInquiry
/*************************************************************************
* (C) Copyright 1992-2007 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
没有合适的资源?快使用搜索试试~ 我知道了~
java大学程序设计和java大学基础教程的例子---缺的---ch14
共37个文件
class:19个
java:18个
需积分: 0 4 下载量 7 浏览量
2009-02-07
19:45:44
上传
评论
收藏 40KB ZIP 举报
温馨提示
java大学程序设计和java大学基础教程的例子缺少一部分,因为上传超过20MB,所以才有了ch14
资源推荐
资源详情
资源评论
收起资源包目录
ch14.zip (37个子文件)
ch14
fig14_13_15
MenuOption.class 1KB
CreditInquiryTest.class 351B
MenuOption.java 2KB
CreditInquiryTest.java 1KB
CreditInquiry$1.class 642B
CreditInquiry.java 6KB
CreditInquiry.class 3KB
fig14_18_19
CreateSequentialFile.java 4KB
CreateSequentialFile.class 2KB
CreateSequentialFileTest.java 1KB
CreateSequentialFileTest.class 426B
fig14_22_23
FileDemonstration.java 4KB
FileDemonstration.class 3KB
FileDemonstrationTest.java 1KB
FileDemonstrationTest.class 380B
com
deitel
jhtp7
ch14
AccountRecord.class 1KB
AccountRecordSerializable.class 1KB
fig14_20_21
ReadSequentialFile.class 2KB
ReadSequentialFile.java 3KB
ReadSequentialFileTest.java 1KB
ReadSequentialFileTest.class 421B
fig14_11_12
ReadTextFile.class 2KB
ReadTextFileTest.class 403B
ReadTextFileTest.java 1KB
ReadTextFile.java 3KB
fig14_06
AccountRecord.java 3KB
fig14_07_09
CreateTextFile.java 4KB
CreateTextFile.class 3KB
CreateTextFileTest.class 408B
CreateTextFileTest.java 1KB
fig14_04_05
FileDemonstration.java 3KB
Test.class 697B
Test.java 2KB
FileDemonstration.class 2KB
FileDemonstrationTest.java 2KB
FileDemonstrationTest.class 703B
fig14_17
AccountRecordSerializable.java 3KB
共 37 条
- 1
资源评论
LIQINGLIN
- 粉丝: 69
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功