Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person
- {
- private String name;
- private Date birthday;
- public Person()
- {
- this(“”,new Date(0,0,0));
- }
- public Person(String name, Date birthday)
- {
- this.name=name;
- this.birthday=birthday;
- }
- public String getName()
- {
- return name;
- }
- public String toString()
- {
- return String.format("Name: %s\nBirthDay: %s\n",name,birthday);
- }
- }
- public class Employee extends Person
- {
- private double salary;
- public Employee()
- {
- super();
- salary=0;
- }
- public Employee(String name, Date birthday, double salary)
- {
- super(name,birthday);
- this.salary=salary;
- }
- public double getSalary()
- {
- return salary;
- }
- public String toString()
- {
- return String.format("%sSalary: %.2f Vnd\n",super.toString(),salary);
- }
- }
- public class Manager extends Employee
- {
- private Employee assistant;
- public Manager()
- {
- super();
- }
- public Manager(String name, Date birthday, double salary)
- {
- super(name,birthday,salary);
- }
- public void setAssistant(Employee assistant)
- {
- this.assistant=assistant;
- }
- public String toString()
- {
- return String.format("%sAssistant: %s",super.toString(),assistant);
- }
- }
- public class Date
- {
- private int month; // 1-12
- private int day; // 1-31 based on month
- private int year; // any year
- // constructor: call checkMonth to confirm proper value for month;
- // call checkDay to confirm proper value for day
- public Date( int theMonth, int theDay, int theYear )
- {
- month = checkMonth( theMonth ); // validate month
- year = checkYear(theYear); // validate year
- day = checkDay( theDay ); // validate day
- } // end Date constructor
- // utility method to confirm proper month value
- private int checkMonth( int testMonth )
- {
- if ( testMonth > 0 && testMonth <= 12 ) // validate month
- return testMonth;
- else // month is invalid
- {
- System.out.printf(
- "Invalid month (%d) set to 1.", testMonth );
- return 1; // maintain object in consistent state
- } // end else
- } // end method checkMonth
- // utility method to confirm proper day value based on month and year
- private int checkDay( int testDay )
- {
- int daysPerMonth[] =
- { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- if (year%400==0||(year%4==0&&year%100!=0))
- daysPerMonth[2]=29;
- // check if day in range for month
- if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
- return testDay;
- // check for leap year
- if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
- ( year % 4 == 0 && year % 100 != 0 ) ) )
- return testDay;
- System.out.printf( "Invalid day (%d) set to 1.", testDay );
- return 1; // maintain object in consistent state
- } // end method checkDay
- private int checkYear( int testYear )
- {
- if (testYear>=0)
- return testYear;
- System.out.printf( "Invalid Year (%d) set to 0.", testYear );
- return 0;
- }
- public void nextDay()
- {
- int daysPerMonth[] =
- { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- if (year%400==0||(year%4==0&&year%100!=0))
- daysPerMonth[2]=29;
- if (day==daysPerMonth[month])
- {
- day=1;
- if (month==12)
- {
- month=1;
- year++;
- }
- else
- month++;
- }
- else
- day++;
- }
- // return a String of the form month/day/year
- public String toString()
- {
- return String.format( "%d/%d/%d", month, day, year );
- } // end method toString
- } // end class Date
- public class PeopleTest
- {
- public static void main(String args[])
- {
- // Test Task 1.a
- Employee newbie = new Employee("Newbie", new Date(2,10,1989), 1000000);
- Manager boss = new Manager("Boss", new Date(2,23,1979), 4000000);
- boss.setAssistant(newbie);
- Manager bigBoss = new Manager("Big Boss", new Date(12,3,1969), 10000000);
- bigBoss.setAssistant(boss);
- System.out.println(newbie);
- System.out.println(boss);
- System.out.println(bigBoss);
- // Test Task 1.b
- Person people[] = {newbie,boss,bigBoss};
- for (int i=0;i<3;i++)
- {
- System.out.println(people[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment