-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataAnalysis.java
157 lines (121 loc) · 4.46 KB
/
DataAnalysis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Functions
// public static void main(String args)throws IOException,ParseException
// entry point for the program ,returns 0 on normal termination
// static String getMaxWeekDay(int n)
// returns a string for the day
// for eg for n=1 it returns "Sunday"
// for n=7 it returns "Saturday"
// Class
// public class DataAnalysis
// It is the class containig main function and is the only class in the program
// It uses a hashtable to keep a count of visitors on a givrn date and day
// It is then used to find the different stats as mentiond in the requirements
// Data Requirements
// formatter type:Dateformat used for formatting
// idate type:Date used for date
// maxweekday type:int keeps the day on which maximum visitors visited
// days type:int[] keeps the count of no of vistors for 7 days
// imaxday and iminday type:Date reference for day on which maximum and minimum visitors visted the zoo
// few self-explanatory variables/references.....
import java.io.*;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Date;
import java.util.Set;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DataAnalysis
{
/**
* @author Prakhar Sharma
* Last modified by Prakhar Sharma
* @param args
* @throws IOException
* @throws ParseException
*/
public static void main(String[] args) throws IOException, ParseException
{
DateFormat formatter = null;
Date idate= null;
Hashtable<Date,Integer> visitors= new Hashtable<Date,Integer>();
String s;
File f = new File("input.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while((s=br.readLine())!=null)
{
String[] details = s.split("\\s");
if(details[0].equals(""))break;
formatter = new SimpleDateFormat("yyyy/MM/dd");
details[0] = "20" + details[0];
// 13/10/14--->> 2013/10/14
idate=(Date)formatter.parse(details[0]);
// Update Hashtable with no. of visitors
if(visitors.containsKey(idate))
visitors.put(idate, visitors.get(idate)+Integer.parseInt(details[2]));
else
visitors.put(idate,Integer.parseInt(details[2]));
}
br.close();
Calendar c=Calendar.getInstance();
Set<Date> keys = visitors.keySet();
Date imaxday=null,iminday=null;
int imaxvisitor=-1,iminvisitor=1000000000;
int days[]=new int[8];
for(Date key:keys)
{
if(visitors.get(key)>imaxvisitor)
{
imaxvisitor=visitors.get(key);
imaxday=key;
}
if(visitors.get(key)<iminvisitor)
{
iminvisitor=visitors.get(key);
iminday=key;
}
c.setTime(key);
days[c.get(Calendar.DAY_OF_WEEK)]+=visitors.get(key);
}
File fout=new File("Output.txt");
fout.createNewFile();
FileWriter fw = new FileWriter(fout);
PrintWriter pw = new PrintWriter(fw);
DateFormat df=DateFormat.getDateInstance(DateFormat.FULL);
pw.println("Date and Day on which maximum no. of visitors visited :"+df.format(imaxday));
pw.println("Date and Day on which minimum no. of visitors visited :"+df.format(iminday));
int maxweekday=-1,uptillmax=-1;
int weekendvisitors=0,totalvisitors=0;
for(int i=1;i<=7;i++)
{
if(days[i]>uptillmax)
{
uptillmax=days[i];
maxweekday=i;
}
if(i==1||i==7)
weekendvisitors+=days[i];
totalvisitors+=days[i];
}
s=getMaxWeekDay(maxweekday);
pw.println("Day on which most no. of visitors visit on average : "+s+"day");
pw.println("Percentage of weekend visitors over total visitors : "+((double)weekendvisitors/totalvisitors)*100.00+"%");
pw.close();
}
static String getMaxWeekDay(int n)
{
String s="";
switch(n)
{
case 1:s="Sun";break;
case 2:s="Mon";break;
case 3:s="Tues";break;
case 4:s="Wednes";break;
case 5:s="Thurs";break;
case 6:s="Fri";break;
case 7:s="Satur";break;
}
return s;
}
}