-4

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:

  1. If transaction amount is less than 5000.00,charge is 2.0% of amount.
  2. If transaction amount is greater than equal to 5000.00,charge is 1.5% of amount.
  3. If transaction amount is greater than 10000.00,charge is 1.0% of amount.
  4. If previous transactions for a customer in that month are greater than 50000.00 charge is 0.5% of amount.
  5. The first two transactions for any month with amount less than equal to 5000.00 are free.
  6. All charges are rounded off to the next greater whole amount. eg. 9.23 to 10.00.

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;   
        }   
    }
}
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • For the sample input, what result does your code generate, and where does it differ? – nanofarad Aug 03 '14 at 20:55
  • The result for the test just showed "result does not match" without telling where it differs. It runs accurately for the given sample input on my system. – Ilisha Wadhwa Aug 03 '14 at 21:00
  • Regarding the test, are you guaranteed that the transactions are sorted by date? If not, you would not know which transactions are the first two. – jazeee Aug 03 '14 at 21:09
  • Yes it was mentioned that we are not supposed to sort and treat the input as it is. – Ilisha Wadhwa Aug 03 '14 at 21:12

1 Answers1

0

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

jazeee
  • 492
  • 5
  • 8
  • ok, this is one correction but it could not have led to failure of all the test cases. So there has to be more to it. But thank you :) – Ilisha Wadhwa Aug 03 '14 at 21:10
  • If I were the interviewer, I would probably use test data that fits the description, but breaks common code samples: For example Amounts larger than double can handle. (Use BigDecimal or BigInteger instead of double) Negative amounts. (Customers may be requesting a refund) Zero amounts. (should we skip that transaction?) – jazeee Aug 03 '14 at 21:26
  • As a side comment, financial transactions should never be done using floating point math. The general advice is to use BigDecimal or BigInteger to hold your financial amounts, since it will keep your full precision. – jazeee Aug 03 '14 at 21:30