Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package Person;
  2.  
  3. class Person {
  4.     String name;
  5.     int age;
  6.     String email;
  7.  
  8.     public Person(String name, int age) {
  9.         try {
  10.             this.setName(name);
  11.         } catch (Exception e) {
  12.             e.printStackTrace();
  13.         }
  14.         try {
  15.             this.setAge(age);
  16.         } catch (Exception e) {
  17.             e.printStackTrace();
  18.         }
  19.         this.setEmail("no email");
  20.     }
  21.  
  22.     public Person(String name, int age, String email) {
  23.         try {
  24.             this.setName(name);
  25.         } catch (Exception e) {
  26.             e.printStackTrace();
  27.         }
  28.         try {
  29.             this.setAge(age);
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.         this.setEmail(email);
  34.     }
  35.  
  36.     public String getName() {
  37.         return name;
  38.     }
  39.  
  40.     public void setName(String name) throws Exception {
  41.         if (name.equals(null) || name.equals("")) {
  42.             throw new Exception("Invalid name");
  43.         }
  44.         this.name = name;
  45.     }
  46.  
  47.     public int getAge() {
  48.         return age;
  49.     }
  50.  
  51.     public void setAge(int age) throws Exception {
  52.         this.age = age;
  53.         if (age < 1 || age > 100) {
  54.             throw new Exception("Invalid age");
  55.         }
  56.     }
  57.  
  58.     public String getEmail() {
  59.         return email;
  60.     }
  61.  
  62.     public void setEmail(String email) {
  63.         if (email.equals(null)) {
  64.             this.email = " ";
  65.         } else {
  66.             this.email = email;
  67.         }
  68.     }
  69.  
  70.     public void toString(String name, int age, String email) {
  71.         System.out.println(getName() + " is " + getAge() + " years old  and has email:" + getEmail());
  72.     }
  73. }
  74.  
  75. public class Main {
  76.     public static void main(String[] args) {
  77.         Person pesho = new Person("Pesho", 244, "pesho@abv.bg");
  78.         Person gosho = new Person("Gosho", 30);
  79.         pesho.toString(pesho.getName(), pesho.getAge(), pesho.getEmail());
  80.         gosho.toString(gosho.getName(), gosho.getAge(), gosho.getEmail());
  81.  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement