java - 如何对特定对象求和?
问题描述
我有这段代码,您可以在其中搜索患者 ID,找到后会显示一个子菜单,然后会提示用户进行选择。如果用户选择帐单,则系统会要求输入一笔交易,并将其汇总到与搜索的 ID 相同的对象中的余额中。但是,当用户输入一个值时,它总是将该值与对象中的 balance(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;
}
}```
解决方案
我从您的问题中了解到的是,您需要将 sum 添加到 Patient Object 数组中特定对象的余额中。下面是做法,
(我排除了一些成员变量,我只是通过查看您的问题中的对象创建并仅在 Patient Class 中保留名称、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
}
}
} }
我想,您现在可以借助这些代码片段解决您的问题。希望它是有帮助的。
推荐阅读
- java - 线程“主”java.lang.Error 中的异常:未解决的编译问题
- python - 如何在 Python 中使用变量打印(dir(module))?
- node.js - node.js multer Route.post() 回调
- r - 在变量中使用字符串来创建新的变量名
- c# - 从另一个窗口中的按钮命令更改文本框的 IsEnabled 属性不起作用?
- javascript - 如何使用 Ajax 将数组值从 Javascript 格式化为 Java Servlet
- java - servlet请求setAttibute和getParameter java的问题
- c# - 如果使用while循环,有没有办法从内存中“强制删除”一个实例?
- java - 不兼容的类型:void 不能转换为 List
- reactjs - 使用 react 和 express(nginx、docker)建立 web-socket 通信