Write a program to calculate discount of the account holders based on the transaction amount and registration date using below-mentioned prototype:
1. Read account details from the User. The details would include id, DOR (date of registration) and transaction amount in the given order. The datatype for id is the string, DOR is the string and transaction amount is the integer.
2. You decide to build two hashmaps. The first hashmap contains employee id as key and DOR as value, and the second hashmap contains same employee ids as key and amount as value.
3. Discount Amount as on 01/01/2015:
a. If the transaction amount greater than or equal to 20000 and registration greater than or equal to 5 years then discount rate is 20% of transaction amount.
b. If the transaction amount greater than or equal to 20000 and registration less then to 5 years then discount rate is 10% of transaction amount.
c. If the transaction amount less than to 20000 and registration greater than or equal to 5 years then discount rate is 15% of transaction amount.
d. If the transaction amount less than to 20000 and registration less then to 5 years then discount rate is 5% of transaction amount.
4. You decide to write a function calculateDiscount which takes the above hashmaps as input and returns the treemap as output. Include this function in class UserMainCode.
Create a Class Main which would be used to read employee details in step 1 and build the two hashmaps. Call the static method present in UserMainCode.
Input and Output Format:
Input consists of transaction details. The first number indicates the size of the employees. The next three values indicate the user id, user DOR, and transaction amount. The DOR (Date of Registration) format is “dd-mm-yyyy”
The output consists of a string which has the user id and discount amount one in a line for each user.
Refer sample output for formatting specifications.
Sample Input 1:
4
A-1010
20-11-2007
25000
B-1011
04-12-2010
30000
C-1012
11-11-2005
15000
D-1013
02-12-2012
10000
Sample Output 1:
A-1010:5000
B-1011:3000
C-1012:2250
D-1013:500
Discount Rate Calculation in Java.
import java.util.HashMap; import java.util.Iterator; import java.util.TreeMap; import java.util.Scanner; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = Integer.parseInt(sc.nextLine()); HashMap<String, String> hm = new HashMap<String, String>(); HashMap<String, Integer> hm1 = new HashMap<String, Integer>(); for (int i = 0; i < s; i++) { String id = sc.nextLine(); hm.put(id, sc.nextLine()); hm1.put(id, Integer.parseInt(sc.nextLine())); } TreeMap<String, Integer> tm = new TreeMap<String, Integer>(); tm = display(hm, hm1); Iterator<String> it = tm.keySet().iterator(); while (it.hasNext()) { String n = it.next(); int fac = tm.get(n); System.out.println(n + ":" + fac); } } public static TreeMap<String, Integer> display(HashMap<String, String> hm, HashMap<String, Integer> hm1) { int amount = 0; double dis = 0; String now = "01/01/2015"; TreeMap<String, Integer> tm = new TreeMap<String, Integer>(); Iterator<String> it = hm.keySet().iterator(); while (it.hasNext()) { String id = it.next(); String dor = hm.get(id); amount = hm1.get(id); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); try { Date d = new Date(); Date d1 = new Date(); String a = dor; String b = "01-01-2014"; d = sdf.parse(a); d1 = sdf.parse(b); long t = d.getTime(); long t1 = d1.getTime(); long t3 = t1 - t; long l1 = (24 * 60 * 60 * 1000); long l = l1 * 365; long year1 = t3 / l; System.out.println("Result=" + year1); if (year1 >= 5 && amount >= 20000) dis = 0.2 * amount; else if (year1 < 5 && amount >= 20000) dis = 0.1 * amount; else if (year1 >= 5 && amount < 20000) dis = 0.15 * amount; else dis = 0.05 * amount; tm.put(id, (int) dis); } catch (Exception e) { e.printStackTrace(); } } return tm; } }