Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/kotlin/mate/academy/Main.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package mate.academy

import mate.academy.service.PasswordValidator
import mate.academy.service.UserService

// Test the UserService implementation
fun main() {
val userService = UserService()

// Case where passwords are incorrect
println(userService.registerUser("john_doe", "short", "short"))
println(userService.registerUser("john_doe1", "short", "short"))
// Output: Your passwords are incorrect. Try again.

// Case where passwords are correct
println(userService.registerUser("jane_doe", "correct_password", "correct_password"))
println(userService.registerUser("jane_doe2", "correct_password", "correct_password"))
// Output: User jane_doe saved successfully.
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package mate.academy.exception

// Provide your code here for PasswordValidationException class
class PasswordValidationException(message: String) : Exception(message)
7 changes: 6 additions & 1 deletion src/main/kotlin/mate/academy/service/PasswordValidator.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package mate.academy.service

import mate.academy.exception.PasswordValidationException

const val PASSWORD_LENGTH_MIN = 10
// This class will validate password requirements
class PasswordValidator {
fun validate(password: String, repeatPassword: String) {
// write your code here
if (password != repeatPassword || password.length < PASSWORD_LENGTH_MIN) {
throw PasswordValidationException("Wrong passwords")
}
}
}
14 changes: 11 additions & 3 deletions src/main/kotlin/mate/academy/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package mate.academy.service

import mate.academy.exception.PasswordValidationException
import mate.academy.model.User

// This class represents a user service with user registration functionality
class UserService {

fun saveUser(user: User) : String {
fun saveUser(user: User): String {
// This is where you would typically save the user to a database
return "User ${user.username} saved successfully."
}

fun registerUser(username: String, password: String, repeatPassword: String) : String {

@Throws
fun registerUser(username: String, password: String, repeatPassword: String): String {
val validator = PasswordValidator()
try {
validator.validate(password, repeatPassword)
return saveUser(User(username, password))
} catch (ex: PasswordValidationException) {
return "Your passwords are incorrect. Try again."
}
}
}