26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

912 Files and Streams Chapter 16<br />

169<br />

170 // if account field value is not empty<br />

171 if ( ! fieldValues[ BankUI.ACCOUNT ].equals( "" ) ) {<br />

172<br />

173 // output values <strong>to</strong> file<br />

174 try {<br />

175 accountNumber = Integer.parseInt(<br />

176 fieldValues[ BankUI.ACCOUNT ] );<br />

177<br />

178 if ( accountNumber > 0 ) {<br />

179<br />

180 // create new record<br />

181 record = new AccountRecord( accountNumber,<br />

182 fieldValues[ BankUI.FIRSTNAME ],<br />

183 fieldValues[ BankUI.LASTNAME ],<br />

184 Double.parseDouble(<br />

185 fieldValues[ BankUI.BALANCE ] ) );<br />

186<br />

187 // output record and flush buffer<br />

188 output.writeObject( record );<br />

189 output.flush();<br />

190 }<br />

191<br />

192 // clear textfields<br />

193 userInterface.clearFields();<br />

194 }<br />

195<br />

196 // process invalid account number or balance format<br />

197 catch ( NumberFormatException formatException ) {<br />

198 JOptionPane.showMessageDialog( this,<br />

199 "Bad account number or balance",<br />

200 "Invalid Number Format",<br />

201 JOptionPane.ERROR_MESSAGE );<br />

202 }<br />

203<br />

204 // process exceptions from file output<br />

205 catch ( IOException ioException ) {<br />

206 closeFile();<br />

207 }<br />

208<br />

209 } // end if<br />

210<br />

211 } // end method addRecord<br />

212<br />

213 // execute application; CreateSequentialFile construc<strong>to</strong>r<br />

214 // displays window<br />

215 public static void main( String args[] )<br />

216 {<br />

217 new CreateSequentialFile();<br />

218 }<br />

219<br />

220 } // end class CreateSequentialFile<br />

Fig. Fig. 16.6 16.6 Creating a sequential file (part 5 of 6).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!