Skip to content

Commit 8e9584f

Browse files
committed
solving
1 parent 5b170ac commit 8e9584f

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

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

3+
import java.lang.Exception
4+
35
// Provide your code here for PasswordValidationException class
6+
public class PasswordValidationException(message: String) : Exception(message)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package mate.academy.service
22

3+
import mate.academy.exception.PasswordValidationException
4+
35
// This class will validate password requirements
46
class PasswordValidator {
57
fun validate(password: String, repeatPassword: String) {
68
// write your code here
9+
if (password.length < 10 || password != repeatPassword) {
10+
throw PasswordValidationException("Wrong passwords")
11+
}
712
}
813
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
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 passwordValidator: 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

13-
fun registerUser(username: String, password: String, repeatPassword: String) : String {
14-
15+
fun registerUser(username: String,
16+
password: String,
17+
repeatPassword: String) : String {
18+
try {
19+
passwordValidator.validate(password, repeatPassword)
20+
} catch (ex: PasswordValidationException) {
21+
return "Your passwords are incorrect. Try again."
22+
}
23+
return saveUser(User(username, password))
1524
}
1625
}

0 commit comments

Comments
 (0)