Class ServiceController
- Since:
- 2023-10-16
- Version:
- 1.0
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final String
The file path for storing and loading service prices, and the map to store service prices.private static Map<ServiceType,
Double[]> A map that associatesServiceType
with an array ofDouble
to represent service prices. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
getServicePrice
(ServiceType serviceType) Retrieves the service price for a specific service type.static double
getUnitPrice
(ServiceType serviceType) Retrieves the unit price for a specific service type.private static void
Loads service prices from the prices file and populates theservicePrices
map.static void
The main method provides an example usage of theServiceController
class.private static void
Saves service prices from theservicePrices
map to the prices file.static void
updateServiceCharges
(ServiceType serviceType, double newServiceCharges) Updates the service charges for a specific service type and saves the changes.static void
updateUnitCharges
(ServiceType serviceType, double newUnitCharges) Updates the unit charges for a specific service type and saves the changes.
-
Field Details
-
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
A map that associatesServiceType
with an array ofDouble
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 theservicePrices
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 theservicePrices
map to the prices file.This method writes the service prices stored in the
servicePrices
map to a text file, specified by thePRICES_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
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
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
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
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
The main method provides an example usage of theServiceController
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:
-