Solved

Q2 - Validate Contract Expiry Date

Posted on 2002-05-19
4
249 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

14 Experts available now in Live!

Get 1:1 Help Now