package problem;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.Timestamp;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import d1Assignment.Helper;
public class WebStat {
private static int actionId = 100;
private static String browser;
private static double ConnectionSpeed;
private String[] webinfo;
private static ArrayList<SiteEntry> siteEntrydetails = new ArrayList<SiteEntry>();
private static ArrayList<SiteExit> siteExitdetails = new ArrayList<SiteExit>();
private static ArrayList<PageHop> pageHopdetails = new ArrayList<PageHop>();
public static void main(String[] args) {
WebStat wl = new WebStat();
wl.start();
}
private void start() {
int option = -1;
while (option != 6) {
updateArrayList();
menu();
option = Helper.readInt("Enter choice > ");
if (option == 1) {
// AddNewSiteEntry();
} else if (option == 2) {
// AddNewSiteExit();
}else if (option == 3) {
// AddPageHop();
} else if (option == 4) {
// ViewBrowseActions();
}
else if (option == 6) {
System.out.println("Thank you for using our Service!");
}
}
}
private void menu() {
Helper.line(60, "=");
System.out.println("WELCOME TO MY WEB STAT PORTAL");
Helper.line(60, "=");
System.out.println("1. Add New Site Entry");
System.out.println("2. Add New Site Exit");
System.out.println("3. Add New Page Hop");
System.out.println("4. List Browse Actions by Date Range");
System.out.println("5. Export with Common Statistics Format");
System.out.println("6. Quit");
}
private void updateArrayList() {
siteEntrydetails.clear();
siteExitdetails.clear();
pageHopdetails.clear();
try {
BufferedReader br = new BufferedReader(new FileReader(new File("visitdetails.txt"))); //read file from items.txt
String line = br.readLine();
while(line != null){
browser = "";
ConnectionSpeed = 0.00;
webinfo = line.split(",");
webinfo[0] = webinfo[0].trim();
actionId = Integer.parseInt(webinfo[0]);
webinfo[1] = webinfo[1].trim();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH:mm");
LocalDateTime dateTime = LocalDateTime.parse(webinfo[1], formatter);
webinfo[6] = webinfo[6].trim();
double d = Double.parseDouble(webinfo[6]);
BrowseInfo browserInfo = new BrowseInfo (webinfo[5],d);
//System.out.println(String.format("%-10d, %-10s, %-10s %-10s %-10s", actionId,dateTime,webinfo[2],browserInfo.getBrowser(),browserInfo.getconnectionSpeed()));
if(webinfo[2].trim().equalsIgnoreCase("entry")) {//save entries with entry in visitdetails.txt into ArrayList
SiteEntry SiteEntryObj = new SiteEntry(actionId,dateTime,webinfo[3],webinfo[4],browserInfo);
siteEntrydetails.add(SiteEntryObj);
browser = "";
ConnectionSpeed = 0.00;
}
if(webinfo[2].trim().equalsIgnoreCase("exit")) {//save entries with exit in visitdetails.txt into ArrayList
SiteExit SiteExitObj = new SiteExit(actionId,dateTime,webinfo[3],webinfo[4],browserInfo);
siteExitdetails.add(SiteExitObj);
browser = "";
ConnectionSpeed = 0.00;
}
if(webinfo[2].trim().equalsIgnoreCase("hop")) {//save entries with hop in visitdetails.txt into ArrayList
PageHop PageHopObj = new PageHop(actionId,dateTime,webinfo[3],webinfo[4],webinfo[5],d);
pageHopdetails.add(PageHopObj);
browser = "";
ConnectionSpeed = 0.00;
}
line = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
for(SiteEntry e: siteEntrydetails) {
System.out.println(e.getBrowserInfo().getBrowser() ); //problem lies here
}
}
}
package problem;
import java.time.LocalDateTime;
public abstract class BrowseAction {
private int actionId;
private LocalDateTime actionDateTime;
private String page;
private static BrowseInfo browserInfo;
public BrowseAction(int actionId,LocalDateTime actionDateTime, String page, BrowseInfo browserInfo){
this.actionId = actionId;
this.actionDateTime = actionDateTime;
this.page = page;
this.browserInfo = browserInfo;
}
public int getActionId() {
return actionId;
}
public LocalDateTime getactionDateTime() {
return actionDateTime;
}
public void setactionDateTime(LocalDateTime a) {
actionDateTime = a;
}
public String getPage() {
return page;
}
public static BrowseInfo getBrowserInfo() {
return browserInfo;
}
public void display() {
}
}
package problem;
public class BrowseInfo {
private static String browser;
private static double connectionSpeed;
public BrowseInfo(String browser, double connectionSpeed) {
this.browser = browser;
this.connectionSpeed = connectionSpeed;
}
public static String getBrowser() {
return browser;
}
public static double getconnectionSpeed() {
return connectionSpeed;
}
}
package problem;
public interface CommonStat {
public String formatStat();
}
package problem;
import java.util.*;
public class Helper {
@SuppressWarnings("resource")
public static String readString(String prompt) {
System.out.print(prompt);
return new java.util.Scanner(System.in).nextLine();
}
public static int readInt(String prompt) {
int input = 0;
boolean valid = false;
while (!valid) {
try {
input = Integer.parseInt(readString(prompt));
valid = true;
} catch (NumberFormatException e) {
System.out.println("*** Please enter an integer ***");
}
}
return input;
}
public static double readDouble(String prompt) {
double input = 0;
boolean valid = false;
while (!valid) {
try {
input = Double.parseDouble(readString(prompt));
valid = true;
} catch (NumberFormatException e) {
System.out.println("*** Please enter a double ***");
}
}
return input;
}
public static float readFloat(String prompt) {
float input = 0;
boolean valid = false;
while (!valid) {
try {
input = Float.parseFloat(readString(prompt));
valid = true;
} catch (NumberFormatException e) {
System.out.println("*** Please enter a float ***");
}
}
return input;
}
public static long readLong(String prompt) {
long input = 0;
boolean valid = false;
while (!valid) {
try {
input = Long.parseLong(readString(prompt));
valid = true;
} catch (NumberFormatException e) {
e.printStackTrace();
System.out.println("*** Please enter a long ***");
}
}
return input;
}
public static char readChar(String prompt) {
char input = 0;
boolean valid = false;
while (!valid) {
String temp = readString(prompt);
if (temp.length() != 1) {
System.out.println("*** Please enter a character ***");
} else {
input = temp.charAt(0);
valid = true;
}
}
return input;
}
public static boolean readBoolean(String prompt) {
boolean valid = false;
while (!valid) {
String input = readString(prompt);
if (input.equalsIgnoreCase("yes") || input.equalsIgnoreCase("y")
|| input.equalsIgnoreCase("true") || input.equalsIgnoreCase("t")) {
return true;
} else if (input.equalsIgnoreCase("no") || input.equalsIgnoreCase("n")
|| input.equalsIgnoreCase("false") || input.equalsIgnoreCase("f")) {
return false;
} else {
System.out.println("*** Please enter Yes/No or True/False ***");
}
}
return false;
}
public static Date readDate(String prompt) {
java.util.Date date = null;
boolean valid = false;
while (!valid) {
try {
String input = readString(prompt).trim();
if (input.matches("\\d\\d/\\d\\d/\\d\\d\\d\\d")) {
int day = Integer.parseInt(input.substring(0, 2));
int month = Integer.parseInt(input.substring(3, 5));
int year = Integer.parseInt(input.substring(6, 10));
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setLenient(false);
cal.set(year, month - 1, day, 0, 0, 0);
date = cal.getTime();
valid = true;
} else {
System.out.println("*** Please enter a date (DD/MM/YYYY) ***");
}
} catch (IllegalArgumentException e) {
System.out.println("*** Please enter a date (DD/MM/YYYY) ***");
}
}
return date;
}
private static String quit = "0";
public static int getUserOption(String title, String[] menu) {
displayMenu(title, menu);
int choice = readInt("Enter Choice --> ");
while (choice > menu.length || choice < 0) {
choice = readInt("Invalid Choice, Re-enter --> ");
}
return choice;
}
private static void displayMenu(String title, String[] menu) {
line(80, "=");
System.out.println(title.toUpperCase());
line(80, "-");
for (int i = 0; i < menu.length; i++) {
System.out.println("[" + (i + 1) + "] " + menu[i]);
}
System.out.println("[" + quit + "] Quit");
line(80, "-");
}
public static void line(int len, String c) {
System.out.println(String.format("%" + len + "s", " ").replaceAll(" ", c));
}
public static Date thisDate(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.set(year, month - 1, day, 0, 0, 0);
return cal.getTime();
}
public static Date thisDate(int year, int month, int day, int hour, int min) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.set(year, month - 1, day, hour, min, 0);
return cal.getTime();
}
public static Date today() {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(new Date(System.currentTimeMillis()));
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}
public static Date addDays(Date date, long days) {
long time = date.getTime() + (days * 24 * 60 * 60 * 1000);
return new Date(time);
}
public static Date setHourMinute(Date date, int hour, int min) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}
public static int getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
public static int getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
}
public static int getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(date);
return cal.get(Calendar.DAY_OF_MONTH);
}
public static Date now() {
return new Date(System.currentTimeMillis());
}
public static int calcDayDifference(Date former, Date latter) {
long diff = latter.getTime() - former.getTime();
int days = (int) (diff / (24 * 60 * 60 * 1000));
return days;
}
public static int calcYearDifference(Date former, Date latter) {
int years = getYear(latter) - getYear(former);
if (years > 0) {
if ((getMonth(former) > getMonth(latter)) ||
(getMonth(former) == getMonth(latter) && getDay(former) > getDay(latter))) {
years -= 1;
}
} else if (years < 0) {
if ((getMonth(former) < getMonth(latter)) ||
(getMonth(former) == getMonth(latter) && getDay(former) < getDay(latter))) {
years += 1;
}
}
return years;
}
public static boolean sameDate(Date one, Date two) {
return getDay(one) == getDay(two) && getMonth(one) == getMonth(two)
&& getYear(one) == getYear(two);
}
}
package problem;
import java.time.LocalDateTime;
public class PageHop extends BrowseAction implements CommonStat{
private String fromPage;
private String output;
//private BrowseInfo browserInfo;
public PageHop ( int actionId,LocalDateTime actionDateTime,String fromPage, String page, String browser, double connectionspeed ){
super(actionId,actionDateTime,page, new BrowseInfo(browser,connectionspeed));
this.fromPage = fromPage;
}
public String getFromPage(){
return fromPage;
}
//public BrowseInfo getBrowserInfo() {
// return browserInfo;
//}
public void display() {
System.out.println(String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s ,%-10s ,%-10.2f ",getActionId(),getactionDateTime() ,"hop",getFromPage(), getPage(),getBrowserInfo().getBrowser(), getBrowserInfo().getconnectionSpeed()));
}
public String formatStat(){
output = String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s ,%-10s ,%-10.2f \n",getActionId(),getactionDateTime() ,"hop",getFromPage(), getPage(),getBrowserInfo().getBrowser(), getBrowserInfo().getconnectionSpeed());
return output;
}
}
package problem;
import java.time.LocalDateTime;
public class SiteEntry extends BrowseAction implements CommonStat{
private String fromExternalSite;
private String output;
public SiteEntry(int actionId,LocalDateTime actionDateTime,String fromExternalSite, String page, BrowseInfo browserInfo ) {
super(actionId,actionDateTime,page, browserInfo); //this super statement must be above the this...statement
this.fromExternalSite = fromExternalSite;
}
public String getFromExternalSite() {
return fromExternalSite;
}
public void display() {
System.out.println(String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s ,%-10s ,%-10.2f",super.getActionId(),super.getactionDateTime() ,"entry",fromExternalSite, super.getPage(),getBrowserInfo().getBrowser(),getBrowserInfo().getconnectionSpeed()));
//System.out.println(String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s" ,super.getActionId(),super.getactionDateTime() ,"entry",fromExternalSite, super.getPage()));
}
public String formatStat(){
output = String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s ,%-10s ,%-10.2f \n",super.getActionId(),super.getactionDateTime() ,"entry",fromExternalSite, super.getPage(),BrowseInfo.getBrowser(),BrowseInfo.getconnectionSpeed());
return output;
}
}
package problem;
import java.time.LocalDateTime;
public class SiteExit extends BrowseAction{
private String toExternalSite;
private String output;
public SiteExit(int actionId,LocalDateTime actionDateTime, String page,String toExternalSite, BrowseInfo browserInfo) {
super(actionId,actionDateTime,page, browserInfo); //this super statement must be above the this...statement
this.toExternalSite = toExternalSite;
}
public String getToExternalSite() {
return toExternalSite;
}
public void display() {
System.out.println(String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s ,%-10s ,%-10.2f ",getActionId(),getactionDateTime() ,"exit", getPage(),getToExternalSite(),BrowseInfo.getBrowser(), BrowseInfo.getconnectionSpeed()));
}
public String formatStat(){
output = String.format("%-10d ,%-20s ,%-10s ,%-30s ,%-30s ,%-10s ,%-10.2f ",getActionId(),getactionDateTime() ,"exit", getPage(),getToExternalSite(),getBrowserInfo().getBrowser(), getBrowserInfo().getconnectionSpeed());
return output;
}
}