我需要使用属性ID计算总医生交易,然后使用列表中的属性ID计算支付医生总金额。 怎么解决?我坚持数不清楚。 请告诉
这是我的代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SandboxList {
public static void main(String[] args) {
List<Doctor> doctors = new ArrayList<Doctor>();
doctors.add(new Doctor("1726", "John", 10000.00));
doctors.add(new Doctor("4321", "Max", 20000.00));
doctors.add(new Doctor("1726", "John", 40000.00));
doctors.add(new Doctor("4321", "Max", 2000.00));
doctors.add(new Doctor("7765", "Sam", 50000.00));
doctors.add(new Doctor("4321", "Max", 6000.00));
/* I want Output should be Like below
ID : 1726 Name : John Total_payment : 50000.00 total_transaction : 2
ID : 4321 Name : Max Total_payment : 28000.00 total_transaction : 3
ID : 7765 Name : Sam Total_payment : 50000.00 total_transaction : 1
*/
Map<String, List<Doctor>> groupedDoctors = new HashMap<String, List<Doctor>>();
for (Doctor doctor: doctors) {
String key = doctor.doctor_id;
if (groupedDoctors.get(key) == null) {
groupedDoctors.put(key, new ArrayList<Doctor>());
}
groupedDoctors.get(key).add(doctor);
}
Set<String> groupedDoctorsKeySet = groupedDoctors.keySet();
for (String doctorId: groupedDoctorsKeySet) {
List<Doctor> dctrs = groupedDoctors.get(doctorId);
for (Doctor doctor : dctrs) {
System.out.println("ID : "+doctor.doctor_id+"\t"+"Name : "+doctor.doctor_name+"\t"+"total_Payment : "+doctor.doctor_payment);
}
}
}
}
class Doctor {
String doctor_id;
String doctor_name;
Double doctor_payment;
Doctor(String doctor_id, String doctor_name, Double doctor_payment) {
this.doctor_id = doctor_id;
this.doctor_name = doctor_name;
this.doctor_payment = doctor_payment;
}
}
答案 0 :(得分:2)
更改以下代码:
for (String doctorId : groupedDoctorsKeySet) {
List<Doctor> dctrs = groupedDoctors.get(doctorId);
Double total_payment = 0d;
String doctor_name = null;
for (Doctor doctor : dctrs) {
if (doctor_name == null) {
doctor_name = doctor.doctor_name;
}
total_payment += doctor.doctor_payment;
}
Doctor doctor = new Doctor(doctorId, doctor_name, total_payment);
System.out.println("ID : " + doctor.doctor_id + "\t" + "Name : " + doctor.doctor_name + "\ttotal_Payment : " + doctor.doctor_payment);
}
答案 1 :(得分:0)
在对医生进行分组后,你可以做这样的事情:
Set<String> groupedDoctorsKeySet = groupedDoctors.keySet();
for (String doctorId : groupedDoctorsKeySet)
{
List<Doctor> dctrs = groupedDoctors.get(doctorId);
if (dctrs != null && dctrs.size() > 0)
{
StringBuilder sb = new StringBuilder();
sb.append("ID : ").append(doctorId).append("\t").append("Name : ");
sb.append(dctrs.get(0).doctor_name).append("\t").append("total_Payment : ");
double total = 0;
// sum the payment
for (Doctor d : dctrs)
{
total += d.doctor_payment;
}
sb.append(total).append("\t").append("total_transaction : ");
sb.append(dctrs.size());
System.out.println(sb);
}
}
<强>输出强>:
ID : 4321 Name : Max total_Payment : 28000.0 total_transaction : 3 ID : 7765 Name : Sam total_Payment : 50000.0 total_transaction : 1 ID : 1726 Name : John total_Payment : 50000.0 total_transaction : 2
答案 2 :(得分:0)
使用您已有的内容,您可以将分组的部分更新为以下内容:
Set<String> groupedDoctorsKeySet = groupedDoctors.keySet();
for (String doctorId: groupedDoctorsKeySet) {
List<Doctor> dctrs = groupedDoctors.get(doctorId);
if (dctrs.size() <= 0) { continue; }
Doctor doctor = dctrs.get(0); //These should all be the same doctor due to the key
double totalPayment = 0.0;
for(Doctor doc : dctrs){ totalPayment += doc.doctor_payment; }
System.out.println("ID : " + doctor.doctor_id + "\tName : " + doctor.doctor_name
+ "\ttotal_Payment : " + totalPayment + "\tNumber Of transactions: " + dctrs.size());
}
=============================================== ========================== 替代解决方案
或者,如果复制医生的目的是能够在将来的某个时间单独检查每笔付款,您可以拥有单一的医生,但有付款清单。公布一种方法,用新的付款细节更新医生
然后,您就可以从该列表的大小中获取交易数量,而Doctor对象将能够确定总数本身:
class Doctor {
String doctor_id;
String doctor_name;
List<Double> doctor_payments = new ArrayList<>();
Doctor(String doctor_id, String doctor_name, Double doctor_payment) {
this.doctor_id = doctor_id;
this.doctor_name = doctor_name;
this.doctor_payments.add(doctor_payment);
}
public void processPayment(double paymentAmount){
this.doctor_payments.add(paymentAmount);
}
@Override
public String toString(){
double totalPayment = 0.0;
for(Double payment : doctor_payments){ totalPayment += payment; }
return "ID : " + doctor_id + "\tName : " + doctor_name
+ "\ttotal_Payment : " + totalPayment + "\tNumber Of transactions: " + doctor_payments.size();
}
}
使用示例:
Doctor john = new Doctor("1726", "John", 10000.00);
john.processPayment(40000.00);
System.out.println(john);
输出:ID : 1726 Name : John total_Payment : 50000.0 Number Of transactions: 2
答案 3 :(得分:0)
我只使用了2个数据结构,因此可以尽可能少地使用资源。
class Doctor {
String doctor_id;
String doctor_name;
Double doctor_payment;
Doctor(String doctor_id, String doctor_name, Double doctor_payment) {
this.doctor_id = doctor_id;
this.doctor_name = doctor_name;
this.doctor_payment = doctor_payment;
}
public String getDoctor_id() {
return doctor_id;
}
public String getDoctor_name() {
return doctor_name;
}
public Double getDoctor_payment() {
return doctor_payment;
}
public void setDoctor_id(String doctor_id) {
this.doctor_id = doctor_id;
}
public void setDoctor_name(String doctor_name) {
this.doctor_name = doctor_name;
}
public void setDoctor_payment(Double doctor_payment) {
this.doctor_payment = doctor_payment;
}
/*
HashCode() and equal() method is override because I want to identify the
element in collection on basis of ID's
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Doctor other = (Doctor) obj;
if (doctor_id == null) {
if (other.doctor_id != null)
return false;
} else if (!doctor_id.equals(other.doctor_id))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((doctor_id == null) ? 0 :
doctor_id.hashCode());
return result;
}
}
public class SandboxList {
public static void main(String[] args) {
List<Doctor> doctors = new ArrayList<Doctor>();
doctors.add(new Doctor("1726", "John", 10000.00));
doctors.add(new Doctor("4321", "Max", 20000.00));
doctors.add(new Doctor("1726", "John", 40000.00));
doctors.add(new Doctor("4321", "Max", 2000.00));
doctors.add(new Doctor("7765", "Sam", 50000.00));
doctors.add(new Doctor("4321", "Max", 6000.00));
Vector<Doctor> groupedDoctors = new Vector<Doctor>();
Doctor loopVariable;
int index;
for (Doctor doctor : doctors) {
index = groupedDoctors.indexOf(doctor);
// -1 shows groupedDoctors doesn't contain of this doctor
if (-1!=index) {
loopVariable = groupedDoctors.elementAt(index);
groupedDoctors.removeElementAt(index);
loopVariable.setDoctor_payment(doctor.getDoctor_payment() + loopVariable.getDoctor_payment());
groupedDoctors.add(loopVariable);
}
else{
groupedDoctors.addElement(doctor);
}
}
for (Doctor doctor : groupedDoctors) {
System.out.println("ID : " + doctor.doctor_id + "\t" + "Name : " +
doctor.doctor_name + "\t"
+ "total_Payment : " + doctor.doctor_payment);
}
}
}
答案 4 :(得分:0)
您可能需要这样的代码:
Set<String> groupedDoctorsKeySet = groupedDoctors.keySet();
if (groupedDoctorsKeySet != null) {
ArrayList<String> list = new ArrayList(groupedDoctorsKeySet);
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (Long.valueOf(o1) < Long.valueOf(o2)) return -1;
else if (Long.valueOf(01) == Long.valueOf(02)) return 0;
else return 1;
}
});
for (String doctorId: list) {
List<Doctor> dctrs = groupedDoctors.get(doctorId);
if (dctrs != null) {
double sum = 0;
String name = null;
for (Doctor doctor : dctrs) {
if (name == null) name = doctor.doctor_name;
sum += doctor.doctor_payment;
}
System.out.println(String.format("ID : %s\tName : %s\tTotal_payment : %.2f total_transaction : %d", new Object[]{doctorId, name, sum, dctrs.size()}));
}
}
}
,输出为:
ID : 1726 Name : John Total_payment : 50000.00 total_transaction : 2
ID : 4321 Name : Max Total_payment : 28000.00 total_transaction : 3
ID : 7765 Name : Sam Total_payment : 50000.00 total_transaction : 1