如何在arrayList中执行加法和减法(或操作对象)?

时间:2015-04-14 13:08:56

标签: java arrays object for-loop arraylist

在我的课程中,我想在两个人之间转移学分。我在arrayList中添加的所有信息。每个人都有一个身份证。 要进行交易,用户必须输入ID。

必须从特定帐户中扣除用户输入的提款金额,并且最终必须使用其他特定帐户添加该金额,具体取决于用户选择的ID。如何操作?< / p>

基本上,我不知道如何使用匹配的ID检查金额/信用额度,然后在该特定人员的金额内执行算术任务。

实际上,我所关注的部分是我的代码中的switch (option2)

这是我正在处理的代码。

AboutPerson:

public static void main(String[] args) {
        String name;
int id;
 int option1;
        int option2;
        double credit;
        int withdraw_id;
        double withdraw_amount;
        double dep_id;
        Scanner input = new Scanner(System.in);
        List<PersonInfo> info = new ArrayList<PersonInfo>();
        while (true) {
            System.out.println("\n");
            System.out.println("1. Input personal info\n"
                    + "2. Print them out\n"
                    + "3. Transfer credits\n"//need help here
                    + "*************"
                    + "*************");
            option1 = input.nextInt();
            input.nextLine();
            switch (option1) {
                case 1:
                    PersonInfo personInfo = new PersonInfo();
                    //take the input
                    System.out.println("Enter a name: ");
                    personInfo.setName(input.nextLine());

                    System.out.println("Give ID: ");
                    personInfo.setId(input.nextInt());
                    System.out.println("Input credit: ");
                    personInfo.setCredit(input.nextDouble());
                    //addint them up
                    info.add(personInfo);
                    break;
                case 2:
                    //display them 
                    System.out.println("");
                    System.out.println("Name\t\tID\t\tCredit");
                    for (PersonInfo pInfo : info) {
                        System.out.println(pInfo);
                    }
                    System.out.println("\t\t.............\n"
                            + "\t\t.............");
                    break;
                case 3:
                    //transfer credit
                    System.out.println("To transfer credit between two persons enter 1");//working with this one
                    System.out.println("To transfer credit within the same persons enter 2");//not focusing on that now
                    option2 = input.nextInt();
                    input.nextLine();
                    switch (option2) {
                        case 1:
                            System.out.println("Enter the ID of the person you want to withdraw amount from: ");
                            withdraw_id = input.nextInt();

                            System.out.println("Enter withdraw amount: ");
                            withdraw_amount = input.nextDouble();
                            //subtract that credit from that account
                            System.out.println("Enter the ID of the person you want to deposit into: ");
                            dep_id = input.nextDouble();
                            //the amount has been withdrawn will be deposited
                            //add that credit
                            System.out.println("Done!\tTo print them out out choose option 2");
                            break;


                    }

            }

        }
  }

PersonInfo:

 package aboutperson;

public class PersonInfo {

    private String name;
    private int id;
    private double credit;

    public PersonInfo() {
        this.name = null;
        this.id = 0;
        this.credit = 0;
    }

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

    public void setId(int id) {
        this.id = id;
    }

    public void setCredit(double credit) {
        this.credit = credit;
    }

    @Override
    public String toString() {
        return name + "\t\t" + id + "\t\t" + credit;
    }

}

3 个答案:

答案 0 :(得分:1)

不要将PersonInfo存储在List中,而是使用Map,您可以将密钥用作id和值PersonInfo本身。所以它会像:

 Map<Integer, PersonInfo> info = new HashMap<Integer, PersonInfo>();
  ...
 personInfo.setCredit(input.nextDouble());
 perdonIdInfoMap.put(personInfo.getId(), personInfo);

然后当你有两个id从地图获取信息时:

personInfoFrom = perdonIdInfoMap.get(id1);
personInfoTo = perdonIdInfoMap.get(id2);
//now deduct from one and add to other like personInfoFrom.setCredit(personInfoFrom.getCridit() + personInfoTo.getCredit());

答案 1 :(得分:1)

首先将getCreditgetID方法添加到PersonInfo

public double getCredit() {
    return this.credit;
}

public int getID() {
    return this.id;
}

然后只需在两个帐户之间进行交换:

System.out.println("Enter the ID of the person you want to deposit into: ");
dep_id = input.nextDouble();
input.nextLine();

System.out.println("Enter your ID: ");
withdraw_id = input.nextDouble();
input.nextLine();

System.out.println(": ");
withdraw_amount = input.nextDouble();
input.nextLine();

PersonInfo fromPerson = null;
PersonInfo toPerson = null;

//find PersonInfo objects from list:
for (PersonInfo pi : info) {
    if (pi.getID() == dep_id) {
          toPerson = pi;
          break;
    }
}

for (PersonInfo pi : info) {
    if (pi.getID() == withdraw_id) {
          fromPerson = pi;
          break;
    }
}

if (fromPerson == null || toPerson == null) {
    System.out.println("Wrong IDs."); //maybe do something more user friendly here
    break;
}

if (withdraw_amount > fromPerson.getCredit()) {
    System.out.println("notify of error");
    break;
}

fromPerson.setCredit(fromPerson.getCredit() - withdraw_amount);
toPerson.setCredit(toPerson.getCredit() + withdraw_amount);

System.out.println("Done!\tTo print them out out choose option 2");

答案 2 :(得分:0)

您可以遍历列表并检查:

for(PersonInfo person: info){
    if(person.getId().equals(searchedId)){
        /* here you can do operation on that person*/

        /* Printing the balance */
        System.out.println("Balance: " + person.getCredit());

        /* Adding or subtracting money*/
        person.setCredit(person.getCredit() + ammount);

        /* Other stuff*/
    }
}

或者你可以使用Map作为SMA建议