A A

A A

  • NA
  • 21
  • 11.9k

Looking for tips and help with my code?

Sep 29 2013 11:45 PM
So i am working on a practice code and im trying to see how things work differently in C#. Im a little more acquainted with C++ but now im learning C#. I really like it but im so used to C++ that i end up writing codes in that language so im trying to get a little more familiarized with this language. My code is really simple but im trying to do things like use constructors and methods in different classes etc. here im asking the user to enter information about his dog and when i execute the program i want it to show a default information like it is incorrect for example "name: no name, race: no race, age: 1 and weight: 1, then i want it to show the information the user entered.Sorry i am still a beginner but im really trying to learn this well. here is what i wrote and i side noted where and what problems im having. Thank you so much in advance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Final_Tarea_2
{
    class Animal
    {
        private string name;
        private string race;
        private double weight;
        private double age;

        public double _age
        {
            get //i want to use 'get' and 'set' for my private variables 'age' and 'weight'
            {
                return age;
            }
            set
            {

                if (value > 0 && value <= 25) age = value;//if this condition is false then i want the
                                                         // else to show in my program but it doesnt do
                                                        //anything. its like if the statement in else did
                                                        //not exist. for example if you enter that the dog's
                                                        //age is 500, at the end of the run it shows that
                                                        //dog's age is 500 instead of 'invalid age' and
                                                        //the default age of '1'. the same occurs with weight.

                else
                {
                age = 1;
                Console.WriteLine("Invalid Age!.");
                }
            }
        }
        public double _weight
        {

            get
            {
                return weight;
            }
            set
            {

                if (value > 0 && value <= 60) weight = value;

                else
                {
                    weight = 1;
                    Console.WriteLine("Invalid Weight!");
                }
            }
        }
        
            public Animal() // its unnecessary but i want to create a constructor
                            //i finally realized that is has to be named after the class
                            //and that it does not need a return type
            {
                Console.Write("These are the constructor details : ");
                getdetails();
            }
        public static getdetails()//so here i want to create the method that i must use inside
                                  //my constructor 'Animal'. its asking that i enter a return type
                                  //but im having trouble because void does not convert string and
                                  //double together. would i have to create 2 methods?
                                  //one for type string and another for type double?
        {
            name = Console.Write("Name: No Name");
            race = Console.Write("Race: No Race");
            age = Console.Write("Age: " + age);
            weight = Console.Write("Weight: " + weight);
        }


       

        static void Main(string[] args)
        {

            Animal dog = new Animal();

            dog.getdetails();//i am confused here im trying to call the details for the
                                     //default information but its not working.

            Console.Write("Please enter your dog's name: ");
            dog.name = Console.ReadLine();
            Console.Write("Please enter your dog's age: ");
            dog.age = double.Parse(Console.ReadLine());
            Console.Write("Please enter your dog's weight: ");
            dog.weight = double.Parse(Console.ReadLine());
            Console.Write("Please enter your dog's race: ");
            dog.race = Console.ReadLine();


            Console.WriteLine(dog.name);
            Console.WriteLine(dog.race);
            Console.WriteLine(dog.weight);
            Console.WriteLine(dog.age);
        }
    }
}
 

Answers (1)