Mirage Source
http://web.miragesource.net/forums/

C# Project (1)
http://web.miragesource.net/forums/viewtopic.php?f=158&t=5645
Page 1 of 1

Author:  Exadon [ Fri May 29, 2009 2:04 pm ]
Post subject:  C# Project (1)

Make a console or windows app that inputs a users birthdate and outputs their age.

This program should have comments as well as error catching.

Best program wins a cookie.

Overview:
  • Figure out the age of the person from date inputted.

Notes:
  • Application can be either console or windows application.

Author:  James [ Fri May 29, 2009 6:19 pm ]
Post subject:  Re: C# Project (1)

Console App. I'll simply post code.

SPOILER: (click to show)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
 *  Name: James Will
 *  Challenge: Mirage Source C#(1)
 *  Purpose: User inputs date of birth, output age.
 *  Type: Console APplication
 */

namespace AgeCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            int age = 0;
            bool isBday = false;
            int[] bDayDate = new int[3];
            DateTime curDate = DateTime.Now;

            // Console.WriteLine(curDate.Month + "/" + curDate.Day + "/" + curDate.Year);
       
            // Ask the user for the needed information
            Console.Write("Hello! What is your name?");
            Console.WriteLine("");
            name = Console.ReadLine();

            // Verify Name
            Console.WriteLine("");
            Console.WriteLine("Welcome {0}!", name);
            Console.WriteLine("Press enter to continue.");

            // Wait for enter.
            Console.ReadLine();

            // Clear the console
            Console.Clear();

            // Ask for the date in MM/DD/YYYY form as a string and parse it.
            Console.Write("{0}, please tell me your date of birth using the format MM/DD/YYYY.", name);
            Console.WriteLine("");
            string[] bDay = Console.ReadLine().Split('/');
           
            // Transfer to Integers
            for (int i = 0; i < 3; i++)
            {
                bDayDate[i] = Convert.ToInt32(bDay[i]);
            }

            // Finally, calculate the age and see if it's their birthday.
            if (curDate.Month > bDayDate[0])
            {
                age = curDate.Year - bDayDate[2];
            }
            else if (curDate.Month < bDayDate[0])
            {
                age = curDate.Year - bDayDate[2] - 1;
            }
            else if (curDate.Month == bDayDate[0])
            {
                if (curDate.Day > bDayDate[1])
                {
                    age = curDate.Year - bDayDate[2];
                }
                else if (curDate.Day < bDayDate[1])
                {
                    age = curDate.Year - bDayDate[2] - 1;
                }
                else if (curDate.Day == bDayDate[1])
                {
                    isBday = true;
                    age = curDate.Year - bDayDate[2];
                }
            }

            // Tell them how old they are.
            Console.Clear();
            Console.WriteLine("Well {0}, seems you are {1} year(s) old!", name, age);

            // If it is their birthday, congratulate them!
            if (isBday == true)
            {
                Console.WriteLine("It also happens to be your birthday! Happy Birthday {0}!", name);
            }

            // Read a line to wait for close.
            Console.ReadLine();
        }
    }
}

Author:  Toast [ Thu Jun 04, 2009 7:41 am ]
Post subject:  Re: C# Project (1)

Windows Form Application.

Attachments:
File comment: [C#.NET Application] Tells you how old you are from your birthday.
C# Intro Project (1).rar [5.08 KiB]
Downloaded 437 times

Author:  Tony [ Thu Jun 04, 2009 8:02 am ]
Post subject:  Re: C# Project (1)

Toast wrote:
Windows Form Application.


++ my vote

I'm 16 years, 4 months, and 18 days old.

Author:  Coke [ Thu Jun 04, 2009 11:31 am ]
Post subject:  Re: C# Project (1)

Do you mind if I do it in Java?

Author:  Exadon [ Thu Jun 04, 2009 12:28 pm ]
Post subject:  Re: C# Project (1)

Yeah , you can do it in Java.

Author:  Kamori [ Fri Jun 05, 2009 8:27 pm ]
Post subject:  Re: C# Project (1)

I tried doing this in VB.net and I have a problem with enum's I used Toast's code as a reference but I get a problem with this line of code

Code:
[Enum].Parse(GetType(eMonths), CMB_Month.Text)



and the error I get
Quote:
Must specify valid information for parsing in the string.

Author:  Exadon [ Sat Jun 06, 2009 1:40 pm ]
Post subject:  Re: C# Project (1)

Very nice work toast and james. Toast, I like that your project works on events besides a normal button click event

Author:  Toast [ Sat Jun 06, 2009 7:03 pm ]
Post subject:  Re: C# Project (1)

Thanks, it's not that hard to do lol.

Author:  unknown [ Sat Jun 06, 2009 7:10 pm ]
Post subject:  Re: C# Project (1)

I wrote mine in java.. I also made a custom coded from scratch swing GUI and a JNLP file, so if you wanna learn any of that read through my code =).

Ageulator.java
Code:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;

// The main frame, all the gui elements go on here
class MainFrame extends JFrame implements ActionListener {

    Calendar m_calendar = Calendar.getInstance();
    JComboBox m_birthMonth = new JComboBox();
    JComboBox m_birthDay = new JComboBox();
    JComboBox m_birthYear = new JComboBox();

    // Create and show the main frame
    public MainFrame() {
        // Close the application when we exit
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(true);
        this.setTitle("Ageulator");
        // Create all the components on the main frame
        this.createGUI();
        // Remove any extra space between components
        this.pack();
        // Start application in the center of the screen
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    // Create all the components and put them on the main frame
    private void createGUI() {
        // Simple GUI layout manager
        this.setLayout(new BorderLayout());
        m_birthMonth.addItem("--");
        // Allow the user to select months 1-12
        for (int i = 1; i <= 12; i++) {
            if (i < 10) {
                m_birthMonth.addItem("0" + i);
            } else {
                m_birthMonth.addItem(i);
            }
        }
        this.add(m_birthMonth, BorderLayout.WEST);

        m_birthDay.addItem("--");
        // Allow the user to select days 1-32
        for (int i = 1; i <= 32; i++) {
            if (i < 10) {
                m_birthDay.addItem("0" + i);
            } else {
                m_birthDay.addItem(i);
            }
        }
        this.add(m_birthDay, BorderLayout.CENTER);

        m_birthYear.addItem("----");
        // Allow the user to select years 1900-present
        for (int i = m_calendar.get(Calendar.YEAR); i >= 1900; i--) {
            m_birthYear.addItem(i);
        }
        this.add(m_birthYear, BorderLayout.EAST);

        // Add a button and make it execute actionperformed when clicked
        JButton calculateAge = new JButton("Calculate my age");
        calculateAge.addActionListener(this);
        this.add(calculateAge, BorderLayout.PAGE_END);
    }

    // Called when the calculate button is pushed
    public void actionPerformed(ActionEvent e) {
        int selDay;
        int selMonth;
        int selYear;
        try {
            selDay = Integer.parseInt(m_birthDay.getSelectedItem().toString());
            selMonth = Integer.parseInt(m_birthMonth.getSelectedItem().toString());
            selYear = Integer.parseInt(m_birthYear.getSelectedItem().toString());
            // Sometimes the age is just current year - birth year
            int age = m_calendar.get(Calendar.YEAR) - selYear;

            // If currentmonth < birthmonth
            if (m_calendar.get(Calendar.MONTH) + 1 < selMonth) {
                age--;
            }

            // If currentmonth = birthmonth and currentday < birthday
            if (m_calendar.get(Calendar.MONTH) + 1 == selMonth) {
                if (m_calendar.get(Calendar.DAY_OF_MONTH) < selDay) {
                    age--;
                }
            }
            // Display your age
            JOptionPane.showMessageDialog(this,
                    "You are " + age + " years old!",
                    "Your age",
                    JOptionPane.INFORMATION_MESSAGE);
        } catch (java.lang.Exception ex) {
            // An error occured parsing (you didn't select a number)
            JOptionPane.showMessageDialog(this,
                    "Please enter a valid age!",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

public class Ageulator {

    private static void createAndShowGUI() {
        MainFrame mainFrame = new MainFrame();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Agulator.jnlp
Code:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp codebase="http://www.raymondcox.net/" spec="1.0+" href="Ageulator.jnlp">
    <information>
        <title>Agulator</title>
        <vendor>Raymond Cox</vendor>
        <homepage href="http://www.raymondcox.net/"/>
        <description>A simple age calculator tool</description>
        <description kind="short">Simple Age Calculator</description>
    <offline-allowed/>
</information>
    <resources>
<j2se version="1.5+"/>
<jar eager="true" href="Ageulator.jar" main="true"/>
    </resources>
    <application-desc main-class="Ageulator">
    </application-desc>
</jnlp>


The code doesn't contain any 'real' error handling, but some basic stuff is in place.

Executables
Download the JNLP
Download the JAR

EDIT: the gui looks kinda ugly on windows.. I'm going to work on fixing that later.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/