Solved

Q2 - Validate Contract Expiry Date

Posted on 2002-05-19
4
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 94
jmeter usage 4 61
netbeans can not run self signed app 8 37
Delete .class files at every compile 4 55
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

739 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