繁体   English   中英

如何使用 Java 添加编辑(Crud)功能?

[英]How can I add Edit (Crud) functionality with Java?

我想创建一个添加编辑功能( public void EditPatientData() )来编辑患者的姓氏、名字、出生日期、长度和体重。 换句话说,我希望能够在系统 ID、姓氏、名字等中编辑患者的姓名。

     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() {

     }
    }

我是 Java 的新手,我需要帮助,因为我被卡住了,不知道如何继续。

添加和编辑是什么意思? 如果添加意味着您要添加患者。 然后您可以使用您的 Patient 构造函数来创建(添加)新患者。 如果要编辑患者,则需要为字段添加设置器,并使用这些设置器来编辑患者对象的一些值。

基本上,如果您想添加 2 名患者,您将需要一个数组。 最佳实践是将所有逻辑工作方法保存在不同的类中,而不是 Patient-data class。 在这种情况下,您基本上不需要任何 getter 或 setter 方法。 构造函数在这里就绰绰有余了。

检查方法并尝试理解逻辑。

我们有基本的几行代码,我们已经使用了 2 3 次,但每次它都帮助我们做不同的事情。 例如,fillMethod() 可以帮助我们进行注册,也可以进行更新。 始终尝试编写可用于许多事情的代码。

我希望它会有所帮助

第 1 步:创建一个配置并首先添加一个数组:它将存储患者的索引。 所以当你想更新时,比如说第四个病人,你基本上会打电话给第四个病人,你会更新它。

public class Config {
    //Patient's data
    public static Patient[] patients= null;
}

第 2 步:创建 InputUtil class 并添加 3 个主要方法:registerPatients()、printRegisteredPatients() 和 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
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM