I want to create add an edit functionality (public void EditPatientData()
) to edit the patients surname, firstname,
dateOfBirth, Length and weight. In other words I want to be able to edit the Patient's in the system Id, Surname, firtstname etc.
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
public class Patient {
private static final int RETURN = 0;
private static final int SURNAME = 1;
private static final int FIRSTNAME = 2;
private static final int DATEOFBIRTH = 3;
private static final int LENGTH = 4;
private static final int WEIGHT = 5;
private static final int EDIT = 6;
private int id;
private String surname;
private String firstName;
private LocalDate dateOfBirth;
private double length;
private double weight;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
public int getId() {
return id;
}
public String getSurname() {
return surname;
}
public String getFirstName() {
return firstName;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public double getLength() {
return length;
}
public double getWeight() {
return weight;
}
// Method to calculate the age of a patient
public int calcAge(LocalDate dateOfBirth) {
//Code gets current date
LocalDate curDate = LocalDate.now();
//If else statement that checks if both dates are not null/
if ((dateOfBirth != null) && (curDate != null)) {
/*if dates are both not null the code will take the birthdate and currentdate and
calculate the difference the code will calculate the age in years */
return Period.between(dateOfBirth, curDate).getYears();
} else {
//if one or both dates are null the code will return 0
return 0;
}
}
//this code formats the double in 2 decimals so it looks cleaner
private static final DecimalFormat df = new DecimalFormat("0.00");
//this code calculates the BMI of the patient
public String calcBMI(double weight, double length) {
return df.format(weight / (length * length));
}
// Constructor
Patient(int id, String surname, String firstName, LocalDate dateOfBirth, double weight,
double length) {
this.id = id;
this.surname = surname;
this.firstName = firstName;
this.dateOfBirth = dateOfBirth;
this.weight = weight;
this.length = length;
}
// Display patient data.
public void viewData() {
System.out.format("===== Patient id=%d ==============================\n", id);
System.out.format("%-17s %s\n", "Surname:", surname);
System.out.format("%-17s %s\n", "firstName:", firstName);
System.out.format("%-17s %s\n", "Date of birth:", dateOfBirth);
System.out.format("%-17s %s\n", "Age:", calcAge(dateOfBirth));
System.out.format("%-17s %s\n", "Weight in KG:", weight);
System.out.format("%-17s %s\n", "Length in M:", length);
System.out.format("%-17s %s\n", "The Patients BMI:", calcBMI(weight, length));
}
// Shorthand for a Patient's full name1
public String fullName() {
return String.format("%s %s [%s]", firstName, surname, dateOfBirth.toString(),
calcAge(dateOfBirth), weight, length, calcBMI(weight, length));
}
//Edit patient data.
public void EditPatientData() {
}
}
I am new to Java and I need help because I am stuck and don't know how to continue.
CodePudding user response:
What do you mean by add & edit? If add mean you want to add a patient! Then you can use your Patient constructor to create(add) New Patient. If you want to edit a Patient then you need to add a setters for your fields and use these setters to edit some values for your patient object.
CodePudding user response:
Basically if you want to add 2 patients you will need an array. It is best practice that keep all your logic work methods in different classes rather than Patient- data class. In this case, you will basically not need any getter or setter method. The constructor will be more than enough here.
Check methods and try to understand the logic.
We have basic few line codes that we have used them 2 3 times but every time it helped us do different things. For example, fillMethod() helps us in registration, also with updates. Always try to write code that you can use for many things.
I hope it will help
Step 1: Create one config and first add an array: It will store patients' index. So when you want to update let's say, the 4th patient, you will call basically the 4th patient and you will update it.
public class Config {
//Patient's data
public static Patient[] patients= null;
}
Step 2: Create InputUtil class and add 3 main methods: registerPatients(), printRegisteredPatients() and updatePatients()
public class PatientUtil {
//this method for register
public static void registerPatients() {
//to set Config Patients array size. How
int numberOfPatients= //many patients you will register, write num
Config.patients= new Student[numberOfPatients];
for (int i = 0; i < count; i ) {
System.out.println((i 1) ".Register");//1. Register
//we put patient to the index
Config.patients[i] = PatientUtil.fillPatient();
}
System.out.println("Registration completed successfully!");
PatientUtil.printAllRegisteredPatirnts();
}
//Step 3. Create fillPatient() method to fill the patient
// this method will help us register and uptade time
public static Patient fillPatient() {
PAtient patient = new Patient(int id, String surname, String firstName,
LocalDate dateOfBirth, double weight,
double length);
return patient;
}
//Step4: Print all registered patients
public static void printAllRegisteredPatients() {
if (Config.patients== null) {
return;
}
for (int i = 0; i < Config.patients.length; i ) {
Patient pt = Config.patients[i];
System.out.println((i 1) "." pt.fullname());//this is your toString method
}
}
//Step5 Update Patients
public static Patient updatePatient(){
int updatePatientAtThisIndex = //write number that you want to update exact patient
System.out.println("Enter the details: ");
Patient updatingPatient = PatientUtil.fillPatient();// asking new student details
Config.patients[updatePatientAtThisIndex ]=updatingPatient ;//updating that index we asked before with new data
}
}