Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, December 9, 2018

Bill Generator from Excel solution


Provided excel sheet of items with their discount
Then
Id Item Amount CouponAdded/Not

Generate bill by adding discount and deducting voucher if availed.

Input :
3
cloth 20.0 (discount)
elec 50
grocer 50.00
100.00(voucher value)
4
1 cloth 1 600.00 YES
3 elec 2 5000.00 NO
1 grocer 1 100.00 YES
2 cloth 2 500.00 N

Output
3 5000.0
2 800.0
1 430.0

import java.util.*;
import java.lang.*;
import java.io.*;
class approxHackr
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int i=0,n=ab.nextInt();
HashMap<String,Double> item=new HashMap<>();
while(i++<n)
{
String itemName=ab.next();
double cost=(100-ab.nextDouble())/100;
item.put(itemName,cost);
}
double couponAmt=ab.nextDouble();
     n=ab.nextInt();
HashMap<Integer,Double> bill=new HashMap<>();
HashSet<Integer> coupon=new HashSet<>();
i=0;
while(i++<n)
{
int id=ab.nextInt();
String itemName=ab.next();
int q=ab.nextInt();
double cost=ab.nextDouble();
boolean flag =(ab.next().compareTo("YES")==0)?true:false;
if(flag)
coupon.add(id);
double billed=bill.containsKey(id)?bill.get(id):0;
bill.put(id,billed+cost*item.get(itemName)*q);
}
for(int x:coupon)
{
double billed=bill.containsKey(x)?bill.get(x):0;
bill.put(x,billed-couponAmt);
}
Map<Double,Integer> out=new TreeMap<>(Collections.reverseOrder());
for(Map.Entry<Integer,Double> x:bill.entrySet())
{
out.put(x.getValue(),x.getKey());
}
for(Map.Entry<Double,Integer> x:out.entrySet())
{
System.out.print(x.getValue()+" "+x.getKey());
System.out.println();
}
}
}

0 Comments:

Post a Comment

Stats