Skip to content

Instantly share code, notes, and snippets.

@internetova
Last active November 22, 2020 18:13
Show Gist options
  • Save internetova/8b993d8983a4f1c00a12ac6e6c3c6802 to your computer and use it in GitHub Desktop.
Save internetova/8b993d8983a4f1c00a12ac6e6c3c6802 to your computer and use it in GitHub Desktop.
/*
Реализуйте класс Student (Студент), который будет наследоваться от класса User. Класс должен иметь следующие свойства:
yearOfAdmission (год поступления в вуз): инициализируется в конструкторе
currentCourse (текущий курс): DateTime.now - yearOfAdmission
Класс должен иметь метод toString() , с помощью которого можно вывести:
имя и фамилию студента - используя родительскую реализацию toString
год поступления
текущий курс
*/
void main() {
final student =
Student(firstName: 'Tatiana', lastName: 'Sugina', yearOfAdmission: 2020);
final about = student.toString();
print(about);
}
class User {
const User({required this.firstName, required this.lastName});
final String firstName;
final String lastName;
@override
String toString() => '$firstName $lastName';
}
class Student extends User {
const Student(
{required String firstName,
required String lastName,
required this.yearOfAdmission})
: super(firstName: firstName, lastName: lastName);
final int yearOfAdmission;
int get currentCourse {
const startStudyMonth = 9;
final currentYear = DateTime.now().year;
final currentMonth = DateTime.now().month;
var currentCourse = currentYear - yearOfAdmission;
if (currentCourse == 0) {
currentCourse = 1;
} else if (currentCourse < 0) {
currentCourse = 0;
} else if (currentCourse > 0 && currentMonth >= startStudyMonth) {
currentCourse += 1;
}
return currentCourse;
}
@override
String toString() {
var result = super.toString();
result +=
'\nГод поступления: $yearOfAdmission, текущий курс $currentCourse';
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment