Created
April 22, 2019 00:17
-
-
Save fsoftwareengineer/ee4c3eaeb4d33a2f55c39e818d161d0a to your computer and use it in GitHub Desktop.
Java Interface Validator Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AgeUserValidator implements UserValidator { | |
@Override | |
public void validate(User user) { | |
if(user.age <= 0 || user.age > 99) { | |
System.out.println("에러: 올바른 나이를 입력해주세요."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
final static UserValidator[] userValidator = {new NameUserValidator(), new AgeUserValidator()}; | |
public static void main(String[] args) { | |
User user = new User(); | |
System.out.print("## 이름 :"); | |
user.name = new Scanner(System.in).next(); | |
System.out.print("## 나이 :"); | |
user.age = new Scanner(System.in).nextInt(); | |
for (int i = 0; i < userValidator.length; i++) { | |
userValidator[i].validate(user); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NameUserValidator implements UserValidator { | |
@Override | |
public void validate(User user) { | |
if(user.name == null || user.name.isEmpty()) { | |
System.out.println("에러: 이름을 입력해주세요."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
public String name; | |
public int age; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface UserValidator { | |
void validate(User user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment