Justin L Justin L - 2 months ago 15
C# Question

C# displaying objects in List from a different class in Main

I am trying to print the items in the Lists to the console, and I am not sure if I am missing something blatantly obvious but I cannot figure out what to do to achieve this. My programming class from first semester basically taught me nothing and I have been learning bits and pieces on my own, so I am clueless as to how to do a lot of things. Any help would be greatly appreciated.

public class VendingMachine
{
public List<Snack> snax = new List<Snack>();
public List<Drink> drinx = new List<Drink>();

public void displayInventory()
{
foreach (Snack s in snax)
{
Console.WriteLine("test");
Console.WriteLine(s.ToString());
}

Console.WriteLine("\n");

foreach (Drink d in drinx)
{
Console.WriteLine(d.ToString());
}
}
}

public abstract class Items
{
public string name;
public int calories;

public Items(string name, int calories)
{
this.name = name;
this.calories = calories;
}

public override string ToString()
{
return (name + " " + calories);
}
}

public class Drink : Items
{
int flOz;

public Drink(string name, int calories, int flOz) : base(name, calories)
{
this.name = name;
this.calories = calories;
this.flOz = flOz;
}
}

public class Snack : Items
{
int oz;

public Snack(string name, int calories, int oz) : base(name, calories)
{
this.name = name;
this.calories = calories;
this.oz = oz;
}
}

public class Program
{
public static void Main(string[] args)
{
VendingMachine Vend1 = new VendingMachine();

Vend1.snax.Add(new Snack("Cookies", 125, 8));
Vend1.snax.Add(new Snack("Chips", 100, 8));
Vend1.snax.Add(new Snack("Nuts & Berries", 80, 8));

Vend1.drinx.Add(new Drink("Soda", 180, 12));
Vend1.drinx.Add(new Drink("Water", 0, 12));
Vend1.drinx.Add(new Drink("Juice", 90, 12));

var invoke = new VendingMachine();
invoke.displayInventory();
}
}


}




Update

I got the list to print, but I need to know how to print not only the item and calories, but also oz/flOz in the inherited classes. How might I do that? I looked at the assignment and it says to tweak the to string override but I am not sure what to put.

public class Program
{
public static void Main(string[] args)
{
VendingMachine Vend1 = new VendingMachine();

Vend1.snax.Add(new Snack("Cookies", 125, 8));
Vend1.snax.Add(new Snack("Chips", 100, 8));
Vend1.snax.Add(new Snack("Nuts", 80, 8));

Vend1.drinx.Add(new Drink("Soda", 180, 12));
Vend1.drinx.Add(new Drink("Water", 0, 12));
Vend1.drinx.Add(new Drink("Juice", 90, 12));

Vend1.displayInventory();
}
}

Answer

In order to output the oz and flOz, and to comply the with requirements, you need to override .ToString() in both Drink and Snack.

public class Drink : Items
{
    int flOz;

    public Drink(string name, int calories, int flOz) : base(name, calories)
    {
        this.name = name;
        this.calories = calories;
        this.flOz = flOz;
    }

    public override string ToString()
    {
        return base.ToString() + "    " + this.flOz;
    }
}

public class Snack : Items
{
    int oz;

    public Snack(string name, int calories, int oz) : base(name, calories)
    {
        this.name = name;
        this.calories = calories;
        this.oz = oz;
    }

    public override string ToString()
    {
        return base.ToString() + "    " + this.oz;
    }
}