Package Controller
Class CustomerController
java.lang.Object
Controller.CustomerController
This class is responsible for managing customer data, including registration,
login,
and data persistence.
Usage
To use this class, create an instance of `CustomerController` and call its methods to perform various operations on customer data.Example Usage
CustomerController controller = new CustomerController();
controller.registerNewUser("username", "password", "email@example.com");
controller.validateLogin("username", "password");
File Management
Customer data is stored in a text file defined by the `customersFilePath` attribute. This class provides methods to load customers from the file and save customers to the file.Thread Safety
This class is not thread-safe, so external synchronization may be required when accessed by multiple threads.- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionA list that holds customer objects.private String
The file path for storing customer data. -
Constructor Summary
ConstructorDescriptionConstructor for the CustomerController class that initializes the list of customers and loads customer data from a file. -
Method Summary
Modifier and TypeMethodDescriptiongetCustomerByUsername
(String username) Retrieve a customer by their username.Retrieves the list of customer objects.boolean
isEmailTaken
(String email) Check if an email address is already in use by a registered customer.boolean
isUsernameTaken
(String username) Check if a username is already taken by a registered customer.loadCustomer
(String username, String password) Load a customer by their username and password.void
Load customer data from a file, populating the customer list.void
Print information about all registered customers.boolean
registerNewUser
(String username, String password, String email) Registers a new user with the provided username, password, and email.void
removeCustomer
(String username) Remove a registered customer with the specified username.void
Save customer data to a file.boolean
validateLogin
(String username, String password) Validate customer login credentials.
-
Field Details
-
customers
A list that holds customer objects.Description
The `customers` list contains instances of theCustomer
class, representing registered customers in the system. Various operations related to customer management are performed on this list. -
customersFilePath
The file path for storing customer data.Description
The `customersFilePath` field holds the file path to the location where customer data is stored and retrieved. The default file path is "src/Model/customers.txt," but you can modify this path as needed to specify a different location for the customer data file.
-
-
Constructor Details
-
CustomerController
public CustomerController()Constructor for the CustomerController class that initializes the list of customers and loads customer data from a file.Initialization
Upon construction of a `CustomerController` instance, it initializes an empty list of customer objects and specifies the default file path for storing customer data. It then proceeds to load customer data from the file, if available.File Loading
This constructor automatically calls the `loadCustomersFromFile` method to load existing customer data from the file specified by the `customersFilePath`.Default File Path
By default, the `customersFilePath` is set to "src/Model/customers.txt". You can modify this path as needed.
-
-
Method Details
-
getCustomers
Retrieves the list of customer objects.Usage
Use this method to obtain a reference to the list of customer objects maintained by theCustomerController
instance.- Returns:
- A list containing
Customer
objects.
-
loadCustomersFromFile
public void loadCustomersFromFile()Load customer data from a file, populating the customer list. If the file is not found, it creates a new file.File Loading
This method reads customer data from the specified file, assuming that the data is stored in CSV (Comma-Separated Values) format, with each line containing customer information in the order: username, password, email.Error Handling
If the file is not found, it will catch a `FileNotFoundException` and create a new customer file using the `saveCustomersToFile` method.- Throws:
FileNotFoundException
- If the customer data file is not found.
-
registerNewUser
Registers a new user with the provided username, password, and email.Registration Process
This method attempts to register a new customer in the system by checking if the provided username and email address are available. If the username or email is already taken, it displays an error message and returns `false`. If both the username and email are available, it creates a new customer object and adds it to the list of customers. The customer data is then saved to a file using the `saveCustomersToFile` method.Username and Email Validation
It uses the `isUsernameTaken` and `isEmailTaken` methods to validate whether the provided username and email are already in use.- Parameters:
username
- The username for the new user.password
- The password for the new user.email
- The email address for the new user.- Returns:
- `true` if the registration is successful, `false` if the username or email is already taken.
-
PrintCustomers
public void PrintCustomers()Print information about all registered customers.Printing Customer Information
This method iterates through the list of registered customers and prints information about each customer, including their username, password, and email, to the console.Output Format
The customer information is printed in the format:Username Password Email
Example Output
If there are two registered customers with usernames "user1" and "user2," the output might look like this:user1 password1 user1@example.com user2 password2 user2@example.com
-
removeCustomer
Remove a registered customer with the specified username.Customer Removal
This method attempts to remove a customer with the given username from the list of registered customers. It checks if a customer with the provided username exists. If found, it removes the customer from the list and updates the customer data file using the `saveCustomersToFile` method. If the username is not found, it prints an error message indicating that the customer was not found.Parameter
- Parameters:
username
- The username of the customer to be removed.Errors and Warnings
If the provided username is not found, a "Customer not found" message is printed to the console.
-
getCustomerByUsername
Retrieve a customer by their username.Customer Retrieval
This method searches the list of registered customers and attempts to retrieve a customer by their username. If a customer with the specified username is found, the customer object is returned. If the username is not found, the method returns `null` to indicate that the customer was not found.Parameter
- Parameters:
username
- The username of the customer to retrieve.- Returns:
- A
Customer
object if found, or `null` if the customer is not found.
-
isUsernameTaken
Check if a username is already taken by a registered customer.Username Availability
This method checks if a provided username is already in use by any of the registered customers. If a customer with the specified username is found, it returns `true` to indicate that the username is taken. If the username is not found among registered customers, it returns `false` to indicate that the username is available for registration.Parameter
- Parameters:
username
- The username to be checked for availability.- Returns:
- `true` if the username is already taken, `false` if it is available for registration.
-
isEmailTaken
Check if an email address is already in use by a registered customer.Email Address Availability
This method checks if a provided email address is already associated with any of the registered customers. If a customer with the specified email address is found, it returns `true` to indicate that the email address is already taken. If the email address is not found among registered customers, it returns `false` to indicate that the email address is available for registration.Parameters
- Parameters:
email
- The email address to be checked for availability.- Returns:
- `true` if the email address is already taken, `false` if it is available for registration.
-
validateLogin
Validate customer login credentials.Login Validation
This method validates customer login credentials by comparing the provided username and password with the credentials of registered customers. If a matching customer is found, it returns `true` to indicate a successful login. If no customer matches the provided credentials, it returns `false` to indicate an unsuccessful login.Parameters
- Parameters:
username
- The username provided during login.password
- The password provided during login.- Returns:
- `true` if the login credentials are valid, `false` if they are not.
-
saveCustomersToFile
public void saveCustomersToFile()Save customer data to a file.Data Saving
This method saves customer data, including usernames, passwords, and email addresses, to a file specified by the `customersFilePath` field. The data is stored in a CSV (Comma-Separated Values) format, where each line contains customer information in the order: username, password, email.Exception Handling
If an error occurs during the file writing process, an `IOException` is caught and an error message is printed to the console, indicating the issue encountered while saving the data.File Format
The customer data is saved in the following format:Username,Password,Email
File Location
The location and name of the file are determined by the `customersFilePath` field. Ensure that this field points to the desired location and file name for saving customer data.- Throws:
IOException
- If an error occurs while writing the customer data to the file.
-
loadCustomer
Load a customer by their username and password.Customer Retrieval
This method attempts to retrieve a registered customer by their provided username and password. If a matching customer is found with both the specified username and password, the customer object is returned. If no matching customer is found, the method returns `null` to indicate that the customer was not found.Parameters
- Parameters:
username
- The username of the customer to retrieve.password
- The password of the customer to retrieve.- Returns:
- A
Customer
object if a match is found, or `null` if the customer is not found based on the provided username and password.
-