As we all know given everybody's busy schedule we don't try to calculate the exact newspaper bill of every month and the newspaper hawker usually charges more than actual total cost of our monthly newspaper subscription. The main problem is that newspapers have different rates on different days. Especially on Saturday and Sundays. Sometimes even on Monday the rate can be Re. 1.
To make it little easier I'd written a Java utility to calculate monthly newspaper bill. Just feed in the daily newspaper cost and it correctly calculates the total monthly subscription cost.
It is written in simple Java and GUI(Graphics User Interface) part is done using Swing. Swing is very popular Java library using which we can easily nice graphical elements like windows, buttons or everything we see on computer screens. We can even create a Browser too!
Remember this java file uses a com library which you can extract from the jar file(using winrar or free 7zip).
Find NewsPaper.jar in the attachment.
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BoxLayout;
import javax.swing.border.TitledBorder;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.text.*;
import winlib.*;
import com.toedter.calendar.*;
import javax.swing.JFileChooser.*;
public class NewsPaper extends JFrame
{
public Dimension getPreferredSize()
{
return new Dimension(common.getScreenWidth()/2,common.getScreenHeight()/2);
}
public NewsPaper()
{
super("NewsPaper Calc");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(1,2,1,1));
panel1.setBorder(new TitledBorder(new EtchedBorder(Color.cyan,Color.gray),"Start and End Date"));
_start_date.set(Calendar.DAY_OF_MONTH,1);
_end_date.set(Calendar.DAY_OF_MONTH,_end_date.getActualMaximum(Calendar.DAY_OF_MONTH));
final JButton start_date=new JButton(common.convert(_start_date.getTime(),"dd/MMM/yyyy"));
final JButton end_date=new JButton(common.convert(_end_date.getTime(),"dd/MMM/yyyy"));
ActionListener startli=new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Callback cb=new Callback(null,null)
{
public Object inform(Object name,Vector na1)
{
if(name!=null)
{
_start_date=(Calendar)name;
start_date.setText(common.convert(_start_date.getTime(),"dd/MMM/yyyy"));
}
return null;
}
};
common.getDateFromUser(NewsPaper.this,cb,true);
}
};
ActionListener endli=new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Callback cb=new Callback(null,null)
{
public Object inform(Object name,Vector na1)
{
if(name!=null)
{
_end_date=(Calendar)name;
end_date.setText(common.convert(_end_date.getTime(),"dd/MMM/yyyy"));
}
return null;
}
};
common.getDateFromUser(NewsPaper.this,cb,true);
}
};
start_date.addActionListener(startli);
end_date.addActionListener(endli);
panel1.add(start_date);
panel1.add(end_date);
JPanel panel11=new JPanel();
panel11.add(new JScrollPane(panel1));
getContentPane().add(panel11);
JPanel panel2=new JPanel();
panel2.setLayout(new FlowLayout());
String [] days={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
for(int i=0;i<days.length;++i)
{
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,2,1,1));
String day=days[i];
JTextField field=new JTextField(8);
JLabel l=new JLabel(day,SwingConstants.RIGHT);
panel.add(l);
panel.add(field);
_price.put(day,field);
panel2.add(panel);
}
getContentPane().add((panel2));
JPanel panel3=new JPanel();
panel3.setLayout(new BoxLayout(panel3,BoxLayout.X_AXIS));
panel3.add(_calc);
ActionListener calcli=new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(!verifyDates())
{
common.show_error("Error","Invalid set of dates!");
return;
}
Iterator it=_price.keySet().iterator();
while(it.hasNext())
{
Object day=it.next();
JTextField f= (JTextField) _price.get(day);
String text=f.getText().trim();
if( text==null|| text.length()==0)
{
common.show_error("Error","Fill the day-wise prices correctly");
return;
}
try{
float i=Float.parseFloat(text);
if(i<0) throw new Exception("ss");
}
catch(Throwable eth)
{
common.show_error("Error","Fill the day-wise prices correctly");
return;
}
}
//now calculate
float sum=0;
Calendar start=(Calendar)_start_date.clone();
while(true)
{
//see which day it is
if( Calendar.MONDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Mon")).getText().trim());
sum+=f;
}
if( Calendar.TUESDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Tue")).getText().trim());
sum+=f;
}
if( Calendar.WEDNESDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Wed")).getText().trim());
sum+=f;
}
if( Calendar.THURSDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Thu")).getText().trim());
sum+=f;
}
if( Calendar.FRIDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Fri")).getText().trim());
sum+=f;
}
if( Calendar.SATURDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Sat")).getText().trim());
sum+=f;
}
if( Calendar.SUNDAY== start.get(Calendar.DAY_OF_WEEK))
{
float f= Float.parseFloat( ((JTextField)_price.get("Sun")).getText().trim());
sum+=f;
}
start.add(Calendar.DAY_OF_MONTH,1);
//see if it's greater than end date
if( start.get(Calendar.YEAR)> _end_date.get(Calendar.YEAR)) break;
else
if( start.get(Calendar.YEAR)== _end_date.get(Calendar.YEAR))
if ( start.get(Calendar.DAY_OF_YEAR) > _end_date.get(Calendar.DAY_OF_YEAR)) break;
}
common.show_success(NewsPaper.this,"Result","Total Amount="+common.floatPrecision ( (double)sum,2));
}
};
_calc.addActionListener(calcli);
getContentPane().add(panel3);
pack();
setVisible(true);
}
public static void main(String args[])
{
new NewsPaper();
}
private boolean verifyDates()
{ //start date should be <= end date
if ( _end_date.getTime().getTime()<_start_date.getTime().getTime()) return false;
return true;
}
Calendar _start_date=new GregorianCalendar(),_end_date=new GregorianCalendar();
Map _price=new TreeMap();
JButton _calc=new JButton("Calculate");