Hello, this is my first time using chegg to post a question so in advance thanks
ID: 3743708 • Letter: H
Question
Hello, this is my first time using chegg to post a question so in advance thanks for all help and I apologize if I seem clueless. my question is if anyone knows how to:
create a java program to
declare a properly-sized array of SalesPerson
read in all the information from both files and populate the array
find the lowest and highest sales person.
print a report to the console or a file.
using the following sales people/id numbers
with the following sales applied to the id numbers
again thank you so much for any help and have a great day.
Explanation / Answer
--------------------------------------------------Sales.java------------------------------------------------------------------
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Sales {
public static void main(String[] args)throws Exception {
//Reading data from the text file "Sales.txt"
Scanner sc_sales=new Scanner(new File("Sales.txt"));
//to save data of "Sales.txt" file
ArrayList<String> list_Sales=new ArrayList<>();
while(sc_sales.hasNext()) {
list_Sales.add(sc_sales.nextLine());
}
//In Hash Map String for id of sales person and ArrayList to save sales of that person
HashMap<String, ArrayList<Integer>> salesMap=new HashMap<>();
for(int i=0;i<list_Sales.size();i++)
{
String value=list_Sales.get(i);
String[] values=value.split(" ");
String key=values[0];
int sales[]= new int[values.length-1];
for(int j=1;j<values.length;j++)
{
sales[j-1]=Integer.parseInt(values[j]);;
}
for(int sale :sales)
{
if(salesMap.containsKey(key))
{
salesMap.get(key).add(sale);
}
else
{
ArrayList<Integer> arr=new ArrayList<>();
arr.add(sale);
salesMap.put(key, arr);
}
}
}
//for calculating the sales of each sales person
HashMap<String,Integer> totalSales=new HashMap<>();
Iterator<String> keySetIterator = salesMap.keySet().iterator();
while(keySetIterator.hasNext())
{
String key = keySetIterator.next();
ArrayList<Integer> person_Sales=salesMap.get(key);
int total_Sales=0;
for(int sales:person_Sales)
{
total_Sales+=sales;
}
totalSales.put(key, total_Sales);
//System.out.println(key + " : "+totalSales.get(key));// printing total sales of an id
}
//Sorting the data according to total sales values
Set<Entry<String, Integer>> set = totalSales.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
//Reading data from the text file "SalesPerson.txt"
Scanner sc_person=new Scanner(new File("SalesPerson.txt"));
//to save data of "SalesPerson.txt" file
ArrayList<String> list_Person=new ArrayList<>();
while(sc_person.hasNext()) {
list_Person.add(sc_person.nextLine());
}
//using HashMap for saving data based on the id of sales Person
HashMap<String,String> person=new HashMap<>();
for(String li:list_Person)
{
String []data=li.split(" ");
String key=data[0];
String value=data[1];
person.put(key, value);
//for printing highest sales person
String []highest=list.get(0).toString().split("=");
if(highest[0].equals(key)){ //comparing id to get the name of person
System.out.print("Highest sales person : ");
System.out.println(value+ " with sales "+highest[1] );
}
//for printing lowest sales person
String []lowest=list.get(list.size()-1).toString().split("=");
if(lowest[0].equals(key)){ //comparing id to get the name of person
System.out.print("Lowest sales person : ");
System.out.println(value+ " with sales "+lowest[1] );
}
}
}
}
-------------------------------------------------Output-------------------------------------------------------
Highest sales person : Paulita Trevarthen with sales 89518
Lowest sales person : Nikia Fugitt with sales 3307
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.