每次用户输入他/她想要注册的代码时,如何添加他/她注册的单位?有没有什么办法可以在每次使用arrayList注册时添加单位?
int units = 3
arrList.add("A25"+"\t\tCS 212"+"\t\tData Structures\t\t\t\t"+ units);
arrList.add("A26"+"\t\tIT 312"+"\t\tData Base Management System 2\t\t"+ units);
arrList.add("A27"+"\t\tIT 312"+"\t\tData Base Management System 2\t\t"+ units);
System.out.println("\n\t\tCodes to enroll");
for(int i = 0; i < 3; i++,num++)
{
codeNo[i] = scan.next();
}
for (String s : arrList) {
for(int i =0; i < codeNo.length; i++)
if (s.startsWith(codeNo[i])) {
System.out.println("\t\t\t"+s);
/**
* this is what I tried
* units = units + units;
*/
}
}
tuitionFee = ( tuitionFee * units + miscFee ) / 3;
System.out.println("\n\n\t\tTOTAL FEE: ");
System.out.printf("\t\tPrelims: "+"%.2f",tuitionFee);
System.out.printf("\t\tMidTerm: "+"%.2f",tuitionFee);
System.out.printf("\t\tFinals: "+"%.2f",tuitionFee);
答案 0 :(得分:0)
HashMap<String,Integer> mapCodeToUnit = new HashMap<String,Integer>();
mapCodeToUnit.put("A25", 3);
mapCodeToUnit.put("A26", 3);
mapCodeToUnit.put("A27", 3);
arrList.add("A25"+"\t\tCS 212"+"\t\tData Structures\t\t\t\t"+ mapCodeToUnit.get("A25"));
arrList.add("A26"+"\t\tIT 312"+"\t\tData Base Management System 2\t\t"+ mapCodeToUnit.get("A26"));
arrList.add("A27"+"\t\tIT 312"+"\t\tData Base Management System 2\t\t"+ mapCodeToUnit.get("A27"));
System.out.println("\n\t\tCodes to enroll");
String codeNo[] = new String[3];
for(int i = 0; i < 3; i++,num++)
{
codeNo[i] = scan.next();
}
int totalNumberOfUnit = 0;
for(int i =0; i < codeNo.length; i++) {
totalNumberOfUnit += mapCodeToUnit.get(codeNo[i]);
}
tuitionFee = ( tuitionFee * totalNumberOfUnit + miscFee ) / 3;
答案 1 :(得分:0)
列出EntrollmentDTO
而不是String
EntrollmentDTO.java
class EntrollmentDTO {
private String id;
private String subId;
private String subName;
// Getters & Setters
}
列出EntrollmentDTO
as,
ArrayList<EntrollmentDTO> entrollmentList = new ArrayList<EntrollmentDTO>();
并添加详细信息,
EntrollmentDTO entrollmentDTO = new EntrollmentDTO();
entrollmentDTO.setId("A25");
// set all values
entrollmentList.add(entrollmentDTO);
通过这种方式,您可以轻松地从列表中访问所需的值。