Compare commits
39 Commits
@ -1,4 +1,88 @@
|
|||||||
# Lab 2
|
# Lab 2
|
||||||
|
|
||||||
> "И тут я обнаружил что случайно сделал 3-5 задачи в 1-2"
|
|
||||||
|
## Диаграмма классов:
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
classDiagram
|
||||||
|
class BinarySearchTree {
|
||||||
|
- root : Node
|
||||||
|
+ add(student : Student) : void
|
||||||
|
+ each(&block) : void
|
||||||
|
- insert(node : Node, student : Student) : Node
|
||||||
|
- in_order_traversal(node : Node, &block) : void
|
||||||
|
}
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
- student : Student
|
||||||
|
- left : Node
|
||||||
|
- right : Node
|
||||||
|
+ Node(student : Student)
|
||||||
|
}
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
- id : String
|
||||||
|
- git : String
|
||||||
|
- phone : String?
|
||||||
|
- telegram : String?
|
||||||
|
- email : String?
|
||||||
|
+ Person(id : String, git : String, phone: String, telegram: String, email: String)
|
||||||
|
+ phone=(String) : Boolean
|
||||||
|
+ telegram=(String) : Boolean
|
||||||
|
+ email=(String) : Boolean
|
||||||
|
+ surname_initials() : NotImplementedError
|
||||||
|
+ valid_phone_number() : Boolean <<class>>
|
||||||
|
+ valid_telegram() : Boolean <<class>>
|
||||||
|
+ valid_email() : Boolean <<class>>
|
||||||
|
+ git_present() : Boolean
|
||||||
|
+ contact_present() : Boolean
|
||||||
|
+ get_first_contact() : String
|
||||||
|
+ git=(git : String) : void
|
||||||
|
+ validate_id(id: String) : void <<class>>
|
||||||
|
+ validate_git(git : String) : void <<class>>
|
||||||
|
- valid_git?(git : String) : Boolean
|
||||||
|
- valid_id?(id : String) : Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
class StudentRepository {
|
||||||
|
+ read_from_txt(file_path : String) : List~Student~ <<class>>
|
||||||
|
+ write_to_txt(file_path : String, students : List~Student~) <<class>>
|
||||||
|
}
|
||||||
|
|
||||||
|
class StudentShort {
|
||||||
|
- surname_initials : String
|
||||||
|
+ get_surname_initials() : String
|
||||||
|
+ StudentShort(id : String, surname_initials : String, phone: String, telegram: String, email: String)
|
||||||
|
+ from_student(student : Student) : StudentShort <<class>>
|
||||||
|
+ from_string(id : String, info_string : String) : StudentShort <<class>>
|
||||||
|
+ to_s() : String
|
||||||
|
- parse_contact_string(contact_string: String) : Array<String> <<class>>
|
||||||
|
}
|
||||||
|
|
||||||
|
class Student {
|
||||||
|
- surname : String
|
||||||
|
- name : String
|
||||||
|
- patronymic : String
|
||||||
|
- birth_date : Date
|
||||||
|
- const NAME_REGEX : String
|
||||||
|
+ Student(id : String, git : String, phone: String, telegram: String, email: String, surname : String, name : String, patronymic : String, birth_date : Date)
|
||||||
|
+ from_string(student_string : String) : Student <<class>>
|
||||||
|
+ surname_initials() : String
|
||||||
|
+ to_s() : String
|
||||||
|
+ get_info() : String
|
||||||
|
+ surname=(surname : String)
|
||||||
|
+ name=(name : String)
|
||||||
|
+ patronymic=(patronymic : String)
|
||||||
|
+ birth_date=(birthdate : String)
|
||||||
|
+ valid_name?(name : String) : Boolean <<class>>
|
||||||
|
- name_initial(name : String) : String
|
||||||
|
- patronymic(patronymic : String) : String
|
||||||
|
}
|
||||||
|
|
||||||
|
BinarySearchTree o-- Node
|
||||||
|
Node o-- Student
|
||||||
|
Person <|-- Student
|
||||||
|
Person <|-- StudentShort
|
||||||
|
Student <.. StudentRepository
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
class BinarySearchTree
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
|
class Node
|
||||||
|
attr_accessor :student, :left, :right
|
||||||
|
|
||||||
|
def initialize(student)
|
||||||
|
@student = student
|
||||||
|
@left = nil
|
||||||
|
@right = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@root = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def add(student)
|
||||||
|
@root = insert(@root, student)
|
||||||
|
end
|
||||||
|
|
||||||
|
def each(&block)
|
||||||
|
in_order_traversal(@root, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def insert(node, student)
|
||||||
|
return Node.new(student) if node.nil?
|
||||||
|
|
||||||
|
if student.birth_date < node.student.birth_date
|
||||||
|
node.left = insert(node.left, student)
|
||||||
|
else
|
||||||
|
node.right = insert(node.right, student)
|
||||||
|
end
|
||||||
|
node
|
||||||
|
end
|
||||||
|
|
||||||
|
def in_order_traversal(node, &block)
|
||||||
|
return if node.nil?
|
||||||
|
|
||||||
|
in_order_traversal(node.left, &block)
|
||||||
|
yield node.student
|
||||||
|
in_order_traversal(node.right, &block)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,39 @@
|
|||||||
|
require_relative 'student'
|
||||||
|
|
||||||
|
class StudentRepository
|
||||||
|
def self.read_from_txt(file_path)
|
||||||
|
raise IOError, "File does not exist: #{file_path}" unless File.exist?(file_path)
|
||||||
|
|
||||||
|
students = []
|
||||||
|
|
||||||
|
File.foreach(file_path) do |line|
|
||||||
|
line.strip!
|
||||||
|
next if line.empty?
|
||||||
|
|
||||||
|
begin
|
||||||
|
student = Student.from_string(line)
|
||||||
|
students << student
|
||||||
|
rescue ArgumentError => e
|
||||||
|
puts "Error processing line: '#{line}'. Reason: #{e.message}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
students
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.write_to_txt(file_path, students)
|
||||||
|
unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
|
||||||
|
raise ArgumentError, 'Expected an array of Student objects'
|
||||||
|
end
|
||||||
|
|
||||||
|
File.open(file_path, 'w') do |file|
|
||||||
|
students.each do |student|
|
||||||
|
file.puts student.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Data successfully written to #{file_path}"
|
||||||
|
rescue IOError => e
|
||||||
|
puts "An error occurred while writing to the file: #{e.message}"
|
||||||
|
end
|
||||||
|
end
|
@ -1,31 +1,59 @@
|
|||||||
require_relative 'person'
|
require_relative 'person'
|
||||||
|
|
||||||
class StudentShort < Person
|
class StudentShort < Person
|
||||||
attr_reader :surname_initials, :contact
|
attr_reader :surname_initials
|
||||||
|
|
||||||
def initialize(student)
|
def initialize(id:, surname_initials:, phone: nil, telegram: nil, email: nil)
|
||||||
super(id: student.id, git: student.git_info)
|
super(id: id, phone: phone, telegram: telegram, email: email)
|
||||||
@surname_initials = student.surname_and_initials
|
@surname_initials = surname_initials
|
||||||
@contact = student.contact_info
|
freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_string(id, info_string)
|
def self.from_student(student)
|
||||||
parts = info_string.split(', ').map(&:strip)
|
new(
|
||||||
surname_initials = parts[0]
|
id: student.id,
|
||||||
git = parts[1].split(': ').last
|
surname_initials: student.surname_initials,
|
||||||
contact = parts[2].split(': ').last
|
phone: student.phone,
|
||||||
|
telegram: student.telegram,
|
||||||
new_instance = allocate
|
email: student.email
|
||||||
new_instance.send(:initialize_from_data, id, surname_initials, git, contact)
|
)
|
||||||
new_instance
|
end
|
||||||
end
|
|
||||||
|
def self.from_string(id, info_string)
|
||||||
private
|
parts = info_string.split(',').map(&:strip)
|
||||||
|
raise ArgumentError, 'Invalid info string format' if parts.size < 2
|
||||||
def initialize_from_data(id, surname_initials, git, contact)
|
|
||||||
@id = id
|
surname_initials = parts[0]
|
||||||
@surname_initials = surname_initials
|
contact_string = parts[1].split(': ', 2).last.strip
|
||||||
@git = git
|
|
||||||
@contact = contact
|
phone, telegram, email = parse_contact_string(contact_string)
|
||||||
|
|
||||||
|
new(
|
||||||
|
id: id,
|
||||||
|
surname_initials: surname_initials,
|
||||||
|
phone: phone,
|
||||||
|
telegram: telegram,
|
||||||
|
email: email
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
contact_info = get_first_contact()
|
||||||
|
"#{@surname_initials}, Contact: #{contact_info}"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.parse_contact_string(contact_string)
|
||||||
|
case contact_string
|
||||||
|
when /\APhone: (.+)\z/i
|
||||||
|
[Regexp.last_match(1).strip, nil, nil]
|
||||||
|
when /\ATelegram: (.+)\z/i
|
||||||
|
[nil, Regexp.last_match(1).strip, nil]
|
||||||
|
when /\AEmail: (.+)\z/i
|
||||||
|
[nil, nil, Regexp.last_match(1).strip]
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Invalid contact string format: #{contact_string}"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
Иванов Иван Иванович | ID: 1 | Phone: +12345678901 | Telegram: @ivanov_user | Email: ivanov@example.com | Git: https://github.com/ivanov
|
||||||
|
Петров Петр Петрович | ID: 2 | Phone: +98765432101 | Telegram: @petrov_user | Email: petrov@example.com | Git: https://github.com/petrov
|
||||||
|
Сидоров Сидор Сидорович | ID: 3 | Phone: +56789012345 | Telegram: @sidorov_user | Email: sidorov@example.com | Git: https://github.com/sidorov
|
Loading…
Reference in new issue