Advertisement
deyanmalinov

3. Validation Data

Jun 9th, 2020
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package DPM;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         int lines = Integer.parseInt(scan.nextLine());
  9.         List<Person> people = new ArrayList<>();
  10.         for (int i = 0; i < lines; i++) {
  11.             Person man = null;
  12.             String[] line = scan.nextLine().split(" ");
  13.             String fname = line[0];
  14.             String sname = line[1];
  15.             int age = Integer.parseInt(line[2]);
  16.             double salary = Double.parseDouble(line[3]);
  17.             try{
  18.                 man = new Person(fname, sname, age, salary);
  19.             }catch (IllegalArgumentException exception){
  20.                 System.out.println(exception.getMessage());
  21.             }
  22.             if (man != null){
  23.                 people.add(man);
  24.             }
  25.         }
  26.         double bonus = Double.parseDouble(scan.nextLine());
  27.         for (Person person : people) {
  28.             person.increaseSalary(bonus);
  29.         }
  30.         for (Person person : people) {
  31.             System.out.println(person.toString());
  32.         }
  33.     }
  34. }
  35. --------------------------------------------------------------------------
  36. package DPM;
  37. import java.text.DecimalFormat;
  38. public class Person {
  39.     private String fname;
  40.     private String sname;
  41.     private int age;
  42.     private double salary;
  43.     public Person(String fname, String sname, int age, double salary){
  44.         this.setFname(fname);
  45.         this.setSname(sname);
  46.         this.setAge(age);
  47.         this.setSalary(salary);
  48.     }
  49.     public void setSalary(double salary){
  50.         if (salary < 460){
  51.             throw new IllegalArgumentException
  52.                     ("Salary cannot be less than 460 leva");
  53.         }
  54.         this.salary=salary;
  55.     }
  56.     public void setFname(String fname){
  57.         if (fname.length() < 3){
  58.             throw new IllegalArgumentException
  59.                     ("First name cannot be less than 3 symbols");
  60.         }
  61.         this.fname=fname;
  62.     }
  63.     public void setSname(String sname){
  64.         if (sname.length() < 3){
  65.             throw new IllegalArgumentException
  66.                     ("Last name cannot be less than 3 symbols");
  67.         }
  68.         this.sname=sname;
  69.     }
  70.     public void setAge(int age){
  71.         if (age < 1){
  72.             throw new IllegalArgumentException
  73.                     ("Age cannot be zero or negative integer");
  74.         }
  75.         this.age=age;
  76.     }
  77.     public String getFname(){
  78.         return this.fname;
  79.     }
  80.     public String getSname(){
  81.         return this.sname;
  82.     }
  83.     public int getAge(){
  84.         return this.age;
  85.     }
  86.     public double getSalary(){
  87.         return this.salary;
  88.     }
  89.     public void increaseSalary(double bonus){
  90.         if (this.age > 30){
  91.             this.salary += this.salary * bonus/100;
  92.         }else {
  93.             this.salary += this.salary * bonus/200;
  94.         }
  95.     }
  96.     @Override
  97.     public String toString() {
  98.         DecimalFormat df = new DecimalFormat("#.0##############");
  99.         return String.format("%s %s gets %s leva",
  100.                 this.getFname(), this.getSname(), df.format(this.getSalary()));
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement