×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Program That Implements Hash Maps Using Java Programming Language Assignment Solution

June 13, 2024
Dr. Seraphina Stormcloud
Dr. Seraphina
🇬🇧 United Kingdom
Java
Dr. Seraphina Stormcloud is a highly accomplished computer scientist who obtained her PhD from the prestigious University of Oxford, followed by post-doctoral research at the University of Cambridge in the UK. With seven years of experience under her belt, Seraphina has completed over 700 Java Homework assignments with exceptional skill and proficiency.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

Instructions

Objective

Write a java assignment program to implement hash maps using programming language.

Requirements and Specifications

Implement-hash-maps-using-Java-programming-language (1)

Source Code

package reports; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.text.MessageFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class ReportsMain { private static final String INPUT_FOLDER = "Resources"; private static final String OUTPUT_FOLDER = "Reports"; private static final String INPUT_FILENAME = "TimeDataMedium"; private static final String CLIENT_OUTPUT_PREFIX = "Client"; private static final String EMPLOYEE_OUTPUT_PREFIX = "Employee"; public static void main(String[] args) { Map employees = new HashMap<>(); Map> clients = new HashMap<>(); try (Scanner scanner = new Scanner(new File(INPUT_FOLDER + "/" + INPUT_FILENAME + ".csv"))) { scanner.nextLine(); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] parts = line.split(","); String employee = parts[0].trim(); String info = parts[1].trim(); String client = parts[3].trim(); if (!client.isEmpty()) { Set singleEmployee = new HashSet<>(); singleEmployee.add(employee); clients.merge(client, singleEmployee, (a, b) -> { a.addAll(b); return a; }); } if (info.equals("Clock In/Out")) { long minutes = getMinutes(parts[4], parts[5], parts[6], parts[7]); employees.merge(employee, minutes, Long::sum); } } } catch (IOException e) { throw new RuntimeException(e); } printClientReport(clients); printEmployeeReport(employees); } private static void printClientReport(Map> clients) { try (PrintWriter writer = new PrintWriter(OUTPUT_FOLDER + "/" + CLIENT_OUTPUT_PREFIX + INPUT_FILENAME + ".txt")) { List clientList = new ArrayList<>(clients.keySet()); Collections.sort(clientList); for (String client : clientList) { List employees = new ArrayList<>(clients.get(client)); Collections.sort(employees); writer.write(MessageFormat.format("CLIENT: {0} --- EMPLOYEES: [{1}]" + System.lineSeparator(), client, String.join(", ", employees))); } } catch (IOException e) { throw new RuntimeException(e); } } private static void printEmployeeReport(Map employees) { try (PrintWriter writer = new PrintWriter(OUTPUT_FOLDER + "/" + EMPLOYEE_OUTPUT_PREFIX + INPUT_FILENAME + ".txt")) { List employeeList = new ArrayList<>(employees.keySet()); Collections.sort(employeeList); for (String employee : employeeList) { Long minutes = employees.get(employee); int hours = (int)(minutes/60); int mins = (int)(minutes%60); String formatted = String.format("%02d:%02d", hours, mins); writer.write(MessageFormat.format("EMPLOYEE: {0} --- TIME: {1}" + System.lineSeparator(), employee, formatted)); } } catch (IOException e) { throw new RuntimeException(e); } } private static long getMinutes(String startTime, String endTime, String startDate, String endDate) { SimpleDateFormat format = new SimpleDateFormat("hh:mm a dd/MM/yyyy"); Date start = null; Date end = null; try { start = format.parse(startTime + " " + startDate); end = format.parse(endTime + " " + endDate); } catch (ParseException e) { e.printStackTrace(); return 0; } long diff = end.getTime() - start.getTime(); return diff / (60 * 1000); } }

Similar Sample

Explore our sample solutions to see the quality of work we deliver. Each example demonstrates our expertise in various programming languages and our problem-solving skills. Discover why students trust us for their programming homework needs and experience our dedication to excellence.