lnyfrnd
lnyfrnd

Reputation: 11

Output is not what its supposed to be

using System;

public class Program
{   
    
        public class toy
        {
            string name = "";
            double price = 0;
            
            public string getName()
            {
                return name;
            }
            
            public double getPrice()
            {
                return price;
            }
            
            public void setName(string Name)
            {
                name = Name;
            }
            
            public void setPrice(double Price)
            {
                price = Price;
            }
            
            static void Main()
                
                
    {
            
            
            toy toy1 =new toy();
            toy1.setName("Car");
            toy1.setPrice(10);
            
            toy toy2 =new toy();
            toy2.setName("Soldier");
            toy2.setPrice(15);
            
            Console.WriteLine(toy1);
            Console.WriteLine(toy2);
                        
            
        }
    }
}

Hello, everyone. I am new at programming. What did I do wrong here? The output says "Program+toy Program+toy"

I wanted it to be like " Car 10 Soldier 15"

Upvotes: 1

Views: 127

Answers (2)

Peyman Majidi
Peyman Majidi

Reputation: 1985

When you print toy1 directly, you print the object not its name;
Already you define a cool method to get the toy name: getName()
So if you want to print the toy's name just call this method like this:

Console.WriteLine(toy1.getName());

but if you want to print the toy name by calling the object name, you should override the ToString() method that runs whenever you behave your object like a string.
when you call Console.WriteLine(SOMETHING_STRING), this method run ToString()

Let's redefine or override ToString() method in a way you expected before:

public override string ToString()
{
    return $"The toy name is `{name}` and blah blah blah";
}

The final code is like:

public class Toy
{
        private string name;
        private double price;
        
        public string GetName()
        {
            return this.name;
        }
        
        public double GetPrice()
        {
            return this.price;
        }
        
        public void SetName(string name)
        {
            this.name = name;
        }
        
        public void SetPrice(double price)
        {
            this.price = price;
        }

        public override string ToString()
        {
            return $"The toy name is `{this.name}` and blah blah blah";
        }

}

I made some changes in your code to follow C# naming conventions. Please notice about changes. for more read this article

Welcome to C#

In the Csharp there is a built-in getter/setter method and unlike Java you do not have to define your setter method like SetPrice().

As you did nothing special in the SetPrice() method, you can simply remove it and set the price like this: toy1.Price = 1000; or get it like: total_price += toy1.Price;, but if you want to validate user input or any other reason, In Csharp we do like this:

    private double price;
    public double Price
    {
        get
        {
            return price;
        }
        set
        {
            if (value >= 0) // price shouldn't be negative
                price = value;
            else
                price = 0; // or throw execption

        }
    }    

If I want to shrink your code it will be like:

public class Toy
{
    public string Name{ get; set; } 
    public double Price{ get; set; } 
    public override string ToString() => $"The toy name is `{this.Name}` and blah blah blah";
}

Upvotes: 2

bfren
bfren

Reputation: 493

If you add the following to your toy class:

public override string ToString()
{
    return $"{name} {price:0.00}";
}

Then you can do Console.WriteLine(toy1); and get what you want. However, also follow Jon Skeet's advice!

Explanation

The dollar sign at the start of the string stands for C#'s interpolated string feature, which allows you to insert variables into a string. The curly braces then tell C# to insert a variable of that name into the string.

An alternative way of doing the same thing would be:

return string.Format("{0} {1:0.00}", name, price);

The '0.00' part tells C# how to format the number - in this case it will round the double to two decimal points, like a price would normally be.

Upvotes: 3

Related Questions