Reputation: 63
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);
}
}
Upvotes: 1
Views: 135
Reputation: 1608
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
Upvotes: 0
Reputation: 176
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.
Upvotes: 1