Skip to content

Commit bc58c6a

Browse files
done
1 parent 5b170ac commit bc58c6a

3 files changed

Lines changed: 17 additions & 4 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) : Exception(message)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package mate.academy.service
22

3-
// This class will validate password requirements
3+
import mate.academy.exception.PasswordValidationException
4+
5+
const val PASSWORD_MIN_LENGTH = 10
6+
47
class PasswordValidator {
58
fun validate(password: String, repeatPassword: String) {
6-
// write your code here
9+
if (password != repeatPassword || password.length < PASSWORD_MIN_LENGTH) {
10+
throw PasswordValidationException("Wrong passwords")
11+
}
712
}
813
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
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+
val validator = 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 {
14-
16+
try {
17+
validator.validate(password, repeatPassword)
18+
val user = User(username, password)
19+
return saveUser(user)
20+
} catch (_: PasswordValidationException) {
21+
return "Your passwords are incorrect. Try again."
22+
}
1523
}
1624
}

0 commit comments

Comments
 (0)