Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Charge Account Modification Programming Challenge 5 Pg. 541 Jave Early Objects 5

ID: 3579200 • Letter: C

Question

Charge Account Modification

Programming Challenge 5

Pg. 541

Jave Early Objects 5th Edition

Modify the charge account validation class that you wrote for Programming Challenge 3 so it reads the list of valid charge account numbers from a file. Use Notepad or another editor to create the file.

AccountNumber.txt: https://atlanticbb9.blackboard.com/bbcswebdav/pid-673166-dt-content-rid-1963225_1/courses/CISM154DICKER_2016FA/AccountNumbers.txt

Validator Class UML

- valid : int []

+ Validator(filename : String)

+ isValid(number : int) : boolean

Explanation / Answer

public class Validator
{
  
public boolean isValid(int number)
{
boolean found = false; // Flag
//give full path of the file
String fileName = "AccountNumber.tx";

try{
String line = null;
FileReader fileReader =
new FileReader(fileName);


BufferedReader br =
new BufferedReader(fileReader);

while((line = br.readLine()) != null) {
if(line.trim().equals(number+"")){
found=true;
break;
}
}   


br.close();   
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
return found;
}
}