deyanmalinov

Problem 3. Mankind

Jun 27th, 2020
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. public class Main {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  8.         String[] lineStudent = reader.readLine().split("\\s+");
  9.         String[] lineWorker = reader.readLine().split("\\s+");
  10.         try {
  11.             Student student = new Student(lineStudent[0], lineStudent[1], lineStudent[2]);
  12.             Worker worker = new Worker(lineWorker[0], lineWorker[1],
  13.                     Double.parseDouble(lineWorker[2]), Double.parseDouble(lineWorker[3]));
  14.             System.out.println(student);
  15.             System.out.println(worker);
  16.         }catch (IllegalArgumentException msg) {
  17.             System.out.println(msg.getMessage());
  18.         }
  19.     }
  20. }--------------------------------------
  21. package DPM;
  22. public class Human {
  23.     private String fname;
  24.     private String sname;
  25.     public Human(String fname, String sname){
  26.         this.setFname(fname);
  27.         this.setSname(sname);
  28.     }
  29.     public String getFname() {
  30.         return fname;
  31.     }
  32.     private void setFname(String fname) {
  33.         if (fname.length() < 4){
  34.             throw new IllegalArgumentException("Expected upper case letter!Argument: firstName");
  35.         }
  36.         if (!Character.isUpperCase(fname.charAt(0))){
  37.             throw new IllegalArgumentException("Expected length at least 4 symbols!Argument: firstName");
  38.         }
  39.         this.fname = fname;
  40.     }
  41.     public String getSname() {
  42.         return sname;
  43.     }
  44.     protected void setSname(String sname) {
  45.         if (!Character.isUpperCase(sname.charAt(0))){
  46.             throw new IllegalArgumentException("Expected upper case letter!Argument: lastName");
  47.         }
  48.         if (sname.length() < 3){
  49.             throw new IllegalArgumentException("Expected length at least 3 symbols!Argument: lastName");
  50.         }
  51.         this.sname = sname;
  52.     }
  53.     @Override
  54.     public String toString() {
  55.         StringBuilder human = new StringBuilder();
  56.  
  57.         human.append("First Name: ").append(this.fname).append(System.lineSeparator())
  58.                 .append("Last Name: ").append(this.sname);
  59.         return human.toString();
  60.     }
  61. }-------------------------------------
  62. package DPM;
  63. public class Student extends Human{
  64.     private String number;
  65.     public Student(String fname, String sname, String number){
  66.         super(fname, sname);
  67.         this.setNumber(number);
  68.     }
  69.     public String getNumber() {
  70.         return number;
  71.     }
  72.     private void setNumber(String  number) {
  73.         if (number.length() < 5 || number.length() > 10){
  74.             throw new IllegalArgumentException("Invalid faculty number!");
  75.         }
  76.         this.number = number;
  77.     }
  78.     @Override
  79.     public String toString() {
  80.         StringBuilder student = new StringBuilder();
  81.         student.append(super.toString()).append(System.lineSeparator())
  82.                 .append("Faculty number: ").append(this.number);
  83.         return student.toString();
  84.     }
  85. }----------------------------------
  86. package DPM;
  87. public class Worker extends Human{
  88.     private double salary;
  89.     private double hours;
  90.     public Worker(String fname, String sname, double salary, double hours){
  91.         super(fname, sname);
  92.         this.setSalary(salary);
  93.         this.setHours(hours);
  94.     }
  95.     public double getSalary() {
  96.         return salary;
  97.     }
  98.     private void setSalary(double salary) {
  99.         if (salary <= 10){
  100.             throw new IllegalArgumentException("Expected value mismatch!Argument: weekSalary");
  101.         }
  102.         this.salary = salary;
  103.     }
  104.     public double getHours() {
  105.         return hours;
  106.     }
  107.     @Override
  108.     protected void setSname(String sname) {
  109.         if (sname.length() <=3){
  110.             throw new IllegalArgumentException("Expected length more than 3 symbols!Argument: lastName");
  111.         }
  112.         super.setSname(sname);
  113.     }
  114.     private void setHours(double hours) {
  115.         if (hours < 1 || hours > 12){
  116.             throw new IllegalArgumentException("Expected value mismatch!Argument: workHoursPerDay");
  117.         }
  118.         this.hours = hours;
  119.     }
  120.     private double getSalaryPerHour() {
  121.          double hourSalary = this.salary / (this.hours * 7);
  122.          return hourSalary;
  123.     }
  124.     @Override
  125.     public String toString() {
  126.         StringBuilder worker = new StringBuilder();
  127.         worker.append(super.toString()).append(System.lineSeparator())
  128.                 .append("Week Salary: ").append(String.format("%.2f", this.salary)).append(System.lineSeparator())
  129.                 .append("Hours per day: ").append(String.format("%.2f", this.hours)).append(System.lineSeparator())
  130.                 .append("Salary per hour: ").append(String.format("%.2f", this.getSalaryPerHour()));
  131.         return worker.toString();
  132.     }
  133. }
RAW Paste Data