-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsports.java
191 lines (156 loc) · 4.18 KB
/
sports.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package begin;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class sports extends student{
private static final long serialVersionUID = 1L;
static Scanner sc=new Scanner(System.in);
public int vote_for(student ob) throws IOException, ClassNotFoundException //function that will check whether candidate has voted or not.
{
student s=null;
FileInputStream is = new FileInputStream("C:\\Users\\Dell\\Desktop\\project\\g_sports_vote.txt"); // candidates who have given their vote in general cultural elections.
while(is.available()>0)
{
ObjectInputStream ois = new ObjectInputStream(is);
s = (student) ois.readObject();
if(ob.ID==s.ID)
{
System.out.println("you have already voted.");
return 1;
}
}
is.close();
//candidate has not voted hence he will be allowed to vote
try
{
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\Dell\\Desktop\\project\\g_sports_vote.txt"),true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
int k=0;
while(true)
{
System.out.println("1. vote");
System.out.println("2. go back");
int q=sc.nextInt();
switch(q)
{
case 1: try
{
vote(); //function that will allow the candidate to vote
k=1; //if vote is successfull
oos.writeObject(ob);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
break;
case 2: k=1;
break;
default: System.out.println("wrong choice...");
}
if(k==1)
{
break;
}
}
oos.close();
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
// sc.close();
}
return 0;
}
public static void vote() throws IOException, ClassNotFoundException
{
FileInputStream is = new FileInputStream("C:\\Users\\Dell\\Desktop\\project\\s_selected.txt"); //going in c_selected to display the data of registered candidates
int count=0;
student so=null;
while(is.available()>0)
{
ObjectInputStream ois1 = new ObjectInputStream(is); //reading the file to know number of registered candidates
so= (student) ois1.readObject();
count++;
}
is.close(); //
student s[]=new student[count]; //storing the student object data in an array
FileInputStream is1 = new FileInputStream("C:\\Users\\Dell\\Desktop\\project\\s_selected.txt");
if(is1.available()==0)
{
System.out.println("no registered candidates yet.");
return;
}
int i=0;
int input;
while(is1.available()>0)
{
ObjectInputStream ois = new ObjectInputStream(is1);// printing data
s [i]= (student) ois.readObject();
System.out.print("Candidate Number :"+ (i+1));
System.out.print(" name : "+s[i].name);
System.out.print(" id : "+s[i].ID);
System.out.print(" batch : "+s[i].batch);
System.out.print(" branch : "+s[i].branch);
System.out.println();
i++;
}
is1.close();
int k=0;
while(true)
{
System.out.println("enter the candidate ID to vote for : ");
input=sc.nextInt();
for(int j=0;j<s.length;j++)
{
if(s[j].ID==input)
{
// s[j].c_comittee+=1; // c_comittee is for counting votes for selected candidates
k=1;
break;
}
}
if(k==1)
{
break;
}
else
{
System.out.println("you entered wrong ID please retry");
}
}
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\Dell\\Desktop\\project\\stats_gen_sports.txt", true) //Set true for append mode
);
//Add new line
String h=Integer.toString(input);
writer.write(h);
writer.newLine();
writer.close();
System.out.println("Done");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
// sc.close();
}
}
}