Skip to content

Commit c277454

Browse files
committed
completed the task
1 parent 5b170ac commit c277454

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package mate.academy.exception
22

33
// Provide your code here for PasswordValidationException class
4+
class PasswordValidationException(message: String) : Exception(message)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
package mate.academy.service
22

3+
import mate.academy.exception.PasswordValidationException
4+
35
// This class will validate password requirements
46
class PasswordValidator {
7+
8+
companion object {
9+
private const val MIN_PASSWORD_LENGTH = 10
10+
}
11+
@Throws(PasswordValidationException::class)
512
fun validate(password: String, repeatPassword: String) {
13+
14+
if (password != repeatPassword || password.length < MIN_PASSWORD_LENGTH) {
15+
throw PasswordValidationException("Wrong passwords")
16+
}
617
// write your code here
718
}
819
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package mate.academy.service
22

3+
import mate.academy.exception.PasswordValidationException
34
import mate.academy.model.User
45

56
// This class represents a user service with user registration functionality
67
class UserService {
8+
private val passwordValidator = PasswordValidator()
79

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

1315
fun registerUser(username: String, password: String, repeatPassword: String) : String {
16+
return try {
17+
// Викликаємо валідацію
18+
passwordValidator.validate(password, repeatPassword)
19+
20+
// Якщо валідація пройшла успішно, створюємо об'єкт і зберігаємо
21+
val user = User(username, password)
22+
saveUser(user)
23+
} catch (e: PasswordValidationException) {
24+
// Обробка випадку, якщо паролі невірні
25+
"Your passwords are incorrect. Try again."
26+
}
1427

1528
}
1629
}

0 commit comments

Comments
 (0)