Skip to content

Commit

Permalink
Add 17.* modules to git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
phongus committed Aug 24, 2015
1 parent b9dd8ef commit 0e76ed7
Show file tree
Hide file tree
Showing 26 changed files with 994 additions and 11 deletions.
9 changes: 9 additions & 0 deletions .idea/libraries/17_4.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 11.5_StackUnwinding/src/UsingExceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

public class UsingExceptions
{
public static void main(String[] args)
public static void main(String[] args) // aoeuaoe
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion 17.3 FileDemonstration/src/FileDemonstration.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void analyzePath(String path)
(name.isDirectory() ? "is a directory" : "is not a directory"),
(name.isAbsolute() ? "is absolute path" : "is not absolute path"),
"Last modified: ",
name.lastModified(), "Lengeth: ", name.length(),
name.lastModified(), "Length: ", name.length(),
"Path: ", name.getPath(), "Absolute path: ",
name.getAbsolutePath(), "Parent: ", name.getParent());

Expand Down
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 17.4 Sequential-Access Text Files/src/CaseStudy/CreditInquiry.java
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
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 17.4 Sequential-Access Text Files/src/CaseStudy/MenuOption.java
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package SequentialFile;

/**
* Fig. 17.4: AccountRecord.java
* AccountRecord class maintains information for one account.
* Fig. 17.4: SequentialFile.AccountRecord.java
* SequentialFile.AccountRecord class maintains information for one account.
*/

public class AccountRecord // testuaoeuoa
public class AccountRecord
{
private int account;
private String firstName;
Expand All @@ -23,7 +25,7 @@ public AccountRecord(int acct, String first, String last, double bal)
setFirstName(first);
setLastName(last);
setBalance(bal);
} // end four-argument AccountRecord constructor
} // end four-argument SequentialFile.AccountRecord constructor

// set account number
public void setAccount(int acct)
Expand Down Expand Up @@ -72,4 +74,4 @@ public double getBalance()
{
return balance;
} // end method getBalance
} // end class AccountRecord
} // end class SequentialFile.AccountRecord
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Fig. 17.5: CreateTextFile.java
* Fig. 17.5: SequentialFile.CreateTextFile.java
* Writing data to a sequential text file with class Formatter.
*/
package SequentialFile;

import java.io.FileNotFoundException;
import java.lang.SecurityException;
Expand Down Expand Up @@ -97,4 +98,4 @@ public void closeFile()
if (output != null)
output.close();
} // end method closeFile
} // end class CreateTextFile
} // end class SequentialFile.CreateTextFile
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
} //
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
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
8 changes: 8 additions & 0 deletions 17.5 Object Serialization/17.5 Object Serialization.iml
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>
Loading

0 comments on commit 0e76ed7

Please sign in to comment.