C# Adding data from a class to List and saving the data to a File

  • Thread starter Thread starter Frank_Furter84
  • Start date Start date
F

Frank_Furter84

Guest
Hi every one,

For a couple of hours i have been struggling with this piece of code:

==============================================================================

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
public interface Item
{
void printData();
}

public class Product : Item
{
public string name;
public int quantity;

public void printData() { }
}

public class Milk : Product
{
public string name = "Milk";
public int quantity = 2;
public int cal;
public int protein;
public string softness;

public void printData()
{
Console.WriteLine("{0} has {1} cal and {2} protein", name, cal, protein);
Console.WriteLine("Softness = " + softness);
}
}

public class Potato : Product
{
public string name = "Potato";
public int quantity = 5;
public int cal;
public int protein;
public string hardness;

public void printData()
{
Console.WriteLine("{0} has {1} cal and {2} protein", name, cal, protein);
Console.WriteLine("Hardness = " + hardness);
}
}

static void Main(string[] args)
{
Product product = new Product();

Milk milk = new Milk();
milk.protein = 8;
milk.cal = 350;
milk.softness = "Soft";
product.printData();
milk.printData();

Potato potato = new Potato();
potato.protein = 12;
potato.cal = 25;
potato.hardness = "Hard";
product.printData();
potato.printData();

List<string> items = new List<string>();
items.Add();

foreach(object Item in items)
{
Console.WriteLine();
//Console.WriteLine(new Potato());
}

/*foreach (string item in items)
{
Console.WriteLine(item);
}

File.WriteAllText("Text.txt",items);*/

Console.ReadLine();
}
}
}

==============================================================================

So i want to get the data from name, quantity, cal, protein, hardness and softness and save it to a file.

Basically that's a homework i was given which i never made in time, and i was revisiting old stuff and found that.

==============================================================================

The homework says:

Describe with classes two items of your choice.

Expand one with his successor.

Write an interface with one method to support all three classes.

Create a list of the interface and save one of the three classes in it.

Use foreach to browse the list items and invoke the common method.

Save the object to a file and read them back.

==============================================================================

I would also like to see how some one else would do it! :)

If you have time, of course . :)

And if you can give me a good explanation i will be really grateful!

The interface is a must! Just cause i want to learn how to use them.

Any tips are welcomed. :)

I am a beginner so please don't bully me too much! :P

Continue reading...
 
Back
Top