CJCoding With Joseph
15free / daySign inCreate account

Thompson Rivers University

CustomerRating Class Driver

Assignment 3 Exceptions - SM 2025

This class controls the entire survey program. It reads customer ratings from a file, lets the user add new ones, handles exceptions like file errors or bad input, and then saves the data back to the file when the user quits.


Class Setup and Constants

We start by defining two constants:

  • MAX_SIZE is the max number of customer records we allow in the program.

  • FILE_NAME is the name of the file we’re reading from and writing to.

private static final int MAX_SIZE = 5;
private static final String FILE_NAME = "rating.txt";

Inside main, we declare:

  • A fixed-size array to hold customer records

  • A count variable to track how many customers have been stored

Customer[] records = new Customer[MAX_SIZE];
int count = 0;


Reading Existing Records from the File

To load survey data at the start of the program, we read from a file called rating.txt that is stored in the constant FILE_NAME. We use a Scanner to read each line. Each line contains a customer’s age and rating, separated by a TAB.

We split the line using .split("\t"), convert the values into numbers, and store them as Customer objects in an array.

Scanner fileScanner = new Scanner(new File(FILE_NAME));

while (fileScanner.hasNextLine() && count < MAX_SIZE) {
    String line = fileScanner.nextLine();
    String[] parts = line.split("\t");
    int age = Integer.parseInt(parts[0]);
    double rating = Double.parseDouble(parts[1]);
    records[count] = new Customer(age, rating);
    count++;
}

fileScanner.close();
Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 54 more lessons in Thompson Rivers University.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.