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
Solved

Q2 - Validate Contract Expiry Date

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

840 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