簡體   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