Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class And Stream Writer Example
12-19-2009, 08:55 AM
Post: #1
Class And Stream Writer Example
Code:
using System;
using System.IO;
using System.Text;
namespace Program_Test
{
    public class Default
    {
        
        public class Human//Make your class
        {
            public int age, weight;
            public float height;
            public string First_name, Last_Name, Middle_Name, Gender;


        };

        public static void Main(string[] args)
        {
        

            try//try for the Stream Writer
            {

                Console.Title = "Human Creation";
                Console.WriteLine("Welcome To Human Creation.\n Do You Want To Create A New Human? \n (Y/N)\n");
                string answer = Console.ReadLine();
                if (answer == "Y")
                {
                    
                    Console.WriteLine("Good Now Lets Get Started...\n");
                    Console.WriteLine("Enter In The New Humans Name(First Name)\n");
                    Human new1 = new Human();//Creating a Variable off of The Class "Human"
                    new1.First_name = Console.ReadLine();
                    Console.WriteLine("Enter In The Humans's Middle Name");
                    new1.Middle_Name = Console.ReadLine();
                    Console.WriteLine("Enter In The Humans Last Name");
                    new1.Last_Name = Console.ReadLine();
                    Console.WriteLine("Good here Is The Info So Far");
                    Console.WriteLine("First Name {0}, Middle Name {1} , Last Name {2}", new1.First_name, new1.Middle_Name, new1.Last_Name);
                    String initials = new1.First_name[0].ToString() + "." + new1.Middle_Name[0].ToString() + "." + new1.Last_Name[0].ToString();
                    //Initials as you know are the first char of every Name ( Fist Middle Last)
                    //So i just used the first char in the array index[0] and converted it to string
                    Console.WriteLine("And The Initials {0}", initials);
                    Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine("***********************\n Physical Attributes\n***********************");
                    Console.WriteLine("Enter The Age Of Your Human");
                    new1.age = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("Enter The Height Of Your Human i.e 5.11");
                    new1.height = float.Parse(Console.ReadLine());// float for decimals
                    Console.WriteLine("Enter The Weight Of Your Human");
                    new1.weight = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("Is Your Human Male Or Female(Male|Female)");
                    new1.Gender = Console.ReadLine();
                    StreamWriter Info = new StreamWriter("Human.txt",true , Encoding.ASCII);//Makes a new Stream Writer , ASCII Encoding bool as true
                    Info.WriteLine("***Personal Information***\n");
                    Info.WriteLine("Humans Name: {0} {1} {2}\nInitials:  {3}", new1.First_name, new1.Middle_Name, new1.Last_Name, initials);
                    Info.WriteLine("***Physical Attributes***\n");
                    Info.WriteLine("Age: {0} \n Height: {1} \n Weight: {2} \n Gender: {3}",new1.age,new1.height,new1.weight,new1.Gender);
                    Info.Close();//Closes the Stream Writer
                }
                else
                {
                    Console.WriteLine("Exiting...");
                    System.Threading.Thread.Sleep(500);
                    Environment.Exit(0);
                }
            }
            catch (Exception Exc)
            {
                Console.WriteLine("Exception {0}", Exc);//Exceptions
                Console.ReadLine();
            }
            finally
            {
                Console.WriteLine("Human Created! Exiting...");//FINALLY
                System.Threading.Thread.Sleep(500);
                Environment.Exit(0);

            }
        

        }
            
    }
}
Syntax Highlighting
http://pastebin.com/m2d192446

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
12-19-2009, 09:15 PM
Post: #2
RE: Class And Stream Writer Example
Very good code !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
12-30-2009, 12:01 AM
Post: #3
RE: Class And Stream Writer Example
one question why do you use console.readline(); in catch
Console.WriteLine("Exception {0}", Exc);//Exceptions
Console.ReadLine();

or you do it just to stay in the cmd windows?
I always use return;
that sends you back on the try clause
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: