I was given the following question in a programming test but i cannot seem to understand why it could't clear all the test cases. What possible test cases am I missing?
A credit card company charges the customer on each transaction as per the following rules:
WAP to calculate the charge.
Input is given as follows: First line contain the number of transactions say N. The next N lines contain the transaction details in order : Transaction date,Customer Name,Amount.
Output: Print charges for transactions in the order of given transactions(one per line).
Eg. Sample input
10
2014-06-13,ABC RETAIL,10000.00
2014-06-14,ABC RETAIL,40000.00
2014-06-20,ABC RETAIL,1000.50
2014-06-23,XYZ RETAIL,9000.00
2014-06-23,ABC RETAIL,5000.00
2014-06-24,ABC RETAIL,100.00
2014-07-12,XYZ RETAIL,900.00
2014-07-13,XYZ RETAIL,55000.00
2014-07-13,XYZ RETAIL,550.00
2014-07-13,ABC RETAIL,105000.00
Sample output :
100.0
400.0
0.0
135.0
0.0
1.0
0.0
550.0
0.0
1050.0
Output Explained :
100.0 (Rule 3 applied)
400.0 (Rule 3 applied)
0.0 (Rule 5 applied for June for ABC RETAIL)
135.0 (Rule 2 applied)
0.0 (Rule 5 applied for July for ABC RETAIL)
1.0 (Rule 4 and 6 applied)
0.0 (Rule 5 applied for August for XYZ RETAIL)
550.0 (Rule 3 Applied)
0.0 (Rule 5 applied for August for xyz RETAIL)
1050.0 (Rule 3 Applied)
My code is as follows:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Merchant
{
ArrayList<Transaction> listTrans;
String name;
Merchant(String n)
{
listTrans=new ArrayList<Transaction> ();
name=n;
}
}
class Transaction
{
int date;
int month;
int year;
double amount;
double charge;
}
public class TestClass{
public static void main(String[] args) {
HashMap<String,Merchant> merchantMap=new HashMap<String,Merchant>();
Merchant merch=null;
String[] trans=new String[3];
String[] date=new String[3];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=null;
try
{
line=br.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
int N=Integer.parseInt(line);
for(int i=0;i<N;i++)
{
System.out.println("i = "+i);
try
{
line=br.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
trans=line.split(",");
date=trans[0].split("-");
merch=merchantMap.get(trans[1]);
if(merch==null)
{
merchantMap.put(trans[1],new Merchant(trans[1]));
merch=merchantMap.get(trans[1]);
}
Transaction t=new Transaction();
t.date=Integer.parseInt(date[2]);
t.month=Integer.parseInt(date[1]);
t.year=Integer.parseInt(date[0]);
t.amount=Double.parseDouble(trans[2]);
int countFree=0;
double totalAmount=0;
for(Transaction tr:merch.listTrans)
{
if(tr.year==t.year)
{
if(tr.month==t.month)
{
totalAmount+=tr.amount;
if(tr.amount<=5000)
countFree++;
}
}
}
if(countFree<2 && t.amount<=5000)
{
t.charge=0.00;
System.out.println("FREE RULE APPLIED");
}
else if(totalAmount>=50000)
{
t.charge=Math.ceil(t.amount*(0.5/100));
System.out.println("0.5 Applied.total amt>50000");
}
else
{
if(t.amount<5000)
{
t.charge=Math.ceil(t.amount*(2.0/100));
System.out.println("2.0 Applied.amt<5000");
}
else if(t.amount>=5000 && t.amount<=9999.99)
{
t.charge=Math.ceil(t.amount*(1.5/100));
System.out.println("1.5 Applied.amt=>5000..<=9999.99");
}
else
{
t.charge=Math.ceil(t.amount*(1.0/100));
System.out.println("1.0 Applied.amt>10,000");
}
}
merch.listTrans.add(t);
System.out.println(t.charge);
merch=null;
}
}
}
The rule regarding "is greater than 10000.00" should not be coded using "t.amount<=9999.99"
If anything, it should be t.amount <= 10000.0