C and C++ Programming Resources

Share/Bookmark
Custom Search

Download Upload Statistics Manager

Posted on September 9th, 2008

This program is for those who have a fixed limit for downloading/uploading. It creates and saves the statistics of the numerical value passed by the user.
Before use the user needs to create 2 text files every month. One named : stats<monthnumber>.txt and another totalb<monthnumber>.txt

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
/*******************************************************
*     MYCPLUS Sample Code - http://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
 
public class UDStats extends JPanel implements ActionListener
{
    private JLabel rs,l1,l2;
    private JMenuBar menubar;
    private JTextField inud;
    private static JProgressBar pb,pb2;
    public static JFrame frame;
    public static int addbytes, totalbytes, mth, date;
    static BufferedReader ff;
    static PrintWriter tfs;
    static PrintWriter tf;
    static Calendar c;
    static String cpath;
 
    public UDStats()
    {
        c=Calendar.getInstance();
        mth=c.get(Calendar.MONTH)+1;
        date=c.get(Calendar.DATE);
 
	    JMenu fileMenu = new JMenu ("File");
        JMenuItem exitItem = new JMenuItem ("Exit");
        fileMenu.add (exitItem);
        exitItem.addActionListener(new exitud());
 
        rs = new JLabel ("Received \\ Send");
        l1 = new JLabel ("Max = 400MB");
        l2 = new JLabel ("Max = 1000MB");
        menubar = new JMenuBar();
        menubar.add (fileMenu);
        inud = new JTextField (5);
        inud.addActionListener(this);
        pb=new JProgressBar(0, 419430400);
        pb2=new JProgressBar(0, 1048576000);
        pb.setValue(0);
        pb2.setValue(0);
		pb.setStringPainted(true);
		pb2.setStringPainted(true);
		//pb.setValue(task.getCurrent());
 
        setPreferredSize (new Dimension (372, 251));
        setLayout (null);
 
        add (rs);
        add (l1);
        add (l2);
        add (menubar);
        add (inud);
        add (pb);
        add (pb2);
 
        //set component bounds (only needed by Absolute Positioning)
        rs.setBounds (45, 30, 90, 25);
        menubar.setBounds (0, 0, 375, 20);
        inud.setBounds (45, 55, 235, 25);
        pb.setBounds(20, 100 , 350, 30);
        l1.setBounds(175, 135, 75, 25);
        pb2.setBounds(20, 170, 350, 30);
        l2.setBounds(175, 205, 75, 25);
    }
 
 
    public static void main (String[] args) throws IOException
    {
        cpath=System.getProperty("java.class.path");
        frame= new JFrame ("UpLoad Download Statistics Manager");
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch(Exception e) {}
 
	    StringBuffer icopath1=new StringBuffer(cpath);
        for(int a=0; a<icopath1.length(); a++)
			if(icopath1.charAt(a)==92)
				icopath1.setCharAt(a, '/');
		String icopath=icopath1.toString()+"/ud.gif";
		frame.setIconImage((new ImageIcon(icopath)).getImage());
 
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new UDStats());
        frame.pack();
        frame.setVisible (true);
 
        ff=new BufferedReader(new FileReader(cpath+"\\totalb"+Integer.toString(mth)+".txt"));
        tfs=new PrintWriter(new BufferedWriter(new FileWriter(cpath+"\\stats"+Integer.toString(mth)+".txt", true)));
        totalbytes=Integer.parseInt(ff.readLine());
        tf=new PrintWriter(new BufferedWriter(new FileWriter(cpath+"\\totalb"+Integer.toString(mth)+".txt", false)));
        pb.setValue(totalbytes);
        //pb.getPercentComplete();
        pb.setString(pprint(totalbytes, 4));
        pb2.setValue(totalbytes);
        //pb2.getPercentComplete();
        pb2.setString(pprint(totalbytes, 10));
    }
 
    public void actionPerformed(ActionEvent e)
    {
	    addbytes=Integer.parseInt(inud.getText());
	    inud.selectAll();
	    totalbytes+=addbytes;
	    try
	    {
		    tfs.println(date+"	"+addbytes);
		    tfs.flush();
	    }
	    catch(Exception e2) {}
	    addbytes=0;
	    pb.setValue(totalbytes);
	    //pb.getPercentComplete();
	    pb.setString(pprint(totalbytes, 4));
	    pb2.setValue(totalbytes);
	    pb2.setString(pprint(totalbytes, 10));
	    //pb2.getPercentComplete();
    }
 
    static String pprint(int b, int s)
    {
	    String temp, progr;
	    int t=b/1048576;
	    double eb=0d;
	    progr=Integer.toString(t);
	    temp=progr+"MB  ";
	    progr=Integer.toString(t/s);
	    temp=temp+progr+"% ";
	    eb=(t-400)*1.4;
	    if(t>400)
	    	temp=temp+"ExtraBill=Rs."+Double.toString(eb);
	    else
	    	temp=temp+"";
	    return(temp);
    }
 
}
 
class exitud extends JPanel implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		UDStats ob=new UDStats();
		ob.tf.println(ob.totalbytes);
		ob.tf.flush();
 
		ob.tfs.println();
		ob.tfs.flush();
		System.exit(0);
	}
}

Tags: ,

Like What you See?

Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!

There are No Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.


Leave a Reply

You must be logged in to post a comment.