-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannel.java
121 lines (78 loc) · 2.43 KB
/
Channel.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
/*
Remember while initializing the catalog file
make sure that you write channel name and price
with comma in between and no spaces between comma
other wise code will break
e.g :- Indore tv,20
@Rishav
*/
import java.io.*;
import java.util.*;
public class Channel{
ArrayList<String> ch = new ArrayList<String>();
ArrayList<Integer> price = new ArrayList<Integer>();
public Channel(){
try{
File file = new File("channel.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line=br.readLine())!=null){
String words[] = line.split(",");
ch.add(words[0]);
price.add(Integer.parseInt(words[1]));
}
br.close();
}catch(Exception ex){
ex.printStackTrace();
System.out.println("something don't look right");
}
}
public void catalogue(){
int index = 0;
Iterator ch_itr = ch.iterator();
Iterator pr_itr = price.iterator();
System.out.printf("\n%-6s %-80s %20s\n","index","Channel Name", "Price");
while(ch_itr.hasNext())
System.out.printf("\n%-6d %-80s %20d",index,ch_itr.next(),pr_itr.next(),++index);
}
/* public void user_subscription(int[] sub){
int index = 1;
for(int i = 0;i<sub.length;++i){
System.out.printf("\n%-6d %-80s %20d",index,ch.get(sub[i]),price.get(sub[i]),++index);
}
} */
public void user_subscription(HashSet<Integer> hs){
Iterator it = hs.iterator();
int index = 1;
System.out.printf("\n%-6s %-80s %20s %20s\n","index","Channel Name","Price","Hash");
while(it.hasNext()){
int temp = (Integer) it.next();
System.out.printf("\n%-6d %-80s %20d %20d",index,ch.get(temp),price.get(temp),temp,++index);
}
}
public int getTotalBalance(HashSet<Integer> hs){
int sum = 0;
Iterator it = hs.iterator();
/* for(int i = 0; i < sub.length; ++i)
sum = sum + price.get(sub[i]); */
while(it.hasNext()){
int temp = (Integer) it.next();
sum = sum + price.get(temp);
}
return sum;
}
public int getTotalBalance(ArrayList<Integer> hs){
int sum = 0;
Iterator it = hs.iterator();
/* for(int i = 0; i < sub.length; ++i)
sum = sum + price.get(sub[i]); */
while(it.hasNext()){
int temp = (Integer) it.next();
sum = sum + price.get(temp);
}
return sum;
}
public int getSize(){
return ch.size();
}
}