java—如何对特定对象中的值求和?

hyrbngr7  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(371)

我有这个代码,你会搜索病人id,当找到一个子菜单将显示,然后会提示用户选择。如果用户选择了账单,那么将要求该账单输入一个事务,并且该账单将与所搜索的id在同一对象中的余额相加。然而,当用户输入一个值时,它总是将该值和对象中的余额(450)相加。
我怎样才能解决这个问题?
注意:它在一个阵列中
输出:仅将输入添加到第一个对象。

patient pAccount[] = new patient [10];

    patient p1 = new patient("Jabari","luck",d1 ,"234-4343", "p01" ,450);

        patient p2 = new patient("Lisa", "nun", d2,"311-5634" , "p02",300);

        patient p3 = new patient("Kyle", "Hep",d3  ,"555-5555" , "p03",2000 );

//search array for patient ID
public static void ID(person [] pAccount) {

                Scanner scan= new Scanner(System.in);

                String num = scan.next();
                for(int i=0; i< pAccount.length; i++) {

                        if (pAccount[i].getpatID().equals(num)) {
                            System.out.println("found");
                            break;
                            }   

                    }
                    }       

//sum user input to balance
public static void bill(person[] pAccount) {

         Scanner in = new Scanner (System.in);
         double sum;

          double num= in.nextDouble();
          for(int i=0; i <= pAccount.length; i++) {
              person ad= pAccount[i];

                sum = ((patient) ad).getbalance()+ num;

                  System.out.println("Balance: " +sum);
                  break;

          }  

    }```
eoxn13cs

eoxn13cs1#

我从您的问题中了解到的是,您需要将sum添加到patient object数组中特定对象的余额中。下面是方法,

(我排除了几个成员变量,这些变量不是通过查看您问题中的对象创建得到的,并且在patient类中只保留了name、patid和balance。我还假设您已经使用了所有字段的构造函数)

我把你的代码,并修改了你的要求一点。您可以参考我在代码段中添加的注解。
patientmainclass.class.类

public class PatientMainClass {

public static void main(String[] args) {
    Patient pAccount[] = new Patient[3];

    Patient p1 = new Patient("Jabari", "p01", 450);
    pAccount[0] = p1;
    Patient p2 = new Patient("Lisa", "p02", 300);
    pAccount[1] = p2;
    Patient p3 = new Patient("Kyle", "p03", 2000);
    pAccount[2] = p3;

    //Use bill method to add Amount to existing balance of all the Patient Objects
    Patient.bill(pAccount);
    System.out.println();
    for (int i = 0; i < pAccount.length; i++) {
        System.out.println("After adding amount to the Balance of pAccount[" + i + "] is : " + pAccount[i].getBalance());
    }

    System.out.println();
    //Use billToSpecific method to add Amount to specific Patient Object
    //to billToSpecific method, pass both Patient Array and Patient ID to which you want to add Amount
    Patient.billToSpecificPatient(pAccount, "p02");
    System.out.println();
    for (int i = 0; i < pAccount.length; i++) {
        System.out.println("After adding amount to p02, Balance of pAccount[" + i + "] is : " + pAccount[i].getBalance());
    }

} }

病人类别

public class Patient {

private String name;
private String patId;
private double balance;

public Patient(String name, String patId, double balance) {
    super();
    this.name = name;
    this.patId = patId;
    this.balance = balance;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPatId() {
    return patId;
}

public void setPatId(String patId) {
    this.patId = patId;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

// This method will Add entered value i.e.. "num" to the Balance of all the Patient Objects
public static void bill(Patient[] pAccount) {
    System.out.println("In bill method");           // Always try using System.out.println for Debugging
    Scanner in = new Scanner(System.in);
    double sum;

    double num = in.nextDouble();
    for (int i = 0; i < pAccount.length; i++) {
        Patient ad = pAccount[i];

        sum = ((Patient) ad).getBalance() + num;
        ad.setBalance(sum);
        System.out.println("Balance: " + sum);
        // break;                                   // Commenting break statement to add balance to all the Objects

    }

}

// This method will Add entered value i.e.. "num" to the Balance of the Specific Patient Object
public static void billToSpecificPatient(Patient[] pAccount, String patId) {
    System.out.println("In billToSpecific method"); // Always try using System.out.println for Debugging
    Scanner in = new Scanner(System.in);
    double sum;

    double num = in.nextDouble();
    for (int i = 0; i < pAccount.length; i++) {
        if (pAccount[i].getPatId().equals(patId)) {
            Patient ad = pAccount[i];
            sum = ((Patient) ad).getBalance() + num;
            ad.setBalance(sum);
            System.out.println("Balance: " + sum);
            break;                                  // Using break statement to exit the loop once we add amount to specific Patient Object
        }

    }

} }

我想,您现在可以通过这些代码片段来解决您的问题。希望对你有帮助。

相关问题