-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatient.java
More file actions
executable file
·157 lines (127 loc) · 4.57 KB
/
Copy pathPatient.java
File metadata and controls
executable file
·157 lines (127 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package hospitalmanagementsystem;
/**
*
* @author egehanhatipoglu
*/
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Patient {
private int id;
private String firstName;
private String lastName;
private String nationalId;
private String phone;
private LocalDate birthDate;
// Head of the Singly Linked List that stores the patient's medical history.
// Each node in this list represents a past visit/examination.
private Visit head;
// ============================================
// INNER CLASS: Visit (Linked List Node)
// ============================================
// Represents a single node in the medical history chain.
// Contains the visit details (data) and a pointer to the next visit (next).
private class Visit {
LocalDateTime date;
Doctor doctor;
String notes;
Visit next;
Visit(LocalDateTime date, Doctor doctor, String notes) {
this.date = date;
this.doctor = doctor;
this.notes = notes;
this.next = null;
}
public String toString() {
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
return "Date: " + date.format(f) +
", Doctor: Dr. " + doctor.getFirstName() + " " + doctor.getLastName() +
", Notes: " + notes;
}
}
public Patient(int id, String firstName, String lastName, String nationalId, String phone, LocalDate birthDate) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.nationalId = nationalId;
this.phone = phone;
this.birthDate = birthDate;
this.head = null;
}
// Adds a new medical record to the end of the Linked List.
// Traverses the list to find the last node and links the new visit.
// Time Complexity: O(n) (where n is the number of past visits).
public void addVisit(LocalDateTime date, Doctor doctor, String notes) {
if (doctor == null) {
System.out.println("✗ Error: Doctor cannot be null!");
return;
}
if (date == null) {
System.out.println("✗ Error: Date cannot be null!");
return;
}
Visit newVisit = new Visit(date,doctor, notes);
// Case 1: List is empty, new node becomes head
if (head == null) {
head = newVisit;
} else {
// Case 2: Traverse to the end and append
Visit current = head;
while (current.next != null) {
current = current.next;
}
current.next = newVisit;
}
}
// Traverses the Linked List from head to tail to display the full medical history.
// Essential for doctors to review past treatments.
public void printHistory() {
if (head == null) {
System.out.println(" No medical history available.");
return;
}
Visit current = head;
int count = 1;
while (current != null) {
System.out.println(" " + count + ". " + current);
current = current.next;
count++;
}
}
public void setId(int id) {
this.id = id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setNationalId(String nationalId) {
this.nationalId = nationalId;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public void setHead(Visit head) {
this.head = head;
}
// Getters
public int getId() { return id; }
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String getNationalId() { return nationalId; }
public String getPhone() { return phone; }
public LocalDate getBirthDate() { return birthDate; }
}