Skip to content

Instantly share code, notes, and snippets.

@fsoftwareengineer
Created April 22, 2019 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsoftwareengineer/ee4c3eaeb4d33a2f55c39e818d161d0a to your computer and use it in GitHub Desktop.
Save fsoftwareengineer/ee4c3eaeb4d33a2f55c39e818d161d0a to your computer and use it in GitHub Desktop.
Java Interface Validator Tutorial
public class AgeUserValidator implements UserValidator {
@Override
public void validate(User user) {
if(user.age <= 0 || user.age > 99) {
System.out.println("에러: 올바른 나이를 입력해주세요.");
}
}
}
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);
}
}
}
public class NameUserValidator implements UserValidator {
@Override
public void validate(User user) {
if(user.name == null || user.name.isEmpty()) {
System.out.println("에러: 이름을 입력해주세요.");
}
}
}
public class User {
public String name;
public int age;
}
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