public class TransactionProcessor
1: // Fig. 14.35: TransactionProcessor.java
2: // A transaction processing program using random-access files.
3: import java.io.IOException;
4: import java.util.NoSuchElementException;
5: import java.util.Scanner;
6:
7: import com.deitel.jhtp6.ch14.RandomAccessAccountRecord;
8:
9: public class TransactionProcessor
10: {
11: private FileEditor dataFile;
12: private RandomAccessAccountRecord record;
13: private MenuOption choices[] = { MenuOption.PRINT,
14: MenuOption.UPDATE, MenuOption.NEW,
15: MenuOption.DELETE, MenuOption.END };
16:
17: private Scanner input = new Scanner( System.in );
18:
19: // get the file name and open the file
20: private boolean openFile()
21: {
22: try // attempt to open file
23: {
24: // call the helper method to open the file
25: dataFile = new FileEditor( "clients.dat" );
26: } // end try
27: catch ( IOException ioException )
28: {
29: System.err.println( "Error opening file." );
30: return false;
31: } // end catch
32:
33: return true;
34: } // end method openFile
35:
36: // close file and terminate application
37: private void closeFile()
38: {
39: try // close file
40: {
41: dataFile.closeFile();
42: } // end try
43: catch ( IOException ioException )
44: {
45: System.err.println( "Error closing file." );
46: System.exit( 1 );
47: } // end catch
48: } // end method closeFile
49:
50: // create, update or delete the record
51: private void performAction( MenuOption action )
52: {
53: int accountNumber = 0; // account number of record
54: String firstName; // first name for account
55: String lastName; // last name for account
56: double balance; // account balance
57: double transaction; // amount to change in balance
58:
59: try // attempt to manipulate files based on option selected
60: {
61: switch ( action ) // switch based on option selected
62: {
63: case PRINT:
64: System.out.println();
65: dataFile.readRecords();
66: break;
67: case NEW:
68: System.out.printf( "\n%s%s\n%s\n%s",
69: "Enter account number,",
70: " first name, last name and balance.",
71: "(Account number must be 1 - 100)", "? " );
72:
73: accountNumber = input.nextInt(); // read account number
74: firstName = input.next(); // read first name
75: lastName = input.next(); // read last name
76: balance = input.nextDouble(); // read balance
77:
78: dataFile.newRecord( accountNumber, firstName,
79: lastName, balance ); // create new record
80: break;
81: case UPDATE:
82: System.out.print(
83: "\nEnter account to update ( 1 - 100 ): " );
84: accountNumber = input.nextInt();
85: record = dataFile.getRecord( accountNumber );
86:
87: if ( record.getAccount() == 0 )
88: System.out.println( "Account does not exist." );
89: else
90: {
91: // display record contents
92: System.out.printf( "%-10d%-12s%-12s%10.2f\n\n",
93: record.getAccount(), record.getFirstName(),
94: record.getLastName(), record.getBalance() );
95:
96: System.out.print(
97: "Enter charge ( + ) or payment ( - ): " );
98: transaction = input.nextDouble();
99: dataFile.updateRecord( accountNumber, // update record
100: transaction );
101:
102: // retrieve updated record
103: record = dataFile.getRecord( accountNumber );
104:
105: // display updated record
106: System.out.printf( "%-10d%-12s%-12s%10.2f\n",
107: record.getAccount(), record.getFirstName(),
108: record.getLastName(), record.getBalance() );
109: } // end else
110: break;
111: case DELETE:
112: System.out.print(
113: "\nEnter an account to delete (1 - 100): " );
114: accountNumber = input.nextInt();
115:
116: dataFile.deleteRecord( accountNumber ); // delete record
117: break;
118: default:
119: System.out.println( "Invalid action." );
120: break;
121: } // end switch
122: } // end try
123: catch ( NumberFormatException format )
124: {
125: System.err.println( "Bad input." );
126: } // end catch
127: catch ( IllegalArgumentException badAccount )
128: {
129: System.err.println( badAccount.getMessage() );
130: } // end catch
131: catch ( IOException ioException )
132: {
133: System.err.println( "Error writing to the file." );
134: } // end catch
135: catch ( NoSuchElementException elementException )
136: {
137: System.err.println( "Invalid input. Please try again." );
138: input.nextLine(); // discard input so user can try again
139: } // end catch
140: } // end method performAction
141:
142: // enable user to input menu choice
143: private MenuOption enterChoice()
144: {
145: int menuChoice = 1;
146:
147: // display available options
148: System.out.printf( "\n%s\n%s\n%s\n%s\n%s\n%s",
149: "Enter your choice", "1 - List accounts",
150: "2 - Update an account", "3 - Add a new account",
151: "4 - Delete an account", "5 - End program\n? " );
152:
153: try // attempt to input menu choice
154: {
155: menuChoice = input.nextInt();
156: } // end try
157: catch ( NoSuchElementException elementException )
158: {
159: System.err.println( "Invalid input." );
160: System.exit( 1 );
161: } // end catch
162:
163: return choices[ menuChoice - 1 ]; // return choice from user
164: } // end enterChoice
165:
166: public void processRequests()
167: {
168: openFile();
169:
170: // get user's request
171: MenuOption choice = enterChoice();
172:
173: while ( choice != MenuOption.END )
174: {
175: performAction( choice );
176: choice = enterChoice();
177: } // end while
178:
179: closeFile();
180: } // end method processRequests
181: } // end class TransactionProcessor
182:
183: /*************************************************************************
184: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
185: * Pearson Education, Inc. All Rights Reserved. *
186: * *
187: * DISCLAIMER: The authors and publisher of this book have used their *
188: * best efforts in preparing the book. These efforts include the *
189: * development, research, and testing of the theories and programs *
190: * to determine their effectiveness. The authors and publisher make *
191: * no warranty of any kind, expressed or implied, with regard to these *
192: * programs or to the documentation contained in these books. The authors *
193: * and publisher shall not be liable in any event for incidental or *
194: * consequential damages in connection with, or arising out of, the *
195: * furnishing, performance, or use of these programs. *
196: *************************************************************************/