import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CustomerRating {
private static final String FILE_NAME = "rating.txt";
/**
* Small size on purpose to trigger ArrayIndexOutOfBoundsException when
* full.
*/
private static final int MAX_SIZE = 5;
private static final Customer[] records = new Customer[MAX_SIZE];
private static int count = 0;
/**
* Main intentionally declares 'throws IOException' to follow the spec.
*/
public static void main(String[] args) throws IOException {
// 1) Read data file (must exist or the program terminates with an IOException)
loadFromFile(FILE_NAME);
// Display what we have after loading
printListHeader();
printListBody();
System.out.println();
// 2) Input loop
try (Scanner sc = new Scanner(System.in)) {
while (true) {
System.out.print(
"Enter age[integer], followed by <TAB> key, then rating[decimal number] "
+ "(or type \"!\" to exit): "
);
String line = sc.nextLine().trim();
if (line.equals("!")) {
break;
}
// Must be exactly two fields separated by a TAB
String[] parts = line.split("\\t");
if (parts.length != 2) {
System.out.println("Invalid input format. Please enter: age<TAB>rating");
continue;
}
try {
int age = Integer.parseInt(parts[0].trim());
double rating = Double.parseDouble(parts[1].trim());
// Intentionally rely on real array bounds to raise the exception
// so we can catch it and show the required message.
try {
records[count] = new Customer(age, rating);
count++; // increment only if the write succeeded
System.out.printf("Customer added: Age=%d Rating=%.2f%n", age, rating);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Array out of bound... New record will not be recorded.");
}
} catch (NumberFormatException nfe) {
System.out.println("Invalid number... Record skipped.");
}
}
}
// 3) Before exit: show the most updated list and averages
System.out.println();
printListHeader();
printListBody();
System.out.println();
printAverages();
// 4) Save back to file (if this throws, program terminates automatically per spec)
saveToFile(FILE_NAME);
}
/**
* Read all existing records from the data file.
*/
private static void loadFromFile(String fileName) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
// Each line: age<TAB>rating
String[] parts = line.split("\\t");
if (parts.length != 2) {
continue; // skip malformed lines silently
}
int age = Integer.parseInt(parts[0].trim());
double rating = Double.parseDouble(parts[1].trim());
// If file has more than MAX_SIZE, extra lines will trigger the exception
// and be ignored with a message, while the program continues.
try {
records[count] = new Customer(age, rating);
count++;
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Array out of bound while loading file... Extra file records skipped.");
break; // no more room; stop loading
}
}
}
}
/**
* Write all (non-average) records back to the file.
*/
private static void saveToFile(String fileName) throws IOException {
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
for (int i = 0; i < count; i++) {
pw.println(records[i].getAge() + "\t" + String.format("%.2f", records[i].getRating()));
}
}
}
private static void printListHeader() {
System.out.println("Most updated list of customer ratings");
System.out.println("-------------------------------------");
System.out.printf("%-10s %-10s%n", "Age", "Rating");
}
private static void printListBody() {
for (int i = 0; i < count; i++) {
System.out.println(records[i]);
}
}
private static void printAverages() {
double sumAge = 0.0, sumRating = 0.0;
for (int i = 0; i < count; i++) {
sumAge += records[i].getAge();
sumRating += records[i].getRating();
}
double avgAge = (count == 0) ? 0.0 : sumAge / count;
double avgRating = (count == 0) ? 0.0 : sumRating / count;
System.out.printf("Average(Age)=%.2f%n", avgAge);
System.out.printf("Average(Rating)=%.2f%n", avgRating);
System.out.println();
}
}