Skip to content

Commit c56855e

Browse files
Task completed)
1 parent 5b170ac commit c56855e

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

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

3-
// Provide your code here for PasswordValidationException class
3+
class PasswordValidationException(message: String): RuntimeException(message)
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
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_PASSWORDS_LENGTH = 10
10+
}
11+
512
fun validate(password: String, repeatPassword: String) {
6-
// write your code here
13+
if (password != repeatPassword || password.length < MIN_PASSWORDS_LENGTH) {
14+
throw PasswordValidationException("Wrong passwords")
15+
}
716
}
817
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
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 {
7-
8+
private val passwordValidator = PasswordValidator()
89
fun saveUser(user: User) : String {
910
// This is where you would typically save the user to a database
1011
return "User ${user.username} saved successfully."
1112
}
1213

1314
fun registerUser(username: String, password: String, repeatPassword: String) : String {
15+
return try {
16+
passwordValidator.validate(password, repeatPassword)
17+
18+
val user = User(username = username,
19+
password = password)
1420

21+
saveUser(user)
22+
} catch (e: PasswordValidationException) {
23+
"Your passwords are incorrect. Try again."
24+
}
1525
}
1626
}

0 commit comments

Comments
 (0)