Solved

Q2 - Validate Contract Expiry Date

Posted on 2002-05-19
4
254 Views
Last Modified: 2010-03-31
My second problem is trying to display the date (current) that the session is being entered and to check this against the expiry date, and display an error message if the contract has expired.  The expiry date on file is currently a dd/mm/yy string. I've been looking at dateFormat, simpleDateFormat and Calendar, but am getting out of my depth and again would be very grateful for help.

WTA

LRH

/*
      Session Costing Window using AWT classes
*/
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

public class SessionWindow extends Frame implements ActionListener, FocusListener{
      private int recNum, numOfRecs;
      private double rTotal;
      private Session mt;        //use SessionType as locer not known until selected
      private TextField txtMemNum, txtTime;
      private Label lblMemDets, lblSessTime, lblCost, lblTimeErr, lblRunTotal;
      
      private TypePremium[] tp = new TypePremium[25];       //array of product details
      
      RunningTotal rt = new RunningTotal();

      public SessionWindow(String title){
            super(title);            //add title to window bar
            setBackground(new Color(0xd9,0xeb,0xeb));
            addWindowListener(new WindowEventListener()); //ensure window events are used
            setLayout(new FlowLayout(FlowLayout.RIGHT, 30, 10)); //set default positioning
            
            Label lblMember = new Label("Member number"); //add components
            add(lblMember);

            txtMemNum = new TextField("", 5);
            add(txtMemNum);
            txtMemNum.addFocusListener(this); //check when focus is changed to text box

            lblMemDets = new Label("");
            add(lblMemDets);

            lblTimeErr = new Label("");
            add(lblTimeErr);

            Label lblSessTime = new Label("Session Length");
            add(lblSessTime);

            txtTime = new TextField("1", 5);
            add(txtTime);

            lblCost = new Label("Cost = ");
            add(lblCost);

            lblRunTotal = new Label("Total sessions cost = ");
            add(lblRunTotal);

            Button cmdCalcCost = new Button("Calculate Cost");
            add(cmdCalcCost);
            cmdCalcCost.addActionListener(this); //listen for button clicks

            recNum = 0;
            LoadArray();
      }

      public void LoadArray(){
            TextFileRead tf = new TextFileRead("memberDetails.txt", 25);      //set-up file for reading
            String sArray[] = new String[25];
                  
            sArray = tf.ReadTextLines();                              //load String array from file
            numOfRecs = tf.NumberOfLinesRead();
            for(int i = 0; i < numOfRecs; i++)      //separate String into tokens
                  parseLine(sArray[i], i);
      }

      private void parseLine(String s, int record){      //separate string into fields
            StringTokenizer st = new StringTokenizer(s, "\t");
            
            int mN=Integer.parseInt(st.nextToken());
            String fN=st.nextToken();
            String sN=st.nextToken();
            String mT=st.nextToken();
            String eD=st.nextToken();
                  
            tp[record]=new TypePremium(mN, fN, sN, mT, eD);
      }
      public int FindSession (int mn){
            for(int i=0; i < numOfRecs; i++)
                  if (tp[i].GetMemNum() == mn) return i;
            return -1;
      }
      
      public void focusGained(FocusEvent e){      //initialise when focus is on text box
            txtMemNum.setText("");
            lblMemDets.setText("----------------------------------------------------------------------------------------");
            lblTimeErr.setText("----------------------------------------------------------------------------------------");
            lblCost.setText("Cost = £0.00");
            show();
      }

      public void focusLost(FocusEvent e){ //output details for product when focus lost
            recNum = FindSession(Integer.parseInt(txtMemNum.getText()));
            if(recNum >= 0)
                  lblMemDets.setText(tp[recNum].toString());
            else
                  lblMemDets.setText("------------- Sorry, this is not a current member number. -------------");
            show();
      }

      public void actionPerformed(ActionEvent e){ //process button clicks

            String arg = e.getActionCommand();

            int loc = FindSession(Integer.parseInt(txtMemNum.getText()));
            if("Calculate Cost".equals(arg) && loc >= 0){
                  if(tp[loc].GetMemType().equals("Prem"))
                        mt = new TypePremium(tp[loc].GetMemNum(), tp[loc].GetFirstName(),
                              tp[loc].GetSurName(), tp[loc].GetMemType(),tp[loc].GetExpDate());
                  else if(tp[loc].GetMemType().equals("Std"))
                        mt = new TypeStandard(tp[loc].GetMemNum(), tp[loc].GetFirstName(),
                              tp[loc].GetSurName(), tp[loc].GetMemType(),tp[loc].GetExpDate());
                  else
                        mt = new TypeCasual(tp[loc].GetMemNum(), tp[loc].GetFirstName(),
                              tp[loc].GetSurName(), tp[loc].GetMemType(),tp[loc].GetExpDate());
            
                  double cost = mt.calcCost(Integer.parseInt(txtTime.getText()));  //use polymorphism
      
java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance();
      nf.setMinimumFractionDigits(2);
                  
lblCost.setText("Session Cost = £" + nf.format(cost)); //output calculated profit
                        show();

rt.InputValue(cost);
      rTotal=rt.RunningTotal();

lblRunTotal.setText("Total Sessions Cost = £" + nf.format(rt.RunningTotal())); //output calculated profit
                  show();

            }

      }

}


0
Comment
Question by:LittleRedHat
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 150 total points
ID: 7020130
Here's how to parse a date:

DateFormat df = new SimpleDateFormat("d/M/y");
Date expiry = df.parse("1/4/2002"); // put your date here
Date now = new Date();

if (expiry.before(now)) {
  // bad expiry date
}
0
 

Author Comment

by:LittleRedHat
ID: 7020522
Thanks for the reply, yoren.  That's helpful, but sorry I don't think I've explained my confusion very clearly.  This is just my second attempt at Java and I keep getting lost working out how I "put the pieces together".

The expiry date is in a text file.  I've worked out how to have the program read the text file and display the expiry date (along with along with the other contract details) in the window, but I can't work out how I go about having the program validate the date and then display the information/result in the window.

Sorry if this still isn't clear, as you can see I'm needing some guidance at "idiot level". :-)

With thanks again.

LRH
0
 

Author Comment

by:LittleRedHat
ID: 7037907
Long delay = long time working out how to pull the expiry date in from the text file then fit it into my code in the right place ... found lots of possible wrong places en route!!! :-)  But did find your code helpful as my baseline.

With thanks.

LRH
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8077205
Grade changed as requested

Computer101
E-E Admin
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 58
diffSum example 4 36
MySQL  on Tomcat 8 43
jsp login check 12 27
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now