-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrescription.java
143 lines (122 loc) · 4.97 KB
/
Prescription.java
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
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Prescription {
public int prescID;
public String firstName;
public String lastName;
public String address;
public float sphere;
public float axis;
public float cylinder;
public Date examinationDate;
public String optometrist;
private String[] remarkTypes = {"client", "optometrist"};
private ArrayList<String> postRemarks = new ArrayList<>();
public boolean addPrescription() {
// check first name length
if (firstName == null || firstName.length() < 4 || firstName.length() > 15) {
System.out.println("First name must be between 4 and 15 characters long.");
return false;
}
// check last name length
if (lastName == null || lastName.length() < 4 || lastName.length() > 15) {
System.out.println("Last name must be between 4 and 15 characters.");
return false;
}
// check address length
if (address == null || address.length() < 20) {
System.out.println("Address must be at least 20 characters.");
return false;
}
// check sphere range
if (sphere < -20.00f || sphere > 20.00f) {
System.out.println("Sphere must be between -20.00 and +20.00.");
return false;
}
// check cylinder range
if (cylinder < -4.00f || cylinder > 4.00f) {
System.out.println("Cylinder must be between -4.00 and +4.00.");
return false;
}
// check axis range
if (axis < 0.0f || axis > 180.0f) {
System.out.println("Axis must be between 0 and 180.");
return false;
}
// check examination date
if (examinationDate == null) {
System.out.println("Examination date is required.");
return false;
}
// check optometrist name length
if (optometrist == null || optometrist.length() < 8 || optometrist.length() > 25) {
System.out.println("Optometrist's name must be between 8 and 25 characters.");
return false;
}
// write prescription to file
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String formattedDate = sdf.format(examinationDate);
String prescriptionData = "Prescription ID: " + prescID + "\n" +
"First Name: " + firstName + "\n" +
"Last Name: " + lastName + "\n" +
"Address: " + address + "\n" +
"Sphere: " + sphere + "\n" +
"Cylinder: " + cylinder + "\n" +
"Axis: " + axis + "\n" +
"Examination Date: " + formattedDate + "\n" +
"Optometrist: " + optometrist + "\n\n";
java.io.FileWriter writer = new java.io.FileWriter("presc.txt", true);
writer.write(prescriptionData);
writer.close();
System.out.println("Prescription added successfully.");
return true;
} catch (Exception e) {
System.out.println("An error occurred while writing to the file.");
return false;
}
}
//second function
public boolean addRemark(String remark, String category) {
//check remark is not null and word count
if (remark == null) {
System.out.println("Remark cannot be null.");
return false;
}
String[] words = remark.trim().split("\\s+");
if (words.length < 6 || words.length > 20) {
System.out.println("Remark must have between 6 and 20 words.");
return false;
}
// check first character is uppercase
if (!Character.isUpperCase(words[0].charAt(0))) {
System.out.println("The first character of the first word must be uppercase.");
return false;
}
//check category is valid
if (category == null || (!category.equalsIgnoreCase(remarkTypes[0]) && !category.equalsIgnoreCase(remarkTypes[1]))) {
System.out.println("Category must be 'client' or 'optometrist'.");
return false;
}
// check if maximum remarks reached
if (postRemarks.size() >= 2) {
System.out.println("Cannot add more than 2 remarks.");
return false;
}
// add remark to list
postRemarks.add(remark);
//write remark to file with prescID
try {
java.io.FileWriter writer = new java.io.FileWriter("review.txt", true);
writer.write("Prescription ID: " + prescID + "\n");
writer.write("Category: " + category + "\n");
writer.write("Remark: " + remark + "\n\n");
writer.close();
return true;
} catch (Exception e) {
System.out.println("An error occurred while writing to the file.");
return false;
}
}
}