Accenture Interview Question
Applications DevelopersTeam: Java Development
Country: India
Interview Type: In-Person
public class PosDataDiscount {
private List<Triplet<String,String,Integer>> lst = new ArrayList<>();
public void readEachLine()
{
File file = new File("C:\\Users\\zaloni422\\Desktop\\pig\\POSData");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String posValues[] = line.split(",");
Triplet<String, String, Integer> t = new Triplet<String, String, Integer>(
posValues[0], posValues[1], Integer.valueOf(posValues[2].trim()));
lst.add(t);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void chargeForEachTransaction() throws ParseException
{
Map<String,String> map = new HashMap<>();
for (Triplet t : lst) {
int trans_amt = 0;
Integer amount = (Integer) t.getValue2();
String month = (String) t.getValue0();
String merchantName = (String) t.getValue0();
if(map.containsKey(merchantName))
{
boolean sameMonthExists = checkForSameMonth(map,month);
if(sameMonthExists)
{
trans_amt = (int) 0.0;
}
}else
{
map.put(merchantName, month);
}
if (amount < 5000) {
trans_amt = (int) (amount * .002);
} else if (amount > 5000 && amount < 9999) {
trans_amt = (int) (amount * .015);
} else if (amount >= 10000) {
trans_amt = (int) (amount * .01);
}
System.out.println("trans_amt-------"+trans_amt);
}
}
private boolean checkForSameMonth(Map<String, String> map, String month) throws ParseException {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date1 = format.parse(month);
Date date2 = format.parse(map.get(month));
cal1.setTime(date1);
cal2.setTime(date2);
return cal1.get(Calendar.MONTH)==cal2.get(Calendar.MONTH);
}
public PosDataDiscount() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
PosDataDiscount pos = new PosDataDiscount();
try
{
pos.readEachLine();
pos.chargeForEachTransaction();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
I thought of this problem like this.
Construct a class to store the transactions.
Your class should have the following attributes:
So here is how I implemented the TransactionCompany class...
The logic of the implementation uses two HashMaps to store numbers of free charges given in a month
and the current total of transactions for a company.
That`s the reason why we should @Override hashCode and equals methods to be able to store the information in the hash maps correctly.
A hash for this class is the combination of the company and the month.
Then you can implement a method that takes as as input a List<TransactionCompany> and applies the rules specified to compute the charges for each transaction.
A driver program to read input from a file with the transactions would look like this:
I figured the Time complexity is O(n) where n is the number of records in the file and space complexity is O(n) too because you have to store two aditional hash maps that for the worst case scenario would have the same size of the number of records.
- Arthur December 18, 2014