fdsf

fdsfsd
 avatar
user_2508819
java
a year ago
1.7 kB
1
Indexable
Never
package hostelmanagementsystem;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.util.List;

public class CalculateGRI {

    private int totalRooms = 0;
    private int occupiedRooms = 0;
    private double occupancyRate = 0.0;
    private String formattedOccupancyRate = "";

    public void run() throws IOException {
        calculateOccupancyRate();
        writeResultsToFile();
    }

    private void calculateOccupancyRate() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get("room.txt"));
        for (String line : lines) {
            String[] parts = line.split(",");
            if (parts[3].trim().equalsIgnoreCase("available")) {
                totalRooms++;
            } else {
                totalRooms++;
                occupiedRooms++;
            }
        }
        occupancyRate = (double) occupiedRooms / totalRooms * 100;
        DecimalFormat df = new DecimalFormat("#.##");
        formattedOccupancyRate = df.format(occupancyRate);
    }

    private void writeResultsToFile() throws IOException {
        try ( PrintWriter writer = new PrintWriter("cgri.txt", "UTF-8")) {
            writer.println("Total Hostel Rooms: " + totalRooms);
            writer.println("Total Occupied Rooms: " + occupiedRooms);
            writer.println("Occupancy Rate: " + formattedOccupancyRate + "%");
        }
    }

    public static void main(String[] args) throws IOException {
        CalculateGRI gri = new CalculateGRI();
        gri.run();
    }
}