Home > database >  Simple C# Program - Classes/Objects
Simple C# Program - Classes/Objects

Time:10-02

I was given starter code in C# and told to write code that will make it work. This is the starter code that I was given:

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            Cup coffeeCup = new Cup() { TypeOfCup = TypeOfCup.Coffee };
            coffeeCup.AddItem(new liquid().WithDescription("Coffee"));
        }
    }
}

This is what I wrote in attempts to make the starter code function:

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            Cup coffeeCup = new Cup() { TypeOfCup = TypeOfCup.Coffee };
            coffeeCup.AddItem(new liquid().WithDescription("Coffee"));
        }
    }
    class Cup {
        public string TypeOfCup { get; set;}
        public int numItems;
        public Cup() {
            this.numItems = 0;
        }
        public void AddItem() {
            numItems  = 1;
        }
    }
    class liquid {
        public string description;
        public liquid() {

        }
        public void WithDescription(string description) {
            this.description = description;
        }
    }
}

I receive 2 errors:

  1. The name 'TypeOfCup' does not exist in the current context
  2. Operator '.' cannot be applied to operand of type 'void'

I am a beginner in C# so I apologize if this is a silly problem. Any insight or tips on how to fix these errors/code would be appreciated.

CodePudding user response:

namespace Sample
{
    enum TypeOfCup
    {
        Coffee,
    }

    class Program
    {
        static void Main(string[] args)
        {
            Cup coffeeCup = new() { TypeOfCup = TypeOfCup.Coffee };
            coffeeCup.AddItem(new liquid().WithDescription("Coffee"));
        }
    }

    class Cup
    {
        public TypeOfCup TypeOfCup { get; set; }
        public List<liquid> Liquids { get; set; } = new List<liquid>();

        public void AddItem(liquid liquid)
        {
            Liquids.Add(liquid);
        }
    }

    class liquid
    {
        public string description = "";

        public liquid()
        {

        }
        public liquid(string description)
        {
            this.description = description;
        }
        public liquid WithDescription(string description)
        {
            this.description = description;
            return this;
        }
    }
}

If you are allowed to change your code in de Main function, then you probably should replace coffeeCup.AddItem(new liquid().WithDescription("Coffee")) with coffeeCup.AddItem(new liquid("Coffee"))

CodePudding user response:

You are supposed to implement TypeOfCup as enum:

enum TypeOfCup {
    None,
    Coffee,
    Tea,
    Chocolate
}

The enum constants are always accessed through the enum name: TypeOfCup.Coffee.

While this is the most idiomatic implementation, you could also implement TypeOfCup as constants or static properties:

static class TypeOfCup
{
    public const int None = 0;
    public const int Coffee = 1;
    public const int Tea = 2;
    public const int Chocolate = 3;
}

or

static class TypeOfCup
{
    public static int None => 0;
    public static int Coffee => 1;
    public static int Tea => 2;
    public static int Chocolate => 3;
}

Another problem is

Error CS1501 No overload for method 'AddItem' takes 1 arguments

You are supposed to pass it an object of type liquid, therefore AddItem must have a corresponding parameter. This means that a Cup must contain a collection storing those liquids instead of just a counter. Something like this:

class Cup
{
    public TypeOfCup TypeOfCup { get; set; }

    public List<liquid> Liquids { get; } = new List<liquid>();

    public void AddItem(liquid liquid)
    {
        Liquids.Add(liquid);
    }
}

The error

Error CS1503 Argument 1: cannot convert from 'void' to '[...].liquid'

comes from the fact the method WithDescription does not return an instance of the liquid. It should be implemented as

class liquid
{
    public string Description { get; set; }

    public liquid WithDescription(string description)
    {
        Description = description;
        return this;
    }
}

This allows calling coffeeCup.AddItem(new liquid().WithDescription("Coffee")); where the liquid that was added a description is passed to AddItem after a call to WithDescription.

Note that returning this is a common practice that allows chaining methods like this:

coffeeCup.AddItem(
    new liquid()
        .WithDescription("Coffee")
        .WithTemperature(50)
        .WithQuantity(20.5)
);

The same instance of the liquid is passed through this call chain.

According to the usual C# naming conventions class name must be in CamelCase. Therefore the liquid class should be named Liquid; however, that's an error in the starter code given to you.

CodePudding user response:

I suppose each cup has a type and a description.

class Program
{
    static void Main(string[] args)
    {
        Cup coffeeCup = new Cup() { TypeOfCup = TypeOfCup.Coffee };
        coffeeCup.AddItem(new liquid().WithDescription("Coffee"));
    }
}

class Cup
{
    public TypeOfCup TypeOfCup { get; set; }
    private liquid Liquid { get; set; }

    public void AddItem(liquid liquid)
    {
        this.Liquid = liquid;
    }
}

class liquid
{
    private string Description { get; set; }

    public liquid WithDescription(string description)
    {
        this.Description = description;
        return this;
    }
}

enum TypeOfCup
{
    Coffee,
    Tea,
}
  •  Tags:  
  • c#
  • Related