简体   繁体   中英

How do I perform Addition and Subtraction (or, manipulating the objects) in an arrayList?

In my program, I want to transfer credits between two person. All of the info I have added in the arrayList. Each person is given an ID. To make transaction, user have to enter ID.

The withdraw amount enterd by the user must be subtracted from the particular account and eventually that amount must be added with another particular account, depending on the ID chosen by user. How to do that?

Basically, I don't know how to check the amount/credit with the matching ID, and then performing arithmetic task within that specific person's amount.

Actually the part I am concentrating on is switch (option2) in my code.

This is my code I am working on.

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;
    }

}

Instead of storing PersonInfo in List , use Map where you can use key as id and value as PersonInfo itself. so it will be like:

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

Then when you have two id's get the info from map like:

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

First add getCredit and getID methods to PersonInfo :

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

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

And then simply exchange it between the two accounts:

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");

You can iterate over the list and check:

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*/
    }
}

Or you could use a Map as SMA suggested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM