Pig Latin Translator

PigLatinTranslator

PigLatinTranslator2

What would a code project be like without a Pig Latin translator right? 🙂 Ok so maybe it sounds lame but for a coding project this was fun. Also this was my first use of nested classes, as you can see from the Class Designer screenshot. I have to admit this route is nice for smaller programs that aren’t very complicated. To me it seems to keep the program from getting complicated with many class files doing all sorts of things.

So the nested class takes care of taking the first letter of the first and last name, seeing if there’s a vowel, and via conditional operators making a decision on how to translate the name. I also used Read-Only getters / setters on this one.

Piglatin Class:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PigLatin
{
    public partial class Piglatin : Form
    {
        private Translate pigTranslate = new Translate();

        public Piglatin()
        {
            InitializeComponent();
        }

        private void translateButton_Click(object sender, EventArgs e)
        {
            string pigName = "";

            //Set all the labels used to display errors to black.
            firstNameLabel.ForeColor = Color.Black;
            lastNameLabel.ForeColor = Color.Black;
            translationLabel.ForeColor = Color.Black;

            //Checking to see if something is entered into the text boxes.
            if (firstNameTextBox.Text.Length < 1)
                firstNameLabel.ForeColor = Color.Red;
            if (lastNameTextBox.Text.Length < 1)
                lastNameLabel.ForeColor = Color.Red;

            //Checking for errors.
            bool isError = false;
            isError = (firstNameLabel.ForeColor == Color.Red || lastNameLabel.ForeColor == Color.Red);

            //If there are no errors then commence translation.
            if (!isError)
            {
                pigTranslate.makePigLatin(firstNameTextBox.Text.Trim(), lastNameTextBox.Text.Trim(), ref pigName);
                translationLabel.Text = "Translation: " + pigTranslate.PigName;
            }
            else
            {
                //Error if both boxes are blank.
                if (firstNameLabel.ForeColor == Color.Red && lastNameLabel.ForeColor == Color.Red)
                {
                    MessageBox.Show("Please fill in " + firstNameLabel.Text + " and " + lastNameLabel.Text, this.Text, MessageBoxButtons.OK);
                }
                //Error if only first name box is blank.
                else if (firstNameLabel.ForeColor == Color.Red)
                    MessageBox.Show("Please fill in " + firstNameLabel.Text, this.Text, MessageBoxButtons.OK);
                //Error if the last name box is blank.
                else
                    MessageBox.Show("Please fill in " + lastNameLabel.Text, this.Text, MessageBoxButtons.OK);
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        internal class Translate
        {
            private string _firstName;
            private string _lastName;
            private string _pigName = "";
            private string _vowels = "AEIOUaeiou";

            internal string FirstName
            {
                get { return _firstName; }
            }

            internal string LastName
            {
                get { return _lastName; }
            }

            internal string PigName
            {
                get { return _pigName; }
            }

            internal string Vowels
            {
                get { return _vowels; }
            }

            internal void makePigLatin(string firstName, string lastName, ref string pigName)
            {
                //Getting the first letter of the first name. Then the rest of the first name.
                string firstNameLetter = firstName.Substring(0, 1);
                string restOfFirstName = firstName.Substring(1, firstName.Length - 1);

                //Getting the first letter of the last name. Then the rest of the last name.
                string lastNameLetter = lastName.Substring(0, 1);
                string restOfLastName = lastName.Substring(1, lastName.Length - 1);

                //Seeing if the first name starts with a vowel.
                int firstNameVowel;
                //Using the IndexOf methods return value to determine if there's a vowel.
                firstNameVowel = Vowels.IndexOf(firstNameLetter);
                if (firstNameVowel != -1)
                {
                    //If there is a vowel.
                    _firstName = firstName + "way";
                }
                else
                {
                    //If there isn't a vowel.
                    _firstName = restOfFirstName + firstNameLetter + "ay";
                }

                //Seeing if the last name starts with a vowel.
                int lastNameVowel;
                //Using the IndexOf methods return value to determine if there's a vowel.
                lastNameVowel = Vowels.IndexOf(lastNameLetter);
                if (lastNameVowel != -1)
                {
                    //If there is a vowel.
                    _lastName = lastName + "way";
                }
                else
                {
                    //If there isn't a vowel.
                    _lastName = restOfLastName + lastNameLetter + "ay";
                }

                //Setting the pig name.
                _pigName = _firstName + " " + _lastName;
            }
        }
    }
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s