Mirage Source

Free ORPG making software.
It is currently Fri Apr 19, 2024 2:10 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: C# Project (1)
PostPosted: Fri May 29, 2009 2:04 pm 
Offline
Regular
User avatar

Joined: Thu Aug 28, 2008 7:55 pm
Posts: 62
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.


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Fri May 29, 2009 6:19 pm 
Offline
Banned
User avatar

Joined: Mon Jun 05, 2006 9:22 pm
Posts: 394
Location: USA
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();
        }
    }
}


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Thu Jun 04, 2009 7:41 am 
Offline
Regular
User avatar

Joined: Sun Jun 01, 2008 8:39 pm
Posts: 91
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 402 times
Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Thu Jun 04, 2009 8:02 am 
Offline
Persistant Poster
User avatar

Joined: Wed Nov 29, 2006 11:25 pm
Posts: 860
Location: Ayer
Toast wrote:
Windows Form Application.


++ my vote

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

_________________
Image


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Thu Jun 04, 2009 11:31 am 
Offline
Persistant Poster
User avatar

Joined: Thu Aug 17, 2006 5:27 pm
Posts: 866
Location: United Kingdom
Do you mind if I do it in Java?


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Thu Jun 04, 2009 12:28 pm 
Offline
Regular
User avatar

Joined: Thu Aug 28, 2008 7:55 pm
Posts: 62
Yeah , you can do it in Java.


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Fri Jun 05, 2009 8:27 pm 
Offline
Newbie

Joined: Fri Jun 05, 2009 8:24 pm
Posts: 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.


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Sat Jun 06, 2009 1:40 pm 
Offline
Regular
User avatar

Joined: Thu Aug 28, 2008 7:55 pm
Posts: 62
Very nice work toast and james. Toast, I like that your project works on events besides a normal button click event


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Sat Jun 06, 2009 7:03 pm 
Offline
Regular
User avatar

Joined: Sun Jun 01, 2008 8:39 pm
Posts: 91
Thanks, it's not that hard to do lol.


Top
 Profile  
 
 Post subject: Re: C# Project (1)
PostPosted: Sat Jun 06, 2009 7:10 pm 
Offline
Regular

Joined: Sun Apr 26, 2009 11:22 pm
Posts: 43
Location: Cincinnati, OH
Google Talk: rj.cox101@gmail.com
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group