Files
G4G0-1/Semester 1/Programming 1/Java/examples/projects/chapter07/weblog-analyzer/LogAnalyzer.java
2024-01-15 20:14:10 +00:00

59 lines
1.4 KiB
Java
Executable File

/**
* Read web server data and analyse hourly access patterns.
*
* @author David J. Barnes and Michael Kölling.
* @version 2016.02.29
*/
public class LogAnalyzer
{
// Where to calculate the hourly access counts.
private int[] hourCounts;
// Use a LogfileReader to access the data.
private LogfileReader reader;
/**
* Create an object to analyze hourly web accesses.
*/
public LogAnalyzer()
{
// Create the array object to hold the hourly
// access counts.
hourCounts = new int[24];
// Create the reader to obtain the data.
reader = new LogfileReader();
}
/**
* Analyze the hourly access data from the log file.
*/
public void analyzeHourlyData()
{
while(reader.hasNext()) {
LogEntry entry = reader.next();
int hour = entry.getHour();
hourCounts[hour]++;
}
}
/**
* Print the hourly counts.
* These should have been set with a prior
* call to analyzeHourlyData.
*/
public void printHourlyCounts()
{
System.out.println("Hr: Count");
for(int hour = 0; hour < hourCounts.length; hour++) {
System.out.println(hour + ": " + hourCounts[hour]);
}
}
/**
* Print the lines of data read by the LogfileReader
*/
public void printData()
{
reader.printData();
}
}