For this lab you will implement a program that mimics data analysis for an e-com
ID: 3881436 • Letter: F
Question
For this lab you will implement a program that mimics data analysis for an e-commerce site. You will implement a set of classes (as determined by you) to represent the data stored in a "log" file. You will then work with the data to compute various statistics on the collected data.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Lab 3 collect data Orientation For this lab you will implement a program that mimics data analysis for an e-commerce site. You will implement a set of classes (as determined by you) to represent the data stored in a "log" file. You will then work with the data to compute various statistics on the collected data Objectives . Understand the provided base code that reads data from a file into an array of strings Write code to process the data into appropriate data structures (including writing your own classes and creating hashmaps to store lists of those classes as appropriate) Write code that operates on the data in order to compute statistics about that data (including relating the various peices of data to one another and computing averages, differences counting given specific instructions) Given Files Retrieve the files provided for this lab here: https://classroom.github.com/a/lehOUDGO These given files include base code to help in the task of analyzing log data. Your final program will need to read in e-commerce like data from a file and compute statistics based on the information in the log. The base code implements the file reading (but not the processing of the data read). You will need to complete the processing of the data for this lab, (including putting the data into appropriate data structures, i.e., not an array of strings). You are encouraged to read through the provided code Log File Examine the provided log files. For example, look at the contents of small.log. You will find various entries representing customer use of an imaginary e-commerce site. Each entry will appear on a single line in the file and consist of an entry type tag followed by the corresponding entry attributes The different types of entries and their attributes are as follows. START sessionId customer!d VIEW sessionId productid price BUY sessionId productId price quantity END sessionId 1 of 4 1/31/2018, 12:25 PMExplanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class LogAnalyzer {
private static final String START_TAG = "START";
private static final int START_NUM_FIELDS = 3;
private static final int START_SESSION_ID = 1;
private static final int START_CUSTOMER_ID = 2;
private static final String BUY_TAG = "BUY";
private static final int BUY_NUM_FIELDS = 5;
private static final int BUY_SESSION_ID = 1;
private static final int BUY_PRODUCT_ID = 2;
private static final int BUY_PRICE = 3;
private static final int BUY_QUANTITY = 4;
private static final String VIEW_TAG = "VIEW";
private static final int VIEW_NUM_FIELDS = 4;
private static final int VIEW_SESSION_ID = 1;
private static final int VIEW_PRODUCT_ID = 2;
private static final int VIEW_PRICE = 3;
private static final String END_TAG = "END";
private static final int END_NUM_FIELDS = 2;
private static final int END_SESSION_ID = 1;
public static void main(String[] args)
{
final Map<String, List<String>> sessionsFromCustomer = new HashMap<>();
Map<String, List<View>> viewsFromSessions = new HashMap<>();
Map<String, List<Buy>> buysFromSessions = new HashMap<>();
final String filename = getFilename(args);
try
{
populateDataStructures(filename, sessionsFromCustomer,
viewsFromSessions,
buysFromSessions
);
printStatistics(
sessionsFromCustomer,
viewsFromSessions,
buysFromSessions
);
}
catch (FileNotFoundException e)
{
System.err.println(e.getMessage());
}
//printOutExample(sessionsFromCustomer, viewsFromSessions, buysFromSessions);
}
private static void populateDataStructures(
final String filename,
final Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsFromSessions,
Map<String, List<Buy>> buysFromSessions
)
throws FileNotFoundException
{
try (Scanner input = new Scanner(new File(filename)))
{
processFile(input, sessionsFromCustomer,
viewsFromSessions, buysFromSessions
);
}
}
private static void processFile(
final Scanner input,
final Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsFromSessions,
Map<String, List<Buy>> buysFromSessions
)
{
while (input.hasNextLine())
{
processLine(input.nextLine(), sessionsFromCustomer,
viewsFromSessions, buysFromSessions
);
}
}
private static void processLine(
final String line,
final Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsForSessions,
Map<String, List<Buy>> buysForSessions
) {
final String[] words = line.split("\h");
if (words.length == 0) {
return;
}
switch (words[0]) {
case START_TAG:
processStartEntry(words, sessionsFromCustomer);
break;
case VIEW_TAG:
processViewEntry(words, viewsForSessions );
break;
case BUY_TAG:
processBuyEntry(words, buysForSessions);
break;
case END_TAG:
processEndEntry(words);
break;
}
}
private static void processStartEntry(
final String[] words,
final Map<String, List<String>> sessionsFromCustomer) {
if (words.length != START_NUM_FIELDS) {
return;
}
List<String> sessions = sessionsFromCustomer.get(words[START_CUSTOMER_ID]);
if (sessions == null) {
sessions = new LinkedList<>();
sessionsFromCustomer.put(words[START_CUSTOMER_ID], sessions);
}
//now that we know there is a list, add the current session
sessions.add(words[START_SESSION_ID]);
}
private static void processViewEntry(final String[] words,
Map<String, List<View>> viewsFromSessions
) {
if (words.length != VIEW_NUM_FIELDS) {
return;
}
String sessionId = words[VIEW_SESSION_ID];
viewsFromSessions.putIfAbsent(sessionId, new LinkedList<>());
String productId = words[VIEW_PRODUCT_ID];
int productPrice = parseInt(words[VIEW_PRICE]);
viewsFromSessions.get(sessionId).add(new View(productId, productPrice));
}
private static void processBuyEntry(
final String[] words,
Map<String, List<Buy>> buysFromSessions
) {
if (words.length != BUY_NUM_FIELDS) {
return;
}
String sessionId = words[BUY_SESSION_ID];
buysFromSessions.putIfAbsent(sessionId, new LinkedList<>());
String productId = words[BUY_PRODUCT_ID];
int productPrice = Integer.parseInt(words[BUY_PRICE]);
int productQuantity = Integer.parseInt(words[BUY_QUANTITY]);
buysFromSessions.get(sessionId).add(new Buy(productId, productPrice, productQuantity));
}
private static void processEndEntry(final String[] words) {
if (words.length != END_NUM_FIELDS) {
return;
}
}
private static void printAverageViewsWithoutPurchase(
Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsFromSessions,
Map<String, List<Buy>> buysFromSessions) {
List<String> sessionNoPurchaseList = new LinkedList<>();
List<String> sessionPurchaseList = getSessionsWithPurchases(sessionsFromCustomer,
buysFromSessions);
for (List<String> sessionList: sessionsFromCustomer.values()) {
for (String session: sessionList) {
if (!sessionPurchaseList.contains(session)) {
sessionNoPurchaseList.add(session);
}
}
}
int totViews = getTotViews(sessionNoPurchaseList, viewsFromSessions);
System.out.printf("Average Views without Purchase: %.1f ",
totViews / (double) sessionNoPurchaseList.size());
}
private static int getTotViews(List<String> sessionList, Map<String, List<View>> viewMap) {
int views = 0;
for (String sessionId: sessionList) {
List<View> currentViewSession = viewMap.get(sessionId);
if (currentViewSession != null) {
views += viewMap.get(sessionId).size();
}
}
return views;
}
private static void printSessionPriceDifference(
Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsFromSessions,
Map<String, List<Buy>> buysFromSessions
) {
System.out.println("Price Difference for Purchased Product by Session");
List<String> sessionsWithPurchases = getSessionsWithPurchases(
sessionsFromCustomer, buysFromSessions);
Map<String, Double> avgPriceViewedSessions = findAveragePriceViewedAllSessions(
sessionsWithPurchases, viewsFromSessions
);
for (String session : sessionsWithPurchases) {
System.out.println(session);
for (Buy purchase : buysFromSessions.get(session)) {
System.out.printf(" %s %.1f ", purchase.getProduct(),
purchase.getPrice() - avgPriceViewedSessions.get(session));
}
}
}
private static Map<String, Double> findAveragePriceViewedAllSessions(
List<String> sessionsWithPurchases, Map<String, List<View>> viewsFromSessions
) {
Map<String, Double> averageMap = new HashMap<>();
for (String session : sessionsWithPurchases) {
int viewPriceTot = 0;
int viewedItems = 0;
for (View viewItem : viewsFromSessions.get(session)) {
viewPriceTot += viewItem.getPrice();
viewedItems += 1;
}
averageMap.put(session, ((double) viewPriceTot) / viewedItems);
}
return averageMap;
}
private static void printCustomerItemViewsForPurchase(
Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsFromSessions,
Map<String, List<Buy>> buysFromSessions
)
{
System.out.println("Number of Views for Purchased Product by Customer");
List<String> sessionPurchaseMadeList = getSessionsWithPurchases(
sessionsFromCustomer, buysFromSessions);
Map<String, List<String>> customerMapSessionPurchased = mapCustomerProductsPurchased(
sessionsFromCustomer, sessionPurchaseMadeList);
for (Map.Entry<String, List<String>> entry: customerMapSessionPurchased.entrySet()) {
List<String> productList1 = getItemsPurchasedByCustomer(entry.getValue(), buysFromSessions);
if (productList1.size() >= 1) {
System.out.println(entry.getKey());
List<String> productList = getItemsPurchasedByCustomer(entry.getValue(), buysFromSessions);
for (String product: productList) {
int totTimesViewed = 0;
for (Map.Entry<String, List<View>> entryView: viewsFromSessions.entrySet()) {
boolean hasProduct = false;
if (sessionsFromCustomer.get(entry.getKey()).contains(entryView.getKey())) {
for (View viewItem: entryView.getValue()) {
if (viewItem.getProduct().equals(product)) {
hasProduct = true;
break;
}
}
}
if (hasProduct) {
totTimesViewed += 1;
}
}
System.out.printf(" %s %d ", product, totTimesViewed);
}
}
}
}
private static List<String> getItemsPurchasedByCustomer(
List<String> sessionListForCustomer,
Map<String, List<Buy>> buysFromSessions
){
List<String> listProduct = new LinkedList<>();
for (String session: sessionListForCustomer) {
for (Buy purchase: buysFromSessions.get(session)) {
if (!listProduct.contains(purchase)) {
listProduct.add(purchase.getProduct());
}
}
}
return listProduct;
}
private static Map<String, List<String>> mapCustomerProductsPurchased(
Map<String, List<String>> sessionsFromCustomers,
List<String> sessionsWithPurchases
){
Map<String, List<String>> newCustomerMap = new HashMap<>();
for (String customer: sessionsFromCustomers.keySet()) {
newCustomerMap.put(customer, new LinkedList<>());
}
for (Map.Entry<String, List<String>> customer: sessionsFromCustomers.entrySet()) {
for (String session: sessionsWithPurchases) {
if (customer.getValue().contains(session)) {
newCustomerMap.get(customer.getKey()).add(session);
}
}
}
return newCustomerMap;
}
private static List<String> getSessionsWithPurchases(
Map<String, List<String>> sessionsFromCustomer,
Map<String, List<Buy>> buysFromSessions
)
{
List<String> sessionList = new LinkedList<>();
for (Map.Entry<String, List<String>> entry: sessionsFromCustomer.entrySet()) {
for (String sessionId: entry.getValue()) {
if (buysFromSessions.containsKey(sessionId)) {
sessionList.add(sessionId);
}
}
}
return sessionList;
}
private static void printStatistics(
Map<String, List<String>> sessionsFromCustomer,
Map<String, List<View>> viewsFromSessions,
Map<String, List<Buy>> buysFromSessions
)
{
printAverageViewsWithoutPurchase(sessionsFromCustomer,
viewsFromSessions,
buysFromSessions);
printSessionPriceDifference(sessionsFromCustomer,
viewsFromSessions,
buysFromSessions);
printCustomerItemViewsForPurchase(sessionsFromCustomer,
viewsFromSessions,
buysFromSessions);
// printOutExample(sessionsFromCustomer, viewsFromSession, buysFromSession);
}
private static void printOutExample(
final Map<String, List<String>> sessionsFromCustomer,
final Map<String, List<View>> viewsFromSession,
final Map<String, List<Buy>> buysFromSession)
{
for(Map.Entry<String, List<String>> entry:
sessionsFromCustomer.entrySet())
{
System.out.println(entry.getKey());
List<String> sessions = entry.getValue();
for(String sessionID : sessions)
{
System.out.println(" in " + sessionID);
List<View> theViews = viewsFromSession.get(sessionID);
if (theViews != null) {
for (View thisView: theViews)
{
System.out.println(" viewed " + thisView.getProduct());
}
}
}
}
}
private static String getFilename(String[] args)
{
if (args.length < 1)
{
System.err.println("Log file not specified.");
System.exit(1);
}
return args[0];
}
}
------------------------------------------------------------------------------------------
public class Buy {
private String productId;
private int productPrice;
private int productQuantity;
public Buy(String product, int price, int quantity) {
productId = product;
productPrice = price;
productQuantity = quantity;
}
@Override
public String toString() {
return String.format("Buy: %s " +
"Price: %d " +
"Quantity: %d ", productId, productPrice, productQuantity);
}
public String getProduct() {
return productId;
}
public int getPrice() {
return productPrice;
}
}
-----------------------------------------------------------------------------------------------------
public class View {
private String productId;
private int productPrice;
public View(String product, int price) {
productId = product;
productPrice = price;
}
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (!getClass().equals(other.getClass())) {
return false;
}
return (productId == ((View) other).productId &&
productPrice == ((View) other).productPrice);
}
public String getProduct() {
return productId;
}
public int getPrice() {
return productPrice;
}
public String toString() {
return String.format("ViewProduct: %s, ViewPrice: %d ",
productId, productPrice);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.