Skip to content

Commit f60fb35

Browse files
committed
feat(hospoital): add possible solution for hospital
1 parent eff56f5 commit f60fb35

File tree

8 files changed

+345
-1
lines changed

8 files changed

+345
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
package org.com.level.hospital;
3+
4+
import java.util.Scanner;
5+
import java.util.function.BiPredicate;
6+
7+
8+
public class Authenticator {
9+
private Scanner scanner = new Scanner(System.in);
10+
public Boolean isLogged = false;
11+
12+
void shouldLogOut() {
13+
System.out.println("Do you want to continue? ");
14+
String next = scanner.next();
15+
if (next.equalsIgnoreCase("no")) {
16+
Main.authenticator.isLogged = false;
17+
}
18+
}
19+
20+
private void printMessage(String x) {
21+
System.out.println(x);
22+
}
23+
24+
public void authProcessFor(int tries,
25+
User user) {
26+
var isLoged = false;
27+
while (! isLoged && tries > 0) {
28+
isLoged = authLogic().test(user.password(), user.name());
29+
if(!isLoged){
30+
printMessage("you still have " + (tries - 1) + " tries left");
31+
tries--;
32+
}
33+
}
34+
isLogged = isLoged;
35+
authMessage();
36+
}
37+
38+
39+
private BiPredicate<String, String> authLogic() {
40+
return (passwordP, userNameP) -> {
41+
printMessage("Enter User Name");
42+
var userName = scanner.next();
43+
printMessage("Enter Password");
44+
var password = scanner.next();
45+
return userName.equals(userNameP) && password.equals(passwordP);
46+
};
47+
}
48+
49+
private void authMessage() {
50+
if (! isLogged) {
51+
printMessage("Sorry, max try possible");
52+
} else {
53+
printMessage("Welcome ");
54+
}
55+
}
56+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.com.level.hospital;
2+
3+
import java.util.List;
4+
5+
public record Doctor(String speciality, String name, List<Slot> slotList) {
6+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.com.level.hospital;
2+
3+
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Scanner;
7+
8+
import static org.com.level.hospital.Printer.printOptions;
9+
10+
public class Main {
11+
static Authenticator authenticator = new Authenticator();
12+
static Scanner scanner = new Scanner(System.in);
13+
static User user;
14+
15+
public static void main(String[] args) {
16+
user = new User("lol", "pass", Map.of());
17+
authenticator.authProcessFor(3, user);
18+
while (authenticator.isLogged && ! user.userHave3Appointments()) {
19+
searchAppointment();
20+
authenticator.shouldLogOut();
21+
}
22+
}
23+
24+
25+
private static void searchAppointment() {
26+
27+
System.out.println("Select the speciality");
28+
var selectedSpeciality = printOptions(Utils.specialities);
29+
var possibleDoctors = filterDoctorsBySpeciality(selectedSpeciality);
30+
31+
System.out.println("Possible Doctors for this Speciality");
32+
var numberSelectedDoctor = printOptions(possibleDoctors.stream()
33+
.map(Doctor :: name)
34+
.toList());
35+
var selectedDoctorByUser = possibleDoctors.get(numberSelectedDoctor);
36+
var possibleSlots = filterSlotsByDoctorAndUser(selectedDoctorByUser);
37+
38+
System.out.println("Select a free slot");
39+
var numberOfSelectedSlot = printOptions(possibleSlots);
40+
41+
var appointment = possibleSlots.get(numberOfSelectedSlot)
42+
.registerAppointment();
43+
44+
user = user.registerAppointment(selectedDoctorByUser,
45+
appointment);
46+
47+
Utils.doctors = updateDoctor(possibleSlots,
48+
appointment,
49+
selectedDoctorByUser);
50+
51+
}
52+
53+
private static List<Slot> filterSlotsByDoctorAndUser(Doctor selectedDoctorByUser) {
54+
return selectedDoctorByUser.slotList()
55+
.stream()
56+
.filter(Slot :: free)
57+
.filter(Main :: isFreeForUser)
58+
.toList();
59+
}
60+
61+
private static List<Doctor> updateDoctor(List<Slot> possibleSlots,
62+
Slot newSlot,
63+
Doctor selectedDoctor)
64+
{
65+
var updatedSlotList = possibleSlots.stream()
66+
.map(slot -> slot.id()
67+
.equals(newSlot.id()) ? newSlot : slot)
68+
.toList();
69+
70+
return Utils.doctors.stream().map(doctor -> {
71+
if (doctor.name().equals(selectedDoctor.name())) {
72+
return new Doctor(selectedDoctor.speciality(),
73+
selectedDoctor.name(),
74+
updatedSlotList);
75+
}
76+
return doctor;
77+
}).toList();
78+
}
79+
80+
private static boolean isFreeForUser(Slot slot) {
81+
return user.getUserSlotsStream()
82+
.noneMatch(slotFromUser -> slotFromUser.timeStamp().equals(slot.timeStamp()));
83+
}
84+
85+
private static List<Doctor> filterDoctorsBySpeciality(int selectedSpeciality) {
86+
return Utils.doctors.stream()
87+
.filter(doctor -> (doctor.speciality()
88+
.equals(Utils.specialities.get(selectedSpeciality)) &&
89+
(! user.doctorAndAppointment()
90+
.containsKey(doctor.name()))
91+
)).toList();
92+
}
93+
94+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.com.level.hospital;
2+
3+
import java.util.List;
4+
5+
public class Printer {
6+
static <T> int printOptions(List<T> options) {
7+
var repeat = true;
8+
var optionNumber = - 1;
9+
while (repeat) {
10+
for (int i = 0; i < options.size(); i++) {
11+
System.out.println(i + ") para " + options.get(i));
12+
}
13+
optionNumber = Main.scanner.nextInt();
14+
repeat = ! (optionNumber >= 0 && optionNumber <= options.size());
15+
if (repeat) {
16+
System.out.println("Elegiste una option invalida intentalo de nuevo");
17+
}
18+
}
19+
return optionNumber;
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.com.level.hospital;
2+
3+
public record Slot(Integer id, String timeStamp, boolean free) {
4+
Slot registerAppointment() {
5+
return new Slot(this.id(),
6+
this.timeStamp(),
7+
false);
8+
}
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.com.level.hospital;
2+
3+
import java.util.Map;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
public record User(String name,
8+
String password,
9+
Map<String, Slot> doctorAndAppointment) {
10+
Stream<Slot> getUserSlotsStream() {
11+
return this.doctorAndAppointment()
12+
.values()
13+
.stream();
14+
}
15+
16+
User registerAppointment(Doctor selectedDoctor,
17+
Slot newSlot) {
18+
var newMapSlot =
19+
Stream.concat(this.doctorAndAppointment().entrySet().stream(),
20+
Stream.of(Map.entry(selectedDoctor.name(), newSlot)))
21+
.collect(Collectors.toMap(Map.Entry :: getKey,
22+
Map.Entry :: getValue));
23+
return new User(this.name(), this.password(), newMapSlot);
24+
}
25+
26+
boolean userHave3Appointments() {
27+
if (this.doctorAndAppointment().size() >= 3) {
28+
System.out.println("Maximal Number Appointments gotten");
29+
return true;
30+
}
31+
return false;
32+
}
33+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.com.level.hospital;
2+
3+
import java.util.List;
4+
5+
public class Utils {
6+
static List<String> specialities = List.of("General Medicine",
7+
"Emergency Care",
8+
"Clinical Analysis",
9+
"Cardiology",
10+
"Neurology",
11+
"Nutrition",
12+
"Physiotherapy",
13+
"Traumatology",
14+
"Internal Medicine");
15+
static List<Doctor> doctors = List.of(
16+
new Doctor("General Medicine", "Dr. Ana Torres", List.of(
17+
new Slot(1, "8-9", true),
18+
new Slot(2, "9-10", true),
19+
new Slot(3, "10-11", true))),
20+
new Doctor("General Medicine", "Dr. Carlos Ruiz", List.of(
21+
new Slot(1, "8-9", true),
22+
new Slot(2, "9-10", true),
23+
new Slot(3, "10-11", true))),
24+
new Doctor("General Medicine", "Dr. Laura García", List.of(
25+
new Slot(1, "8-9", true),
26+
new Slot(2, "9-10", true),
27+
new Slot(3, "10-11", true))),
28+
new Doctor("Emergency Care", "Dr. Roberto Fernández", List.of(
29+
new Slot(1, "8-9", true),
30+
new Slot(2, "9-10", true),
31+
new Slot(3, "10-11", true))),
32+
new Doctor("Emergency Care", "Dr. Sofia Martínez", List.of(
33+
new Slot(1, "8-9", true),
34+
new Slot(2, "9-10", true),
35+
new Slot(3, "10-11", true))),
36+
new Doctor("Emergency Care", "Dr. Juan Pérez", List.of(
37+
new Slot(1, "8-9", true),
38+
new Slot(2, "9-10", true),
39+
new Slot(3, "10-11", true))),
40+
new Doctor("Clinical Analysis", "Dr. María López", List.of(
41+
new Slot(1, "8-9", true),
42+
new Slot(2, "9-10", true),
43+
new Slot(3, "10-11", true))),
44+
new Doctor("Clinical Analysis", "Dr. Jorge Gómez", List.of(
45+
new Slot(1, "8-9", true),
46+
new Slot(2, "9-10", true),
47+
new Slot(3, "10-11", true))),
48+
new Doctor("Clinical Analysis", "Dr. Patricia Díaz", List.of(
49+
new Slot(1, "8-9", true),
50+
new Slot(2, "9-10", true),
51+
new Slot(3, "10-11", true))),
52+
new Doctor("Cardiology", "Dr. Fernando Sánchez", List.of(
53+
new Slot(1, "8-9", true),
54+
new Slot(2, "9-10", true),
55+
new Slot(3, "10-11", true))),
56+
new Doctor("Cardiology", "Dr. Isabel Rodríguez", List.of(
57+
new Slot(1, "8-9", true),
58+
new Slot(2, "9-10", true),
59+
new Slot(3, "10-11", true))),
60+
new Doctor("Cardiology", "Dr. Luis Jiménez", List.of(
61+
new Slot(1, "8-9", true),
62+
new Slot(2, "9-10", true),
63+
new Slot(3, "10-11", true))),
64+
new Doctor("Neurology", "Dr. Carmen Ortiz", List.of(
65+
new Slot(1, "8-9", true),
66+
new Slot(2, "9-10", true),
67+
new Slot(3, "10-11", true))),
68+
new Doctor("Neurology", "Dr. Diego Vázquez", List.of(
69+
new Slot(1, "8-9", true),
70+
new Slot(2, "9-10", true),
71+
new Slot(3, "10-11", true))),
72+
new Doctor("Neurology", "Dr. Elena Moreno", List.of(
73+
new Slot(1, "8-9", true),
74+
new Slot(2, "9-10", true),
75+
new Slot(3, "10-11", true))),
76+
new Doctor("Nutrition", "Dr. Sara Navarro", List.of(
77+
new Slot(1, "8-9", true),
78+
new Slot(2, "9-10", true),
79+
new Slot(3, "10-11", true))),
80+
new Doctor("Nutrition", "Dr. Rafael Morales", List.of(
81+
new Slot(1, "8-9", true),
82+
new Slot(2, "9-10", true),
83+
new Slot(3, "10-11", true))),
84+
new Doctor("Nutrition", "Dr. Clara Fernández", List.of(
85+
new Slot(1, "8-9", true),
86+
new Slot(2, "9-10", true),
87+
new Slot(3, "10-11", true))),
88+
new Doctor("Physiotherapy", "Dr. José González", List.of(
89+
new Slot(1, "8-9", true),
90+
new Slot(2, "9-10", true),
91+
new Slot(3, "10-11", true))),
92+
new Doctor("Physiotherapy", "Dr. Teresa Romero", List.of(
93+
new Slot(1, "8-9", true),
94+
new Slot(2, "9-10", true),
95+
new Slot(3, "10-11", true))),
96+
new Doctor("Physiotherapy", "Dr. Daniel Castillo", List.of(
97+
new Slot(1, "8-9", true),
98+
new Slot(2, "9-10", true),
99+
new Slot(3, "10-11", true))),
100+
new Doctor("Traumatology", "Dr. Laura Martín", List.of(
101+
new Slot(1, "8-9", true),
102+
new Slot(2, "9-10", true),
103+
new Slot(3, "10-11", true))),
104+
new Doctor("Traumatology", "Dr. Miguel Ángel Soto", List.of(
105+
new Slot(1, "8-9", true),
106+
new Slot(2, "9-10", true),
107+
new Slot(3, "10-11", true))),
108+
new Doctor("Traumatology", "Dr. Beatriz Rivas", List.of(
109+
new Slot(1, "8-9", true),
110+
new Slot(2, "9-10", true),
111+
new Slot(3, "10-11", true))),
112+
new Doctor("Internal Medicine", "Dr. Óscar Domínguez", List.of(
113+
new Slot(1, "8-9", true),
114+
new Slot(2, "9-10", true),
115+
new Slot(3, "10-11", true))),
116+
new Doctor("Internal Medicine", "Dr. Alicia Vega", List.of(
117+
new Slot(1, "8-9", true),
118+
new Slot(2, "9-10", true),
119+
new Slot(3, "10-11", true))),
120+
new Doctor("Internal Medicine", "Dr. Guillermo Alonso", List.of(
121+
new Slot(1, "8-9", true),
122+
new Slot(2, "9-10", true),
123+
new Slot(3, "10-11", true)))
124+
);
125+
}

level_1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The user can schedule an appointment for: General Medicine, Emergency Care, Clin
4343
There are 3 doctors for each specialty.
4444
The user can only book one appointment per specialist. An error message should be displayed if the user tries to choose two appointments with the same doctor or the same specialty. As a developer, you can choose the doctors' names.
4545
The maximum limit for appointments, in general, is 3.
46-
Upon selecting a specialty, it will display if the user prefers a morning or afternoon appointment and show available hours. As a developer, you can choose the hours.
46+
Upon selecting a specialists, it will display if the user prefers a morning or afternoon appointment and show available hours. As a developer, you can choose the hours.
4747
Display available specialists.
4848
The user can choose their preferred specialist.
4949
The basic process is: Login -> Choose specialty -> Choose doctor -> Choose time slot.

0 commit comments

Comments
 (0)