PracticalTest_SCSJ2154_2019

 avatar
unknown
plain_text
2 years ago
3.7 kB
4
Indexable
**Country.java

public class Country {
    // Attributes
    private String name;
    private double factor;
    private int index;
    private String category;

    // Constructor
    public Country(String name, double factor, int index, String category) {
        this.name = name;
        this.factor = factor;
        this.index = index;
        this.category = category;
    }

    // Getter methods
    public String getName() {
        return name;
    }

    public double getFactor() {
        return factor;
    }

    public int getIndex() {
        return index;
    }

    public String getCategory() {
        return category;
    }
}


**Level.java

public enum Level {
    // Enum constants
    LEVEL1(8.0, "HIGH HAPPINESS"),
    LEVEL2(6.0, "MEDIUM HAPPINESS"),
    LEVEL3(4.0, "LOW HAPPINESS"),
    LEVEL4(1.0, "NOT HAPPY");

    // Attributes
    private final double index;
    private final String category;

    // Constructor
    Level(double index, String category) {
        this.index = index;
        this.category = category;
    }

    // Getter methods
    public double getIndex() {
        return index;
    }

    public String getCategory() {
        return category;
    }
}


**HapinessReport.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;

public class HappinessReport {
    // Counters for different levels
    private static int highCount = 0;
    private static int mediumCount = 0;
    private static int lowCount = 0;
    private static int notHappyCount = 0;
    private static int totalLevels = 0;

    public static void main(String[] args) {
        // Vector to store Country objects
        Vector<Country> countries = new Vector<>();

        // Read input from the file
        try {
            Scanner scanner = new Scanner(new File("InputSA.txt"));

            while (scanner.hasNext()) {
                // Read data from each line
                String levelStr = scanner.next();
                String name = scanner.next();
                double factor = scanner.nextDouble();

                // Convert level string to Level enum
                Level level = Level.valueOf(levelStr);

                // Create Country object and add to the vector
                Country country = new Country(name, factor, level.ordinal() + 1, level.getCategory());
                countries.add(country);

                // Count levels
                countLevels(level);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // Print all levels
        System.out.println("All Levels:");
        for (Level level : Level.values()) {
            System.out.println(level.name() + ": " + level.getIndex() + " - " + level.getCategory());
        }

        // Print level counts
        System.out.println("\nLevel Counts:");
        System.out.println("High Happiness: " + highCount);
        System.out.println("Medium Happiness: " + mediumCount);
        System.out.println("Low Happiness: " + lowCount);
        System.out.println("Not Happy: " + notHappyCount);
        System.out.println("Total Levels: " + totalLevels);
    }

    // Helper method to count levels
    private static void countLevels(Level level) {
        switch (level) {
            case LEVEL1:
                highCount++;
                break;
            case LEVEL2:
                mediumCount++;
                break;
            case LEVEL3:
                lowCount++;
                break;
            case LEVEL4:
                notHappyCount++;
                break;
        }
        totalLevels++;
    }
}
Editor is loading...
Leave a Comment