Advertisement
ForestFox

госы_люди_dart

Jun 1st, 2025
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.75 KB | None | 0 0
  1. void main() {
  2.   // Create a list containing three objects of each class.
  3.   List<People> peopleList = [
  4.     People(name: 'Alice', age: 25),
  5.     People(name: 'Bob', age: 32),
  6.     People(name: 'Charlie', age: 28),
  7.     Worker(name: 'David', age: 40, post: 'Engineer', salary: 60000),
  8.     Worker(name: 'Eve', age: 35, post: 'Manager', salary: 75000),
  9.     Worker(name: 'Fred', age: 29, post: 'Analyst', salary: 55000),
  10.     Student(name: 'Grace', age: 20, marks: [85, 90, 92], subjects: ['Math', 'Science', 'History']),
  11.     Student(name: 'Henry', age: 22, marks: [78, 82, 88], subjects: ['English', 'Art', 'Music']),
  12.     Student(name: 'Ivy', age: 21, marks: [92, 95, 98], subjects: ['Physics', 'Chemistry', 'Biology']),
  13.   ];
  14.  
  15.   // Print information about each person using the toString method.
  16.   print('Information about each person:');
  17.   for (var person in peopleList) {
  18.     print(person);
  19.   }
  20.  
  21.   // Print the names of those who are younger than 30 years old.
  22.   print('\nNames of people younger than 30:');
  23.   for (var person in peopleList) {
  24.     if (person.age < 30) {
  25.       print(person.name);
  26.     }
  27.   }
  28.  
  29.   // Calculate the average salary of all workers.
  30.   double averageSalary = 0;
  31.   int workerCount = 0;
  32.   for (var person in peopleList) {
  33.     if (person is Worker) {
  34.       averageSalary += person.salary;
  35.       workerCount++;
  36.     }
  37.   }
  38.   averageSalary = workerCount > 0 ? averageSalary / workerCount : 0;
  39.  
  40.   // Count the number of Worker objects with a salary below the average.
  41.   int workersBelowAverage = 0;
  42.   for (var person in peopleList) {
  43.     if (person is Worker && person.salary < averageSalary) {
  44.       workersBelowAverage++;
  45.     }
  46.   }
  47.  
  48.   print('\nNumber of workers with salary below average: $workersBelowAverage');
  49.  
  50.   // Print information about the subjects of each Student.
  51.   print('\nSubjects of each student:');
  52.   for (var person in peopleList) {
  53.     if (person is Student) {
  54.       print('${person.name}: ${person.subjects}');
  55.     }
  56.   }
  57. }
  58.  
  59. class People {
  60.   final String name;
  61.   final int age;
  62.  
  63.   People({required this.name, required this.age});
  64.  
  65.   @override
  66.   String toString() => 'Name: $name, Age: $age';
  67. }
  68.  
  69. final class Worker extends People {
  70.   final String post;
  71.   final int salary;
  72.  
  73.   Worker({
  74.     required super.name,
  75.     required super.age,
  76.     required this.post,
  77.     required this.salary,
  78.   });
  79.  
  80.   @override
  81.   String toString() => 'Name: $name, Age: $age, Post: $post, Salary: $salary';
  82. }
  83.  
  84. final class Student extends People {
  85.   final List<int> marks;
  86.   final List<String> subjects;
  87.  
  88.   Student({
  89.     required super.name,
  90.     required super.age,
  91.     required this.marks,
  92.     required this.subjects,
  93.   });
  94.  
  95.   @override
  96.   String toString() => 'Name: $name, Age: $age, Marks: $marks, Subjects: $subjects';
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement