1

I am new to Java. Please help to resolve the below problem statement.

I have an input file as mentioned below and having the values in following order in comma separated "id,invoiceNumber,custid,totalamt, amountdue". I need to find out the custid who is having amount due >1000. If one custid is repeated multiple times having dueamt > 1000, then i need to print the no of pending due payments.

*Input file :
102,12545,111,10000,5000
103,12546,111,10300,4000
104,12545,110,10000,2000*

*Output in the console:
cust id : 111
No of pending due payment ; 2
Total due amount : 9000
cust id : 110
No of pending due payment ; 1
Total due amount : 2000*

I am in the process of the below code but didnt get an idea

public static void main(String[] args) throws FileNotFoundException, IOException 
    {

        String line = null;
        Long custid;
        Double dueamt;
        int count = 1;
        Double newdue;
        Map<Long,Map> hashmap = new TreeMap<>();
        Invoice invoiceobj = null;
        try(BufferedReader br1 = new BufferedReader(new FileReader("input.txt")))
        {
            while((line = br1.readLine()) != null)
            {
                invoiceobj = new Invoice();
                String[] detailsarr = line.split(",");
                invoiceobj.setId(Long.parseLong(detailsarr[0]));
                invoiceobj.setInvoiceNumber(detailsarr[1]);
                invoiceobj.setCustomerId(Long.parseLong(detailsarr[2]));
                custid = Long.parseLong(detailsarr[2]);
                invoiceobj.setTotalAmount(Double.parseDouble(detailsarr[3]));
                invoiceobj.setAmountDue(Double.parseDouble(detailsarr[4]));
                dueamt = Double.parseDouble(detailsarr[4]);

                if(hashmap.containsKey(custid))
                {
                    Map<Double,Integer> hashmap2 = hashmap.get(custid);
                    newdue = olddue + dueamt;
                    count++;
                    hashmap2.put(newdue, count);



                }
                else
                {
                Map<Double,Integer> hashmap1 = new TreeMap<>(); 
                hashmap1.put(dueamt, 1);
                hashmap.put(custid, hashmap1);
                }
    }

            for(Map.Entry<Long,Double> entry : hashmap2.entrySet())
            {
                Long custid1 = entry.getKey();
                Double amt = entry.getValue();



                if(amt>1000)
                {
                System.out.println("Customer id "+custid1);

                System.out.println("Number of invoice pending for payment:"+count);

                System.out.println("Total Due Amount: $"+amt);
                }


            }
4
  • 1
    you can improve the question by correcting the formatting, braces and cleaning out the commented code maybe. Commented Sep 4, 2018 at 17:11
  • What happens when you run the provided code? Is there more output than you wanted, or less, or the wrong output? It would help us to solve your problem if you could tell us where the problem was. Commented Sep 4, 2018 at 18:19
  • Hi Jim, Basically i dont know the logic to solve the problem. The above code does not run successfully and i am stuck in the middle without knowing the logic.. I would like to know how to count "cust id" which is having more than one occurrence. I am trying to use Map. Commented Sep 4, 2018 at 18:22
  • You can store customer id, due amount in one map and customer id, count in another. Just use the same logic of updating due amount for count as well. Then iterate over the first map and check for amount condition. If it's true then print values from both map along with the key as customer id. Commented Sep 4, 2018 at 18:27

2 Answers 2

1

You can also improve your logic by storing customer id, due amount in one map and customer id, count in another.

Map<Long,Double> map1 = new TreeMap<>();
 Map<Long,Integer> map2 = new HashMap<>();
 if(map1.containsKey(custid)){
    Double currDue = map1.get(custid);
    currDue+=dueAmt;
    map1.put(custid,currDue);
    map2.put(custid,map2.get(custid)+1);
} else {
    map1.put(custid,dueAmt);
    map2.put(custid,1);
}

Lastly just iterate over map1's entry set and check if value >1000 then get count from map2 using same key.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it much easily and won't need two or more maps if you use Multimap on your code. Multimap allows you to store multiple values (here it will be multiple dueamt for a single custid) on same key and later iterate them over to add final amount you need. Also you don't need to count the amounts and simply check the number of values for each key to contain invoice counts. More details about Multimap is here.

Import it as :

import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap;

public static void main( String[] args ) throws FileNotFoundException, IOException {
    String line = null;
    Long custid;
    Double dueamt;
    Multimap< Long, Double > hashmap = ArrayListMultimap.create( );

    try (BufferedReader br1 = new BufferedReader( new FileReader( "input.txt" ) )) {

        while ( ( line = br1.readLine( ) ) != null ) {

            String[] detailsarr = line.split( "," );
            Long invoiceID = Long.parseLong( detailsarr[ 0 ] );
            String invoiceNumber = detailsarr[ 1 ];
            custid = Long.parseLong( detailsarr[ 2 ] );
            Double totalAmount = Double.parseDouble( detailsarr[ 3 ] );
            dueamt = Double.parseDouble( detailsarr[ 4 ] );
            if ( dueamt > 1000.00 ) {
                hashmap.put( custid, dueamt );
            }
        }
    }
    for ( Long key: hashmap.keySet( ) ) {
        System.out.println( "CustomerId " + key );
        System.out.println( "Number of invoice pending for payment:" + hashmap.get( key ).size( ) );
        System.out.println( "Total Due Amount: $" + hashmap.get( key ).stream( ).mapToDouble( Double::doubleValue ).sum( ) );
    }
}

Output :

CustomerId 110
Number of invoice pending for payment:1
Total Due Amount: $2000.0
CustomerId 111
Number of invoice pending for payment:2
Total Due Amount: $9000.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.