Newspaper monthly bill calculator utility in Java


This article describes a newspaper monthly bill calculator utility written in Java. This tool shows how we can create simple and useful GUI tools in Java's GUI library- Swing. Java is best suited for developing cross platform compatible tools - which can be run anywhere in the world!

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).


How to run


java -jar NewsPaper.jar


Find NewsPaper.jar in the attachment.


code


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");



Attachments

Related Articles

Creating new JEE 5 applications (Java and EJB Web Services) using RAD 7.5

Now days, most of the enterprise applications are developed in RAD 7.0+ (Rational Application Developer). The Web service application can be written in Java or in EJB. For creating Web services in Java or EJB, the vast documentation is available. Referring such documentation during speedy development period becomes a hectic job. So to avoid it, this article will help to easily develop a JEE5 application using simple steps.

Program for Addition performer in Java

This article provides a description to the addition performer in Java, that can perform addition operation on two input numbers. Code for the addition performer is provided in the article. The program is based on use of Swings. In Java Frames can be created and different text fields alongwith labels corresponding to them can be added. And use of all these elements can develope an efficient adder.

What is difference between an Interface and Abstract class in Java?

Are you looking for an article which explains the concepts of an Interface and Abstract class in simple language? Then your search ends here. Read this article, herein the detail about abstract class and an Interface is given. Also the important differences between an Interface and Abstract class is in Java is explained. This article highlights the important aspects regarding whether abstract class to be used or an Interface is to be used for given requirement of Enterprise application.

Directory Structure - XML Output

In this article, I have submitted a Java program. We will be able to get the dynamic XML file with treeview represents a folder and file structure of a given folder path using it. This Java program also show how to use a recursive function to read a directory structure and then save it to XML file. I commented properly for your better understanding how the program flow goes. The output XML file is well-formed

More articles: Java

Comments

Author: Deva13 Apr 2014 Member Level: Bronze   Points : 0

Can u please mail me "Newspaper monthly bill calculator utility in Java" complete project on my email id as
devaspharer@gmail.com?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: