?
Solved

Q2 - Validate Contract Expiry Date

Posted on 2002-05-19
4
Medium Priority
?
264 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 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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