diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..1b799f3 --- /dev/null +++ b/.classpath @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..5edc1ee --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + coding-competition + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ea7a397 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,16 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.jdt.ui.prefs b/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000..25c0923 --- /dev/null +++ b/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,74 @@ +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.convert_to_enhanced_for_loop_if_loop_var_used=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.insert_inferred_type_arguments=false +sp_cleanup.lazy_logical_operator=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.merge_conditional_blocks=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.number_suffix=false +sp_cleanup.objects_equals=false +sp_cleanup.on_save_use_additional_actions=false +sp_cleanup.organize_imports=true +sp_cleanup.precompile_regex=false +sp_cleanup.push_down_negation=false +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_redundant_modifiers=false +sp_cleanup.remove_redundant_semicolons=false +sp_cleanup.remove_redundant_type_arguments=false +sp_cleanup.remove_trailing_whitespaces=false +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_array_creation=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unused_imports=false +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.simplify_lambda_expression_and_method_ref=false +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_anonymous_class_creation=false +sp_cleanup.use_autoboxing=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_directly_map_method=false +sp_cleanup.use_lambda=true +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true +sp_cleanup.use_unboxing=false +sp_cleanup.use_var=false diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml index 21d55bf..daa4bce 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,11 @@ jackson-dataformat-csv 2.11.2 + + com.google.code.gson + gson + 2.8.6 + diff --git a/src/main/java/sf/codingcompetition2020/AgentReport.java b/src/main/java/sf/codingcompetition2020/AgentReport.java new file mode 100644 index 0000000..3d18bea --- /dev/null +++ b/src/main/java/sf/codingcompetition2020/AgentReport.java @@ -0,0 +1,113 @@ +package sf.codingcompetition2020; + +import java.awt.Color; +import java.awt.Container; +import java.awt.GridLayout; +import java.util.HashMap; +import java.util.List; + +import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.JFrame; +import javax.swing.JLabel; + +import sf.codingcompetition2020.structures.Agent; + +public class AgentReport extends JFrame{ + + int width = 2400; + int height = 400; + + + public AgentReport(String area, String agentsFilePath) { + + CodingCompCsvUtil csvUtil = new CodingCompCsvUtil(); + + int total = csvUtil.getAgentCountInArea(agentsFilePath, area); + + this.setLayout(new GridLayout(10, 1)); + + this.setSize(width, height); + + this.setTitle("Area Report for: " + area); + + + + Container pane = this.getContentPane(); + pane.setLayout(new GridLayout(10, 1)); + + int areaCount = csvUtil.getAgentCountInArea(agentsFilePath, area); + + JLabel agentHeaderLabel = new JLabel("Number of agents in area : " + Integer.toString(areaCount)); + pane.add(agentHeaderLabel); + + + // language to agents speaking it in area + HashMap> languageToAgents = new HashMap>(); + + List languages = csvUtil.getAllAgentLanguagesInArea(agentsFilePath, area); + + for(String language : languages) { + csvUtil.getAgentsInAreaThatSpeakLanguage(agentsFilePath, area, language); + List agentsSpeakignLanugage = csvUtil.getAgentsInAreaThatSpeakLanguage(agentsFilePath, area, language); + languageToAgents.put(language, agentsSpeakignLanugage); + } + + JLabel languageTableLabel = new JLabel("Language : Amount Spoken in Area : COLOR"); + pane.add(languageTableLabel); + + // This is an abomination I apologize in advance, haha + + Color[] colors = new Color[] {Color.RED, Color.GREEN, Color.gray, Color.CYAN, Color.black, Color.BLUE, Color.PINK, Color.MAGENTA, Color.YELLOW}; + HashMap colorToString = new HashMap(){{ + put(Color.RED, "RED"); + put(Color.GREEN, "GREEN"); + put(Color.gray, "GRAY"); + put(Color.CYAN, "CYAN"); + put(Color.black, "BLACK"); + put(Color.BLUE, "BLUE"); + put(Color.PINK, "PINK"); + put(Color.MAGENTA, "MAGENTA"); + put(Color.YELLOW, "YELLOW"); + }}; + + int currentColor = 0; + int maxColor = 0; + + for(String language : languageToAgents.keySet()) { + int amountSpoken = languageToAgents.get(language).size(); + JLabel languageLabel = new JLabel( language + " : " + Integer.toString(amountSpoken) + " : " + colorToString.get(colors[currentColor])); + pane.add(languageLabel); + + currentColor++; + if(currentColor == colors.length) { + currentColor = 0; + } + } + + + + BarGraph graph = new BarGraph(55, 55); + + this.getContentPane().add(graph); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.pack(); + + currentColor = 0; + maxColor = 0; + + for(String language : languageToAgents.keySet()) { + + int amountSpoken = languageToAgents.get(language).size(); + graph.addBar(colors[currentColor], amountSpoken); + currentColor++; + if(currentColor == colors.length) { + currentColor = 0; + } + } + + + + } + +} diff --git a/src/main/java/sf/codingcompetition2020/AreasScreen.java b/src/main/java/sf/codingcompetition2020/AreasScreen.java new file mode 100644 index 0000000..e3db005 --- /dev/null +++ b/src/main/java/sf/codingcompetition2020/AreasScreen.java @@ -0,0 +1,187 @@ +package sf.codingcompetition2020; + +import javax.swing.JFrame; +import java.awt.Container; +import java.awt.FlowLayout; +import java.awt.GridLayout; +import java.awt.Label; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import javax.swing.Box; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; + +import codingcompetition2019.AllNaturalDisasters; + +public class AreasScreen extends JFrame{ + + private final String agentFilePath = "src/main/resources/DataFiles/agents.csv"; + private final String claimFilePath = "src/main/resources/DataFiles/claims.csv"; + private final String customerFilePath = "src/main/resources/DataFiles/customers.csv"; + private final String vendorFilePath = "src/main/resources/DataFiles/vendors.csv"; + + private CodingCompCsvUtil csvUtil; + private JComboBox areaComboBox; + private ArrayList areas; + + public AreasScreen() { + csvUtil = new CodingCompCsvUtil(); + + this.setTitle("AgentReport"); + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + + this.setSize(400, 100); + + csvUtil = new CodingCompCsvUtil(); + + + JLabel areaLabel = new JLabel("View data in area: "); + areaLabel.setVerticalAlignment(JLabel.TOP); + this.add(areaLabel); + + areas = (ArrayList) csvUtil.getAllAgentAreas(agentFilePath); + + areas.add(0, "Choose an area."); + + this.areaComboBox = new JComboBox(areas.toArray()); + this.setLayout(new FlowLayout()); + this.add(this.areaComboBox); + + JButton viewDataButton = new JButton("View Data"); + this.getContentPane().add(viewDataButton); // Adds Button to content pane of frame + + + this.setVisible(true); + + viewDataButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + if(areaComboBox.getSelectedIndex() == 0) { + return; + } + String area = areas.get(areaComboBox.getSelectedIndex() - 1); + new AgentReport(area, agentFilePath).setVisible(true); + + } + }); + } +} + + + +//package sf.codingcompetition2020; +// +//import java.awt.Container; +//import java.awt.FlowLayout; +//import java.awt.GridLayout; +//import java.awt.Label; +//import java.awt.event.ActionEvent; +//import java.awt.event.ActionListener; +//import java.util.ArrayList; +//import java.util.Arrays; +//import java.util.List; +//import java.util.stream.Stream; +// +//import javax.swing.Box; +//import javax.swing.JButton; +//import javax.swing.JComboBox; +//import javax.swing.JFrame; +//import javax.swing.JLabel; +// +//import codingcompetition2019.AllNaturalDisasters; +//import sf.codingcompetition2020.structures.Agent; +// +//public class HomeScreen { +// +// private static final String agentFilePath = "src/main/resources/DataFiles/agents.csv"; +// private static final String claimFilePath = "src/main/resources/DataFiles/claims.csv"; +// private static final String customerFilePath = "src/main/resources/DataFiles/customers.csv"; +// private static final String vendorFilePath = "src/main/resources/DataFiles/vendors.csv"; +// +// public static void main(String args[]) { +// JFrame frame = new JFrame("Agent Report"); +// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); +// frame.setLocationRelativeTo(null); +// +// frame.setSize(400, 100); +// +//// JLabel headerLabel = new JLabel("Choose an Agent to view a report:"); +//// headerLabel.setVerticalAlignment(JLabel.TOP); +//// frame.add(headerLabel); +//// +// CodingCompCsvUtil csvUtil = new CodingCompCsvUtil(); +//// +//// ArrayList agents = (ArrayList) csvUtil.readCsvFile(agentFilePath, Agent.class); +//// +//// +//// ArrayList agentNames = new ArrayList(); +//// +//// agentNames.add("Ghoose an Agent:"); +//// +// +//// for(Agent agent : agents) { +//// agentNames.add(agent.getFirstName() + " " + agent.getLastName()); +//// } +// +// JLabel areaLabel = new JLabel("View data in area: "); +// areaLabel.setVerticalAlignment(JLabel.TOP); +// frame.add(areaLabel); +// +// ArrayList areas = new ArrayList(); +// +//// ArrayList areas = (ArrayList) csvUtil.getAllAreas(agentFilePath); +// +// areas.add(0, "Choose an area."); +// +// JComboBox areaComboBox = new JComboBox(areas.toArray()); +// frame.setLayout(new FlowLayout()); +// frame.add(areaComboBox); +// +// JButton button = new JButton("View Data"); +// frame.getContentPane().add(button); // Adds Button to content pane of frame +// +// +// frame.setVisible(true); +// +// btnGetData.addActionListener(new ActionListener() { +// public void buttonClick(ActionEvent e) { +// String area = areas.get(areaComboBox.getSelectedIndex() - 1); +// JFrame agentAreaFrame = this.ViewAgentsByArea(csvUtil, area); +// agentAreaFrame.setVisible(true); +// +// } +// }); +// +// +// } +// +// +// +// public static JFrame ViewAgentsByArea(CodingCompCsvUtil csvUtil, String area) { +// +// JFrame frame = new JFrame(); +// +// int total = csvUtil.getAgentCountInArea(agentFilePath ,area); +// +// Container pane = frame.getContentPane(); +// pane.setLayout(new GridLayout(2, 2)); +// pane.add(Box.createVerticalGlue()); +// +// JLabel headerLabel = new JLabel("Number of agents in area:"); +// headerLabel.setVerticalAlignment(JLabel.TOP); +// frame.add(headerLabel); +// +// return frame; +// +// +// } +// +//} diff --git a/src/main/java/sf/codingcompetition2020/BarGraph.java b/src/main/java/sf/codingcompetition2020/BarGraph.java new file mode 100644 index 0000000..2637a8e --- /dev/null +++ b/src/main/java/sf/codingcompetition2020/BarGraph.java @@ -0,0 +1,101 @@ +package sf.codingcompetition2020; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.util.ArrayList; + +import javax.swing.JPanel; + +public class BarGraph extends JPanel { + private ArrayList bars = new ArrayList(); +// private ArrayList keys = new ArrayList(); + + private int width; + private int height; + + private int chartWidth; + private int chartHeight; + + public BarGraph(int width, int height) { + this.height = height; + this.width = width; + +// this.chartHeight = (int) (height * .7); +// this.chartWidth = (int) (width * .7); +// +// JPanel dataTypePanel = new JPanel(); +// dataTypePanel.setLayout(new GridLayout(1, 2)); +// dataTypePanel.setBorder(new EmptyBorder(20, 20, 20, 20)); +// dataTypePanel.add(new JLabel("Select Data Type")); + } + +// public BarGraph(int width, int height, int chartWidth, int chartHeight) { +// this.height = height; +// this.width = width; +// +// this.chartWidth = chartWidth; +// this.chartHeight = chartHeight; +// } + + public void addBar(Color color, int size) { + Bar bar = new Bar(color, size); + this.bars.add(bar); + repaint(); + } + +// public void addKey(Color color, String description) { +// Key key = new Key(color, description); +// this.keys.add(key); +// repaint(); +// } + + @Override + protected void paintComponent(Graphics g) { + int max = Integer.MIN_VALUE; + for (Bar bar : bars) { + if (bar.size > max) { + max = bar.size; + } + } + + // Draw the actual bars + int width = (getWidth() / bars.size()) - 2; + int x = 1; + for (Bar bar : bars) { + Color color = bar.color; + int height = (int) ((getHeight() - 5) * ((double) bar.size / max)); + g.setColor(color); + g.fillRect(x, getHeight() - height, width, height); + g.setColor(Color.black); + g.drawRect(x, getHeight() - height, width, height); + x += (width + 2); + } + + } + + @Override + public Dimension getPreferredSize() { + return new Dimension(this.height, this.width); + } +} + +class Bar { + public Color color; + public Integer size; + + public Bar(Color color, Integer size) { + this.color = color; + this.size = size; + } +} + +//class Key { +// public Color color; +// public String description; +// +// public Key(Color color, String description) { +// this.color = color; +// this.description = description; +// } +//} \ No newline at end of file diff --git a/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java b/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java index 58267da..25e2edb 100644 --- a/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java +++ b/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java @@ -1,147 +1,354 @@ package sf.codingcompetition2020; -import java.io.FileReader; -import java.io.Reader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InvalidClassException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.Scanner; import java.util.stream.Collectors; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MappingIterator; -import com.fasterxml.jackson.databind.ObjectReader; -import com.fasterxml.jackson.dataformat.csv.CsvMapper; -import com.fasterxml.jackson.dataformat.csv.CsvSchema; - import sf.codingcompetition2020.structures.Agent; import sf.codingcompetition2020.structures.Claim; import sf.codingcompetition2020.structures.Customer; import sf.codingcompetition2020.structures.Vendor; public class CodingCompCsvUtil { - - /* #1 - * readCsvFile() -- Read in a CSV File and return a list of entries in that file. + + /* + * #1 readCsvFile() -- Read in a CSV File and return a list of entries in that + * file. + * * @param filePath -- Path to file being read in. + * * @param classType -- Class of entries being read in. + * * @return -- List of entries being returned. */ public List readCsvFile(String filePath, Class classType) { + Class myClass = classType.getClass(); + List list = new ArrayList(); + + File in = new File(filePath); + Scanner readMe = new Scanner(System.in); + try { + readMe = new Scanner(in); + String line = ""; + if (readMe.hasNextLine()) { + line = readMe.nextLine(); + } + while (readMe.hasNextLine()) { + line = readMe.nextLine(); + + if (classType.equals(Agent.class)) { + list.add((T) Agent.parseFromString(line)); + } else if (classType.equals(Claim.class)) { + list.add((T) Claim.parseFromString(line)); + } else if (classType.equals(Customer.class)) { + list.add((T) Customer.parseFromString(line)); + } else if (classType.equals(Vendor.class)) { + list.add((T) Vendor.parseFromString(line)); + } else { + throw new InvalidClassException("Cannot convert to class: " + classType.toString()); + } + } + } catch (FileNotFoundException e) { + System.out.println("No file found at " + filePath); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + readMe.close(); + return list; + } + + private List getStructInArea(String filePath, String area, Class classType) throws InvalidClassException { + List list = readCsvFile(filePath, classType); + List listarea = new ArrayList(); + String s = ""; + for (T struct : list) { + if (classType.equals(Agent.class)) { + s = ((Agent) struct).getArea(); + } else if (classType.equals(Claim.class)) { + throw new InvalidClassException("Cannot get on area of class" + classType.toString()); + } else if (classType.equals(Customer.class)) { + s = ((Customer) struct).getArea(); + } else if (classType.equals(Vendor.class)) { + s = ((Vendor) struct).getArea(); + } + if (s.equals(area)) { + listarea.add(struct); + } + } + return listarea; } - - /* #2 - * getAgentCountInArea() -- Return the number of agents in a given area. + /* + * #2 getAgentCountInArea() -- Return the number of agents in a given area. + * * @param filePath -- Path to file being read in. + * * @param area -- The area from which the agents should be counted. + * * @return -- The number of agents in a given area */ - public int getAgentCountInArea(String filePath,String area) { + public int getAgentCountInArea(String filePath, String area) { + int result = 0; + try { + result = (int) getStructInArea(filePath, area, Agent.class).size(); + } catch (InvalidClassException e) { + System.out.println("Should Not Happen: " + e.getMessage()); + throw new RuntimeException(e.getMessage()); + } + return result; + } + + // TODO:this + public void getallareas(String agentFilePath) { } - - /* #3 - * getAgentsInAreaThatSpeakLanguage() -- Return a list of agents from a given area, that speak a certain language. + /* + * #3 getAgentsInAreaThatSpeakLanguage() -- Return a list of agents from a given + * area, that speak a certain language. + * * @param filePath -- Path to file being read in. + * * @param area -- The area from which the agents should be counted. + * * @param language -- The language spoken by the agent(s). + * * @return -- The number of agents in a given area */ public List getAgentsInAreaThatSpeakLanguage(String filePath, String area, String language) { - + List agents = new ArrayList(); + try { + agents = getStructInArea(filePath, area, Agent.class); + } catch (Exception e) { + System.out.println("Should Not Happen: " + e.getMessage()); + } + List result = new ArrayList(); + for (Agent agent : agents) { + if (agent.getLanguage().equals(language)) { + result.add(agent); + } + } + return result; } - - - /* #4 - * countCustomersFromAreaThatUseAgent() -- Return the number of individuals from an area that use a certain agent. + + /* + * #4 countCustomersFromAreaThatUseAgent() -- Return the number of individuals + * from an area that use a certain agent. + * * @param filePath -- Path to file being read in. + * * @param customerArea -- The area from which the customers should be counted. + * * @param agentFirstName -- First name of agent. + * * @param agentLastName -- Last name of agent. + * * @return -- The number of customers that use a certain agent in a given area. */ - public short countCustomersFromAreaThatUseAgent(Map csvFilePaths, String customerArea, String agentFirstName, String agentLastName) { - + public short countCustomersFromAreaThatUseAgent(Map csvFilePaths, String customerArea, + String agentFirstName, String agentLastName) { + List custs = new ArrayList(); + List agents = new ArrayList(); + String filePath1 = csvFilePaths.get("customerList"); + String filePath2 = csvFilePaths.get("agentList"); + short count = 0; + try { + custs = getStructInArea(filePath1, customerArea, Customer.class); + agents = readCsvFile(filePath2, Agent.class); + } catch (Exception e) { + System.out.println("Should Not Happen: " + e.getMessage()); + } + + for (Customer cust : custs) { + for (Agent agent : agents) { + if (cust.getAgentId() == agent.getAgentId() && agent.getFirstName().equals(agentFirstName) + && agent.getLastName().equals(agentLastName)) { + count++; + } + } + } + return count; } - - /* #5 - * getCustomersRetainedForYearsByPlcyCostAsc() -- Return a list of customers retained for a given number of years, in ascending order of their policy cost. + /* + * #5 getCustomersRetainedForYearsByPlcyCostAsc() -- Return a list of customers + * retained for a given number of years, in ascending order of their policy + * cost. + * * @param filePath -- Path to file being read in. + * * @param yearsOfServeice -- Number of years the person has been a customer. - * @return -- List of customers retained for a given number of years, in ascending order of policy cost. + * + * @return -- List of customers retained for a given number of years, in + * ascending order of policy cost. */ public List getCustomersRetainedForYearsByPlcyCostAsc(String customerFilePath, short yearsOfService) { - + List custs = readCsvFile(customerFilePath, Customer.class); + List resultCusts = new ArrayList(); + for (Customer cust : custs) { + if (cust.getYearsOfService() == yearsOfService) { + resultCusts.add(cust); + } + } + resultCusts = resultCusts.stream().sorted((a, b) -> { + return Integer.compare(Integer.parseInt(a.getTotalMonthlyPremium().replaceAll("\\$", "")), + Integer.parseInt(b.getTotalMonthlyPremium().replaceAll("\\$", ""))); + }).collect(Collectors.toList()); + return resultCusts; } - - /* #6 - * getLeadsForInsurance() -- Return a list of individuals who’ve made an inquiry for a policy but have not signed up. - * *HINT* -- Look for customers that currently have no policies with the insurance company. + /* + * #6 getLeadsForInsurance() -- Return a list of individuals who’ve made an + * inquiry for a policy but have not signed up. *HINT* -- Look for customers + * that currently have no policies with the insurance company. + * * @param filePath -- Path to file being read in. - * @return -- List of customers who’ve made an inquiry for a policy but have not signed up. + * + * @return -- List of customers who’ve made an inquiry for a policy but have + * not signed up. */ public List getLeadsForInsurance(String filePath) { - + List custs = readCsvFile(filePath, Customer.class); + List resultCusts = new ArrayList(); + for (Customer cust : custs) { + if (!(cust.isAutoPolicy() || cust.isHomePolicy() || cust.isRentersPolicy())) { + resultCusts.add(cust); + } + } + return resultCusts; } - - /* #7 - * getVendorsWithGivenRatingThatAreInScope() -- Return a list of vendors within an area and include options to narrow it down by: - a. Vendor rating - b. Whether that vendor is in scope of the insurance (if inScope == false, return all vendors in OR out of scope, if inScope == true, return ONLY vendors in scope) + /* + * #7 getVendorsWithGivenRatingThatAreInScope() -- Return a list of vendors + * within an area and include options to narrow it down by: a. Vendor rating b. + * Whether that vendor is in scope of the insurance (if inScope == false, return + * all vendors in OR out of scope, if inScope == true, return ONLY vendors in + * scope) + * * @param filePath -- Path to file being read in. + * * @param area -- Area of the vendor. + * * @param inScope -- Whether or not the vendor is in scope of the insurance. + * * @param vendorRating -- The rating of the vendor. - * @return -- List of vendors within a given area, filtered by scope and vendor rating. + * + * @return -- List of vendors within a given area, filtered by scope and vendor + * rating. */ - public List getVendorsWithGivenRatingThatAreInScope(String filePath, String area, boolean inScope, int vendorRating) { - + public List getVendorsWithGivenRatingThatAreInScope(String filePath, String area, boolean inScope, + int vendorRating) { + List vendors = new ArrayList(); + List resultvendors = new ArrayList(); + try { + vendors = getStructInArea(filePath, area, Vendor.class); + } catch (Exception e) { + System.out.println("Should Not Happen: " + e.getMessage()); + } + for (Vendor vendor : vendors) { + if (vendor.getVendorRating() >= vendorRating && ((inScope && vendor.isInScope()) || !inScope)) { + resultvendors.add(vendor); + } + } + return resultvendors; } - - /* #8 - * getUndisclosedDrivers() -- Return a list of customers between the age of 40 and 50 years (inclusive), who have: - a. More than X cars - b. less than or equal to X number of dependents. + /* + * #8 getUndisclosedDrivers() -- Return a list of customers between the age of + * 40 and 50 years (inclusive), who have: a. More than X cars b. less than or + * equal to X number of dependents. + * * @param filePath -- Path to file being read in. + * * @param vehiclesInsured -- The number of vehicles insured. + * * @param dependents -- The number of dependents on the insurance policy. - * @return -- List of customers filtered by age, number of vehicles insured and the number of dependents. + * + * @return -- List of customers filtered by age, number of vehicles insured and + * the number of dependents. */ public List getUndisclosedDrivers(String filePath, int vehiclesInsured, int dependents) { + List custs = readCsvFile(filePath, Customer.class); + List resultcusts = new ArrayList(); + for (Customer cust : custs) { + if (cust.getAge() >= 40 && cust.getAge() <= 50 && cust.getVehiclesInsured() > vehiclesInsured) { + if ((cust.getDependents() != null && cust.getDependents().size() <= dependents) + || (cust.getDependents() == null && 0 <= dependents)) { + resultcusts.add(cust); + } + } + } + return resultcusts; + } - } - - - /* #9 - * getAgentIdGivenRank() -- Return the agent with the given rank based on average customer satisfaction rating. - * *HINT* -- Rating is calculated by taking all the agent rating by customers (1-5 scale) and dividing by the total number - * of reviews for the agent. + /* + * #9 getAgentIdGivenRank() -- Return the agent with the given rank based on + * average customer satisfaction rating. *HINT* -- Rating is calculated by + * taking all the agent rating by customers (1-5 scale) and dividing by the + * total number of reviews for the agent. + * * @param filePath -- Path to file being read in. + * * @param agentRank -- The rank of the agent being requested. + * * @return -- Agent ID of agent with the given rank. */ public int getAgentIdGivenRank(String filePath, int agentRank) { - - } + List agents = readCsvFile(filePath, Agent.class); + HashMap ratings = new HashMap(); + int agentID = 0; + List custs = readCsvFile(filePath, Customer.class); + for (Customer cust : custs) { + if (!ratings.containsKey(cust.getAgentId())) { + ratings.put(cust.getAgentId(), (int) cust.getAgentRating()); + } else { + ratings.put(cust.getAgentId(), (ratings.get(cust.getAgentId()) + cust.getAgentRating()) / 2); + } + } + for (Agent agent : agents) { + if (ratings.get(agent.getAgentId()) != null && ratings.get(agent.getAgentId()) == agentRank) { + agentID = agent.getAgentId(); + } + } + return agentID; + } - - /* #10 - * getCustomersWithClaims() -- Return a list of customers who’ve filed a claim within the last (inclusive). + /* + * #10 getCustomersWithClaims() -- Return a list of customers who’ve filed a + * claim within the last (inclusive). + * * @param filePath -- Path to file being read in. + * * @param monthsOpen -- Number of months a policy has been open. - * @return -- List of customers who’ve filed a claim within the last . + * + * @return -- List of customers who’ve filed a claim within the last + * . */ - public List getCustomersWithClaims(Map csvFilePaths, short monthsOpen) { - - } + public List getCustomersWithClaims(Map csvFilePaths, short monthsOpen) { + String filePath1 = csvFilePaths.get("customerList"); + String filePath2 = csvFilePaths.get("claimList"); + List custs = readCsvFile(filePath1, Customer.class); + List claims = readCsvFile(filePath2, Claim.class); + List resultcusts = new ArrayList(); + for (Claim claim : claims) { + for (Customer cust : custs) { + if (claim.getCustomerId() == cust.getCustomerId() && claim.getMonthsOpen() <= monthsOpen) { + if (!resultcusts.contains(cust)) { + resultcusts.add(cust); + } + } + } + } + return resultcusts; + } } diff --git a/src/main/java/sf/codingcompetition2020/HomeScreen.java b/src/main/java/sf/codingcompetition2020/HomeScreen.java new file mode 100644 index 0000000..bb73b27 --- /dev/null +++ b/src/main/java/sf/codingcompetition2020/HomeScreen.java @@ -0,0 +1,57 @@ +package sf.codingcompetition2020; + +import java.awt.Container; +import java.awt.FlowLayout; +import java.awt.GridLayout; +import java.awt.Label; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import javax.swing.Box; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; + +import codingcompetition2019.AllNaturalDisasters; +import sf.codingcompetition2020.structures.Agent; + +public class HomeScreen { + + private static final String agentFilePath = "src/main/resources/DataFiles/agents.csv"; + private static final String claimFilePath = "src/main/resources/DataFiles/claims.csv"; + private static final String customerFilePath = "src/main/resources/DataFiles/customers.csv"; + private static final String vendorFilePath = "src/main/resources/DataFiles/vendors.csv"; + + public static void main(String args[]) { + AreasScreen screen = new AreasScreen(); + screen.setVisible(true); + + } + + + + public static JFrame ViewAgentsByArea(CodingCompCsvUtil csvUtil, String area) { + + JFrame frame = new JFrame(); + + int total = csvUtil.getAgentCountInArea(agentFilePath ,area); + + Container pane = frame.getContentPane(); + pane.setLayout(new GridLayout(2, 2)); + pane.add(Box.createVerticalGlue()); + + JLabel headerLabel = new JLabel("Number of agents in area:"); + headerLabel.setVerticalAlignment(JLabel.TOP); + frame.add(headerLabel); + + return frame; + + + } + +} diff --git a/src/main/java/sf/codingcompetition2020/structures/Agent.java b/src/main/java/sf/codingcompetition2020/structures/Agent.java index e2e6f93..d4987c5 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Agent.java +++ b/src/main/java/sf/codingcompetition2020/structures/Agent.java @@ -1,11 +1,46 @@ package sf.codingcompetition2020.structures; public class Agent { - + private int agentId; private String area; private String language; private String firstName; private String lastName; - + + private Agent() { + // blank + } + + public static Agent parseFromString(String line) { + Agent agent = new Agent(); + String[] arr = line.split(","); + agent.agentId = Integer.parseInt(arr[0]); + agent.area = arr[1]; + agent.language = arr[2]; + agent.firstName = arr[3]; + agent.lastName = arr[4]; + + return agent; + } + + public int getAgentId() { + return agentId; + } + + public String getArea() { + return area; + } + + public String getLanguage() { + return language; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } } diff --git a/src/main/java/sf/codingcompetition2020/structures/Claim.java b/src/main/java/sf/codingcompetition2020/structures/Claim.java index 581140a..5631ac2 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Claim.java +++ b/src/main/java/sf/codingcompetition2020/structures/Claim.java @@ -5,5 +5,35 @@ public class Claim { private int customerId; private boolean closed; private int monthsOpen; - + + private Claim() { + // blank + } + + public static Claim parseFromString(String line) { + Claim claim = new Claim(); + String[] arr = line.split(","); + claim.claimId = Integer.parseInt(arr[0]); + claim.customerId = Integer.parseInt(arr[1]); + claim.closed = Boolean.parseBoolean(arr[2]); + claim.monthsOpen = Integer.parseInt(arr[3]); + + return claim; + } + + public int getClaimId() { + return claimId; + } + + public int getCustomerId() { + return customerId; + } + + public boolean isClosed() { + return closed; + } + + public int getMonthsOpen() { + return monthsOpen; + } } diff --git a/src/main/java/sf/codingcompetition2020/structures/Customer.java b/src/main/java/sf/codingcompetition2020/structures/Customer.java index f151906..dc181a5 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Customer.java +++ b/src/main/java/sf/codingcompetition2020/structures/Customer.java @@ -3,9 +3,8 @@ import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; public class Customer { private int customerId; @@ -24,4 +23,98 @@ public class Customer { private short yearsOfService; private Integer vehiclesInsured; + private Customer() { + // blank + } + + public static Customer parseFromString(String line) { + Customer customer = new Customer(); + // https://stackoverflow.com/questions/18893390/splitting-on-comma-outside-quotes + // for regex + String[] arr = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); + Gson son = new Gson(); + customer.customerId = Integer.parseInt(arr[0]); + customer.firstName = arr[1]; + customer.lastName = arr[2]; + customer.age = Integer.parseInt(arr[3]); + customer.area = arr[4]; + customer.agentId = Integer.parseInt(arr[5]); + customer.agentRating = Short.parseShort(arr[6]); + customer.primaryLanguage = arr[7]; + customer.dependents = new ArrayList(); + try { + customer.dependents = son.fromJson(arr[8], ArrayList.class); + } catch (JsonSyntaxException e) { + System.out.print(arr[8]); + } + customer.homePolicy = Boolean.parseBoolean(arr[9]); + customer.autoPolicy = Boolean.parseBoolean(arr[10]); + customer.rentersPolicy = Boolean.parseBoolean(arr[11]); + customer.totalMonthlyPremium = arr[12]; + customer.yearsOfService = Short.parseShort(arr[13]); + customer.vehiclesInsured = Integer.parseInt(arr[14]); + + return customer; + } + + public int getCustomerId() { + return customerId; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public int getAge() { + return age; + } + + public String getArea() { + return area; + } + + public int getAgentId() { + return agentId; + } + + public short getAgentRating() { + return agentRating; + } + + public String getPrimaryLanguage() { + return primaryLanguage; + } + + public List getDependents() { + return dependents; + } + + public boolean isHomePolicy() { + return homePolicy; + } + + public boolean isAutoPolicy() { + return autoPolicy; + } + + public boolean isRentersPolicy() { + return rentersPolicy; + } + + public String getTotalMonthlyPremium() { + return totalMonthlyPremium; + } + + public short getYearsOfService() { + return yearsOfService; + } + + public Integer getVehiclesInsured() { + return vehiclesInsured; + } + } diff --git a/src/main/java/sf/codingcompetition2020/structures/Dependent.java b/src/main/java/sf/codingcompetition2020/structures/Dependent.java index d4deb1a..ac10374 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Dependent.java +++ b/src/main/java/sf/codingcompetition2020/structures/Dependent.java @@ -4,4 +4,16 @@ public class Dependent { private String firstName; private String lastName; + public Dependent(String firstName, String lastName) { + this.firstName = firstName; + this.firstName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } } diff --git a/src/main/java/sf/codingcompetition2020/structures/Vendor.java b/src/main/java/sf/codingcompetition2020/structures/Vendor.java index 6b6fb76..aed96a9 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Vendor.java +++ b/src/main/java/sf/codingcompetition2020/structures/Vendor.java @@ -5,5 +5,36 @@ public class Vendor { private String area; private int vendorRating; private boolean inScope; - + + private Vendor() { + // blank + } + + public static Vendor parseFromString(String line) { + Vendor vendor = new Vendor(); + String[] arr = line.split(","); + vendor.vendorId = Integer.parseInt(arr[0]); + vendor.area = arr[1]; + vendor.vendorRating = Integer.parseInt(arr[2]); + vendor.inScope = Boolean.parseBoolean(arr[3]); + + return vendor; + } + + public int getVendorId() { + return vendorId; + } + + public String getArea() { + return area; + } + + public int getVendorRating() { + return vendorRating; + } + + public boolean isInScope() { + return inScope; + } + } diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF index e2a1a34..8952561 100644 --- a/target/classes/META-INF/MANIFEST.MF +++ b/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Built-By: yc1d -Build-Jdk: 1.8.0_201 +Built-By: Killian +Build-Jdk: 14.0.2 Created-By: Maven Integration for Eclipse diff --git a/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.properties b/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.properties index fe569e3..f09e580 100644 --- a/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.properties +++ b/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.properties @@ -1,7 +1,7 @@ #Generated by Maven Integration for Eclipse -#Thu Oct 08 09:27:33 MST 2020 -version=1.0.0-SNAPSHOT -groupId=sf.codingcompetition2020 +#Sat Oct 10 20:08:20 CDT 2020 +m2e.projectLocation=C\:\\Users\\Killian\\Desktop\\2020 m2e.projectName=coding-competition -m2e.projectLocation=/Users/yc1d/Development/coding-competition/problem/online-competition +groupId=sf.codingcompetition2020 artifactId=coding-competition +version=1.0.0-SNAPSHOT diff --git a/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.xml b/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.xml index 21d55bf..daa4bce 100644 --- a/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.xml +++ b/target/classes/META-INF/maven/sf.codingcompetition2020/coding-competition/pom.xml @@ -27,6 +27,11 @@ jackson-dataformat-csv 2.11.2 + + com.google.code.gson + gson + 2.8.6 + diff --git a/target/classes/sf/codingcompetition2020/AgentReport$1.class b/target/classes/sf/codingcompetition2020/AgentReport$1.class new file mode 100644 index 0000000..97e9844 Binary files /dev/null and b/target/classes/sf/codingcompetition2020/AgentReport$1.class differ diff --git a/target/classes/sf/codingcompetition2020/AgentReport.class b/target/classes/sf/codingcompetition2020/AgentReport.class new file mode 100644 index 0000000..b68b88c Binary files /dev/null and b/target/classes/sf/codingcompetition2020/AgentReport.class differ diff --git a/target/classes/sf/codingcompetition2020/AreasScreen$1.class b/target/classes/sf/codingcompetition2020/AreasScreen$1.class new file mode 100644 index 0000000..e8cb5e4 Binary files /dev/null and b/target/classes/sf/codingcompetition2020/AreasScreen$1.class differ diff --git a/target/classes/sf/codingcompetition2020/AreasScreen.class b/target/classes/sf/codingcompetition2020/AreasScreen.class new file mode 100644 index 0000000..053d9ab Binary files /dev/null and b/target/classes/sf/codingcompetition2020/AreasScreen.class differ diff --git a/target/classes/sf/codingcompetition2020/Bar.class b/target/classes/sf/codingcompetition2020/Bar.class new file mode 100644 index 0000000..182a059 Binary files /dev/null and b/target/classes/sf/codingcompetition2020/Bar.class differ diff --git a/target/classes/sf/codingcompetition2020/BarGraph.class b/target/classes/sf/codingcompetition2020/BarGraph.class new file mode 100644 index 0000000..ac91d5a Binary files /dev/null and b/target/classes/sf/codingcompetition2020/BarGraph.class differ diff --git a/target/classes/sf/codingcompetition2020/CodingCompCsvUtil.class b/target/classes/sf/codingcompetition2020/CodingCompCsvUtil.class index 00daba9..1b5b826 100644 Binary files a/target/classes/sf/codingcompetition2020/CodingCompCsvUtil.class and b/target/classes/sf/codingcompetition2020/CodingCompCsvUtil.class differ diff --git a/target/classes/sf/codingcompetition2020/HomeScreen.class b/target/classes/sf/codingcompetition2020/HomeScreen.class new file mode 100644 index 0000000..55eb63b Binary files /dev/null and b/target/classes/sf/codingcompetition2020/HomeScreen.class differ diff --git a/target/classes/sf/codingcompetition2020/structures/Agent.class b/target/classes/sf/codingcompetition2020/structures/Agent.class index 26bf31f..0f1ac4e 100644 Binary files a/target/classes/sf/codingcompetition2020/structures/Agent.class and b/target/classes/sf/codingcompetition2020/structures/Agent.class differ diff --git a/target/classes/sf/codingcompetition2020/structures/Claim.class b/target/classes/sf/codingcompetition2020/structures/Claim.class index 1ce796d..9443063 100644 Binary files a/target/classes/sf/codingcompetition2020/structures/Claim.class and b/target/classes/sf/codingcompetition2020/structures/Claim.class differ diff --git a/target/classes/sf/codingcompetition2020/structures/Customer.class b/target/classes/sf/codingcompetition2020/structures/Customer.class index 844ea29..fa809ab 100644 Binary files a/target/classes/sf/codingcompetition2020/structures/Customer.class and b/target/classes/sf/codingcompetition2020/structures/Customer.class differ diff --git a/target/classes/sf/codingcompetition2020/structures/Dependent.class b/target/classes/sf/codingcompetition2020/structures/Dependent.class index 3ee505f..caefb85 100644 Binary files a/target/classes/sf/codingcompetition2020/structures/Dependent.class and b/target/classes/sf/codingcompetition2020/structures/Dependent.class differ diff --git a/target/classes/sf/codingcompetition2020/structures/Vendor.class b/target/classes/sf/codingcompetition2020/structures/Vendor.class index fdbca9b..44022e3 100644 Binary files a/target/classes/sf/codingcompetition2020/structures/Vendor.class and b/target/classes/sf/codingcompetition2020/structures/Vendor.class differ diff --git a/target/test-classes/sf/codingcompetition2020/CodingCompCsvUtilTest.class b/target/test-classes/sf/codingcompetition2020/CodingCompCsvUtilTest.class index 765ac60..2925b19 100644 Binary files a/target/test-classes/sf/codingcompetition2020/CodingCompCsvUtilTest.class and b/target/test-classes/sf/codingcompetition2020/CodingCompCsvUtilTest.class differ