forked from phongus/Deitel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
994 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
17.4 Sequential-Access Text Files/17.4 Sequential-Access Text Files.iml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<output url="file://$USER_HOME$/Desktop/Deitel/production/17.4 Sequential-Access Text Files" /> | ||
<output-test url="file://$USER_HOME$/Desktop/Deitel/test/17.4 Sequential-Access Text Files" /> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/CaseStudy" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" /> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" exported="" name="17.4" level="project" /> | ||
<orderEntry type="module" module-name="Deitel" exported="" /> | ||
<orderEntry type="library" name="src" level="application" /> | ||
</component> | ||
</module> |
144 changes: 144 additions & 0 deletions
144
17.4 Sequential-Access Text Files/src/CaseStudy/CreditInquiry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Fig. 17.12: CreditInquiry.java | ||
// This program reads a file sequentially and displays the | ||
// content based on the type of account the user requests | ||
// (credit balance, debit balance or zero balance). | ||
package CaseStudy; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.lang.IllegalStateException; | ||
import java.nio.channels.IllegalSelectorException; | ||
import java.util.NoSuchElementException; | ||
import java.util.Scanner; | ||
import SequentialFile.AccountRecord; | ||
import com.sun.org.apache.bcel.internal.generic.MONITORENTER; | ||
|
||
import javax.accessibility.AccessibleTable; | ||
|
||
|
||
public class CreditInquiry | ||
{ | ||
private MenuOption accountType; | ||
private Scanner input; | ||
private final static 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 store data that will 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 account 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 new 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 balance:\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 | ||
} // etd method processRequests | ||
} // end class CreditInquiry |
13 changes: 13 additions & 0 deletions
13
17.4 Sequential-Access Text Files/src/CaseStudy/CreditInquiryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package CaseStudy; | ||
|
||
/** | ||
* Created by P Trang on 8/3/2015. | ||
*/ | ||
public class CreditInquiryTest | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
CreditInquiry application = new CreditInquiry(); | ||
application.processRequests(); | ||
} // end main | ||
} // end class CreditInquiryTest |
27 changes: 27 additions & 0 deletions
27
17.4 Sequential-Access Text Files/src/CaseStudy/MenuOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package CaseStudy; | ||
|
||
// Fig. 17.11: MenuOption.java | ||
// Enumeration for the credit-inquiry program's options. | ||
|
||
public enum MenuOption | ||
{ | ||
// declare contents of enum type | ||
ZERO_BALANCE(1), | ||
CREDIT_BALANCE(2), | ||
DEBIT_BALANCE(3), | ||
END(4); | ||
|
||
private final int value; // current menu option | ||
|
||
// constructor | ||
MenuOption(int valueOption) | ||
{ | ||
value = valueOption; | ||
} // end MenuOptions enum constructor | ||
|
||
// return the value of a constant | ||
public int getValue() | ||
{ | ||
return value; | ||
} // end method getValue | ||
} // end enum MenuOption |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
17.4 Sequential-Access Text Files/src/SequentialFile/CreateTextFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package SequentialFile;// Testing the SequentialFile.CreateTextFile class | ||
|
||
public class CreateTextFileTest | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
CreateTextFile application = new CreateTextFile(); | ||
|
||
application.openFile(); | ||
application.addRecords(); | ||
application.closeFile(); | ||
} // end main | ||
} // |
73 changes: 73 additions & 0 deletions
73
17.4 Sequential-Access Text Files/src/SequentialFile/ReadTextFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package SequentialFile; /** | ||
* Fig. 17.9: SequentialFile.ReadTextFile.java | ||
* This program reads a text files and displays each record. | ||
*/ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.lang.IllegalStateException; | ||
import java.util.NoSuchElementException; | ||
import java.util.Scanner; | ||
|
||
public class ReadTextFile | ||
{ | ||
private Scanner input; | ||
|
||
// enable user to open file | ||
public void openFile() | ||
{ | ||
try | ||
{ | ||
input = new Scanner(new File("clients.txt")); | ||
} // end try | ||
catch(FileNotFoundException fileNotFoundException) | ||
{ | ||
System.err.println("Error opening file."); | ||
System.exit(1); | ||
} // end catch | ||
} // end method openFile | ||
|
||
// read record from file | ||
public void readRecords() | ||
{ | ||
// object to be written to screen | ||
AccountRecord record = new AccountRecord(); | ||
|
||
System.out.printf("%-10s%-12s%-12s%10s\n", "Account", | ||
"First Name", "Last Name", "Balance"); | ||
try // read records from files using Scanner object | ||
{ | ||
while (input.hasNext()) | ||
{ | ||
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 | ||
|
||
// display record contents | ||
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 | ||
} // end method readRecords | ||
|
||
// close files and terminate application | ||
public void closeFile() | ||
{ | ||
if (input != null) | ||
{ | ||
input.close(); // close file | ||
} | ||
} // end method closeFile | ||
} // end class SequentialFile.ReadTextFile |
13 changes: 13 additions & 0 deletions
13
17.4 Sequential-Access Text Files/src/SequentialFile/ReadTextFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package SequentialFile;// Testing the SequentialFile.ReadTextFile class. | ||
|
||
public class ReadTextFileTest | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
ReadTextFile application = new ReadTextFile(); | ||
|
||
application.openFile(); | ||
application.readRecords(); | ||
application.closeFile(); | ||
} // end main | ||
} // end class ReadTexFileTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Oops, something went wrong.