[Solved] How to perform Computations in Array list?


This is the sample answer .When I checked your question,I saw that I need to create one person class with name,accountNo,amount,accType,Date.When we check two array List to subtract,we need to find same AccountNo in both array.So we need to use contains method and I override equal and hashCode.My Sample Person class is just like:
package java7.demo;

import java.util.Date;

public class Person {
    private String name;
    private int accountNo;
    private String accType;
    private int amount;
    private Date date;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAccountNo() {
        return accountNo;
    }
    public void setAccountNo(int accountNo) {
        this.accountNo = accountNo;
    }
    public String getAccType() {
        return accType;
    }
    public void setAccType(String accType) {
        this.accType = accType;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + accountNo;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (accountNo != other.accountNo)
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "name=" + name + ", accountNo=" + accountNo
                + ", accType=" + accType + ", amount=" + amount + ", date="
                + date + "";
    }




}

Main Class is just like:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AccountSubtractionTest {

    @SuppressWarnings("deprecation")
    public static void main(String args[]){

        List<Person>FinalAmount = new ArrayList<Person>();
        List<Person>Transcation = new ArrayList<Person>();
        @SuppressWarnings("deprecation")
        Date d1 = new Date("01/01/2016");
        @SuppressWarnings("unused")
        Date d2 = new Date("04/01/2016");
        Person p1 = new Person();
        p1.setName("Peter");
        p1.setAccountNo(1234);
        p1.setAccType("Saving");
        p1.setAmount(5000);
        p1.setDate(d1);


        Person p2 = new Person();
        p2.setName("Robin");
        p2.setAccountNo(2222);
        p2.setAccType("Saving");
        p2.setAmount(5000);
        p2.setDate(new Date("02/02/2016"));

        FinalAmount.add(p1);
        FinalAmount.add(p2);

        Person p3 = new Person();
        p3.setName("Peter");
        p3.setAccountNo(1234);
        p3.setAccType("Saving");
        p3.setAmount(1000);
        p3.setDate(d2);

        Person p4 = new Person();
        p4.setName("Robin");
        p4.setAccountNo(2222);
        p4.setAccType("Saving");
        p4.setAmount(2000);
        p4.setDate(new Date("04/02/2016"));

        Transcation.add(p3);
        Transcation.add(p4);

        for(Person p:FinalAmount){
            if(Transcation.contains(p)){
                int index = Transcation.indexOf(p);
                Person person = Transcation.get(index);
                int amount = p.getAmount() - person.getAmount();
                p.setAmount(amount);
                Transcation.remove(index);
            }
        }

        for(int i=0; i<FinalAmount.size(); i++){
            System.out.println(FinalAmount.get(i));
        }






    }

}

in main class,i just use Date d1 = new Date(“01/01/2016”).So you need to change the date type format that you want to use.My date is just for sample.When I run the code output get is just like:

name=Peter, accountNo=1234, accType=Saving, amount=4000, date=Fri Jan 01 00:00:00 MMT 2016
name=Robin, accountNo=2222, accType=Saving, amount=3000, date=Tue Feb 02 00:00:00 MMT 2016

Please run the two classes that I gave as sample and Please see which code you need to modify.

solved How to perform Computations in Array list?