Package Controller

Class ServiceController

java.lang.Object
Controller.ServiceController

public class ServiceController extends Object
This class manages service prices for various service types. It provides methods to update and retrieve service prices from a file. Service prices are stored in a map, and changes are persisted to a text file. The file format for storing service prices is as follows: - Each line contains the service name, unit price, and service price separated by colons and commas. - Example: GAS:0.4,0.6
Since:
2023-10-16
Version:
1.0
  • Field Details

    • PRICES_FILE_PATH

      private static final String PRICES_FILE_PATH
      The file path for storing and loading service prices, and the map to store service prices.

      This class utilizes a text file to persist service prices and a map to maintain them in memory. The path to the prices file and the map are initialized as constants to manage service prices efficiently.

      Since:
      2023-10-16
      See Also:
    • servicePrices

      private static Map<ServiceType,Double[]> servicePrices
      A map that associates ServiceType with an array of Double to represent service prices.

      The map is used to store service prices in memory, and it is populated during the class initialization by reading data from the prices file.

      See Also:
  • Constructor Details

    • ServiceController

      public ServiceController()
  • Method Details

    • loadPricesFromFile

      private static void loadPricesFromFile()
      Loads service prices from the prices file and populates the servicePrices map.

      This method reads service prices from a text file specified by the PRICES_FILE_PATH constant and parses them into a map, associating service types with their respective unit and service prices.

      The format of each line in the prices file should be: ServiceType:unitPrice,servicePrice

      Throws:
      IOException - if an I/O error occurs while reading the prices file.
      See Also:
    • savePricesToFile

      private static void savePricesToFile()
      Saves service prices from the servicePrices map to the prices file.

      This method writes the service prices stored in the servicePrices map to a text file, specified by the PRICES_FILE_PATH constant, in the format: ServiceType:unitPrice,servicePrice.

      Existing data in the prices file will be overwritten with the updated service prices.

      Throws:
      IOException - if an I/O error occurs while writing to the prices file.
      See Also:
    • updateUnitCharges

      public static void updateUnitCharges(ServiceType serviceType, double newUnitCharges)
      Updates the unit charges for a specific service type and saves the changes.

      This method allows you to update the unit charges for a given ServiceType. If the specified service type does not exist in the price data, it will be added with default prices (0.0 for unit charges and service charges).

      After updating the unit charges, the changes are saved to the prices file using savePricesToFile() to persist the updated service prices.

      Parameters:
      serviceType - The type of service for which unit charges need to be updated.
      newUnitCharges - The new unit charges to set for the specified service type.
      Throws:
      IOException - if an I/O error occurs while saving the updated prices to the file.
      See Also:
    • updateServiceCharges

      public static void updateServiceCharges(ServiceType serviceType, double newServiceCharges)
      Updates the service charges for a specific service type and saves the changes.

      This method allows you to update the service charges for a given ServiceType. If the specified service type does not exist in the price data, it will be added with default prices (0.0 for unit charges and service charges).

      After updating the service charges, the changes are saved to the prices file using savePricesToFile() to persist the updated service prices.

      Parameters:
      serviceType - The type of service for which service charges need to be updated.
      newServiceCharges - The new service charges to set for the specified service type.
      Throws:
      IOException - if an I/O error occurs while saving the updated prices to the file.
      See Also:
    • getServicePrice

      public static double getServicePrice(ServiceType serviceType)
      Retrieves the service price for a specific service type.

      This method returns the service price for the specified ServiceType. If the service type does not exist in the price data, it will return a default service price of 0.0.

      Parameters:
      serviceType - The type of service for which the service price is to be retrieved.
      Returns:
      The service price for the specified service type.
      See Also:
    • getUnitPrice

      public static double getUnitPrice(ServiceType serviceType)
      Retrieves the unit price for a specific service type.

      This method returns the unit price for the specified ServiceType. If the service type does not exist in the price data, it will return a default unit price of 0.0.

      Parameters:
      serviceType - The type of service for which the unit price is to be retrieved.
      Returns:
      The unit price for the specified service type.
      See Also:
    • main

      public static void main(String[] args)
      The main method provides an example usage of the ServiceController class.

      This example demonstrates how to interact with the service prices using the ServiceController methods. It initializes a service type (in this case, 'GAS'), prints the initial service and unit charges, updates the charges, and prints the updated charges.

      Usage: - Run this method to see the effect of updating service charges for a specific service type.

      Parameters:
      args - The command-line arguments (not used in this example).
      See Also: