Monday, March 9, 2015

Age Verifier

    I created a program to check if someone is over 18. I only used the main method just because this is a simple program and it went faster.

    The first 6 lines import the necessary files for creating an OptionPane and getting the date.

    From line 9 to 34 i am initializing variables for the current date and the entered date. Specifically, lines 27 to 29 are used to create the panes that get the users input.

    The rest of the code from line 35 on is checking the input with the current time to decide whether the person is 18. Depending on whether they are or aren't it will print in the console and in a pane if they are 18 or not.

    Currently there is nothing to handle if the person inputs a string, or the wrong format or if they click cancel. I am fairly new to JOptionPanes and don't know how to handle the cancel button. With the wrong input I could have handled it by using some if statements on the input.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

public class Main {

 public static void main(String[] args) {

  String month, day, year;
  int imonth, iday, iyear, itmonth, itday, ityear;
  Date date = new Date();

  DateFormat monthf = new SimpleDateFormat("MM");
  DateFormat dayf = new SimpleDateFormat("dd");
  DateFormat yearf = new SimpleDateFormat("yyyy");

  String todaym = monthf.format(date);
  String todayd = dayf.format(date);
  String todayy = yearf.format(date);

  itmonth = Integer.parseInt(todaym);
  itday = Integer.parseInt(todayd);
  ityear = Integer.parseInt(todayy);

  month = JOptionPane.showInputDialog("Month of birth(MM)");
  day = JOptionPane.showInputDialog("Day of birth(DD)");
  year = JOptionPane.showInputDialog("Year of birth(YYYY)");

  imonth = Integer.parseInt(month);
  iday = Integer.parseInt(day);
  iyear = Integer.parseInt(year);

  if (iyear - ityear == -18) {
   if (imonth - itmonth == 0) {
    if (iday - itday <= 0) {
     System.out.println("you are 18");
     JOptionPane.showMessageDialog(null, "you are 18");
    } else if (iday - itday > 0) {
     System.out.println("you are not 18");
     JOptionPane.showMessageDialog(null, "you are not 18");
    }
   } else if (imonth - itmonth < 0) {
    System.out.println("you are 18");
    JOptionPane.showMessageDialog(null, "you are 18");
   } else if (imonth - itmonth > 0) {
    System.out.println("you are not 18");
    JOptionPane.showMessageDialog(null, "you are not 18");
   }
  } else if (iyear - ityear > -18) {
   System.out.println("you are not 18");
   JOptionPane.showMessageDialog(null, "you are not 18");
  } else if (iyear - ityear < -18) {
   System.out.println("you are 18");
   JOptionPane.showMessageDialog(null, "you are 18");
  }
 }
}

No comments:

Post a Comment