Duclv

Untitled

Oct 22nd, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. public class Person
  2. {
  3.     private String name;
  4.     private Date birthday;
  5.     public Person()
  6.     {
  7.         this(“”,new Date(0,0,0));
  8.     }
  9.     public Person(String name, Date birthday)
  10.     {
  11.         this.name=name;
  12.         this.birthday=birthday;
  13.     }
  14.     public String getName()
  15.     {
  16.         return name;
  17.     }
  18.     public String toString()
  19.     {
  20.         return String.format("Name: %s\nBirthDay: %s\n",name,birthday);
  21.     }
  22. }
  23.  
  24. public class Employee extends Person
  25. {
  26.     private double salary;
  27.     public Employee()
  28.     {
  29.         super();
  30.         salary=0;
  31.     }
  32.     public Employee(String name, Date birthday, double salary)
  33.     {
  34.         super(name,birthday);
  35.         this.salary=salary;
  36.     }
  37.     public double getSalary()
  38.     {
  39.         return salary;
  40.     }
  41.     public String toString()
  42.     {
  43.         return String.format("%sSalary: %.2f Vnd\n",super.toString(),salary);
  44.     }
  45. }
  46.  
  47. public class Manager extends Employee
  48. {
  49.     private Employee assistant;
  50.     public Manager()
  51.     {
  52.         super();
  53.     }
  54.     public Manager(String name, Date birthday, double salary)
  55.     {
  56.         super(name,birthday,salary);
  57.     }
  58.     public void setAssistant(Employee assistant)
  59.     {
  60.         this.assistant=assistant;
  61.     }
  62.     public String toString()
  63.     {
  64.         return String.format("%sAssistant: %s",super.toString(),assistant);
  65.     }
  66. }
  67.  
  68. public class Date
  69. {
  70.     private int month; // 1-12
  71.     private int day;   // 1-31 based on month
  72.     private int year;  // any year
  73.  
  74.     // constructor: call checkMonth to confirm proper value for month;
  75.     // call checkDay to confirm proper value for day
  76.     public Date( int theMonth, int theDay, int theYear )
  77.     {
  78.         month = checkMonth( theMonth ); // validate month
  79.         year = checkYear(theYear); // validate year
  80.         day = checkDay( theDay ); // validate day
  81.     } // end Date constructor
  82.  
  83.     // utility method to confirm proper month value
  84.     private int checkMonth( int testMonth )
  85.     {
  86.         if ( testMonth > 0 && testMonth <= 12 ) // validate month
  87.         return testMonth;
  88.         else // month is invalid
  89.         {
  90.             System.out.printf(
  91.                 "Invalid month (%d) set to 1.", testMonth );
  92.             return 1; // maintain object in consistent state
  93.         } // end else
  94.     } // end method checkMonth
  95.  
  96.     // utility method to confirm proper day value based on month and year
  97.     private int checkDay( int testDay )
  98.     {
  99.         int daysPerMonth[] =
  100.         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  101.         if (year%400==0||(year%4==0&&year%100!=0))
  102.             daysPerMonth[2]=29;
  103.         // check if day in range for month
  104.         if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
  105.             return testDay;
  106.  
  107.         // check for leap year
  108.         if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
  109.             ( year % 4 == 0 && year % 100 != 0 ) ) )
  110.             return testDay;
  111.  
  112.         System.out.printf( "Invalid day (%d) set to 1.", testDay );
  113.         return 1;  // maintain object in consistent state
  114.     } // end method checkDay
  115.  
  116.     private int checkYear( int testYear )
  117.     {
  118.         if (testYear>=0)
  119.             return testYear;
  120.         System.out.printf( "Invalid Year (%d) set to 0.", testYear );
  121.         return 0;
  122.     }
  123.  
  124.     public void nextDay()
  125.     {
  126.         int daysPerMonth[] =
  127.         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  128.         if (year%400==0||(year%4==0&&year%100!=0))
  129.             daysPerMonth[2]=29;
  130.         if (day==daysPerMonth[month])
  131.         {
  132.             day=1;
  133.             if (month==12)
  134.             {
  135.                 month=1;
  136.                 year++;
  137.             }
  138.             else
  139.                 month++;
  140.         }
  141.         else
  142.             day++;
  143.     }
  144.     // return a String of the form month/day/year
  145.     public String toString()
  146.     {
  147.         return String.format( "%d/%d/%d", month, day, year );
  148.     } // end method toString
  149. } // end class Date
  150.  
  151. public class PeopleTest
  152. {
  153.     public static void main(String args[])
  154.     {
  155.         // Test Task 1.a
  156.         Employee newbie = new Employee("Newbie", new Date(2,10,1989), 1000000);
  157.         Manager boss = new Manager("Boss", new Date(2,23,1979), 4000000);
  158.         boss.setAssistant(newbie);
  159.         Manager bigBoss = new Manager("Big Boss", new Date(12,3,1969), 10000000);
  160.         bigBoss.setAssistant(boss);
  161.         System.out.println(newbie);
  162.         System.out.println(boss);
  163.         System.out.println(bigBoss);
  164.  
  165.         // Test Task 1.b
  166.         Person people[] = {newbie,boss,bigBoss};
  167.         for (int i=0;i<3;i++)
  168.         {
  169.             System.out.println(people[i]);
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment