Add class Person with code from the previous task to the project.
Add new class Worker, which inherits class Person.
Class Worker should consist next members:
a) fields: workplace and salary.
b) two properties for access to these fields
c) constructor for initialisation of all fields (remember about fields in the base class)
d) method OutPut() for writing all fields into Console
e) method SalaryIncrease() for increasing salary by formula salary=salary+Age
In the method Main() create array of Workers and fill it with data from Console by calling appropriate constructor.
Output all Workers with Age more then 20 years.
Increase salary for workers, whose salary is more then 200.Output result.
Sort array of Workers by name (for this task class Worker should implement interface IComparable). Output result.
package LessonsFromC.Homework3.worker;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import LessonsFromC.Homework2.person.Person;
public class Worker extends Person implements IComparable{
private String workplace;
private int salary = 0;
Worker() {
}
Worker(String theName, int theBirthDate, int theSalary) {
super.setName(theName);
super.setAge(theBirthDate);
this.salary = theSalary;
}
public String getWorkplace() {
return workplace;
}
public void setWorkplace(String workplace) {
this.workplace = workplace;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void outPut() {
System.out.println(getSalary());
System.out.println(getWorkplace());
}
public int salaryIncrease() {
salary = salary + Age();
return salary;
}
public static Comparator<Person> personNameComparator = new Comparator<Person>() {
public int compare(Person person1, Person person2) {
String personName1 = person1.getName().toUpperCase();
String personName2 = person2.getName().toUpperCase();
return personName1.compareTo(personName2);
}
};
}
______________________
package LessonsFromC.Homework3.worker;
import java.util.Comparator;
import LessonsFromC.Homework2.person.Person;
public interface IComparable{
public static Comparator<Person> personNameComparator = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
return 0;
}};
}
______________________
package LessonsFromC.Homework3.worker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class TestWorker {
public static void main(String[] args) {
// create array of Workers and fill it with data from Console
Scanner sc = new Scanner(System.in);
List<Worker> workers = new ArrayList<Worker>();
for (int i = 0; i < 2; i++) {
System.out.println("Type name");
String name = sc.next();
System.out.println("Type the year of birthday");
int birthDate = sc.nextInt();
System.out.println("Type the salary");
int salary = sc.nextInt();
workers.add(i, new Worker(name, birthDate, salary));
}
List<Worker> newWorkers = new ArrayList<Worker>();
for (Worker wk : workers) {
// Output all Workers with Age more then 20 years.
if (wk.Age() > 20) {
newWorkers.add(wk);
System.out.println(wk + " The age of person is more then 20: "
+ wk.Age());
}
// Increase salary for workers, whose salary is more then 200. Output result.
if (wk.getSalary() > 200) {
System.out.println("New salary is " + wk.salaryIncrease());
}
}
// Sort array of Workers by name (for this task class Worker should implement interface IComparable). Output result.
Collections.sort(workers, Worker.personNameComparator);
System.out.println(workers);
}
}
Немає коментарів:
Дописати коментар