praktikum1/vr21/Program.cs

104 lines
3.1 KiB
C#
Raw Normal View History

2017-02-01 16:06:52 +02:00
using System;
2017-02-01 17:35:17 +02:00
using System.Collections.Generic;
2017-02-01 18:08:40 +02:00
using System.Linq;
2017-02-01 17:21:58 +02:00
using System.Xml.Linq;
2017-02-01 17:35:17 +02:00
using System.Xml.Serialization;
2017-02-07 01:02:19 +02:00
using System.IO;
using System.Xml;
2017-02-01 16:06:52 +02:00
namespace vr21
{
2017-02-07 01:02:19 +02:00
public class Recipe
2017-02-01 17:35:17 +02:00
{
public string Name;
2017-02-01 18:08:40 +02:00
public string Description;
2017-02-01 17:35:17 +02:00
public List<Ingredient> Ingredients;
2017-02-01 18:08:40 +02:00
public List<string> Steps;
2017-02-01 17:35:17 +02:00
2017-02-07 01:02:19 +02:00
public Recipe()
2017-02-01 17:35:17 +02:00
{
Ingredients = new List<Ingredient>();
2017-02-01 18:08:40 +02:00
Steps = new List<string>();
2017-02-01 17:35:17 +02:00
}
}
public class Ingredient
{
public string Name;
2017-02-07 01:02:19 +02:00
public int Amount;
2017-02-01 17:35:17 +02:00
public string Unit;
}
2017-02-01 16:06:52 +02:00
class Program
{
2017-02-07 01:02:19 +02:00
// private static string _xmllocation = @"..\..\..\XMLRecipe.xml";
private static string _xmllocation = @"XMLRecipe.xml";
public static List<Recipe> Recipes = new List<Recipe>();
2017-02-01 16:06:52 +02:00
static void Main(string[] args)
2017-02-01 17:35:17 +02:00
{
2017-02-07 01:02:19 +02:00
Ingredient kartul = new Ingredient() {Name = "Kartul", Amount = 500, Unit = "g"};
Recipe r = new Recipe() {Name="Supp"};
2017-02-01 17:35:17 +02:00
r.Ingredients.Add(kartul);
r.Ingredients.Add(kartul);
2017-02-01 18:08:40 +02:00
r.Steps.Add("Koori");
r.Steps.Add("Keeda");
2017-02-07 01:02:19 +02:00
var l = Recipes;
l.Add(r);
2017-02-01 17:35:17 +02:00
l.Add(r);
2017-02-07 01:02:19 +02:00
r = new Recipe() {Name="kala"};
2017-02-01 17:35:17 +02:00
l.Add(r);
2017-02-07 01:02:19 +02:00
2017-02-01 17:35:17 +02:00
XmlSerializer x = new XmlSerializer(l.GetType());
2017-02-07 01:02:19 +02:00
using (var fs = new FileStream(_xmllocation, FileMode.Create)) {
x.Serialize(fs, l);
}
2017-02-01 17:35:17 +02:00
//writeReceipesInformation();
2017-02-07 01:02:19 +02:00
readRecipesInformation();
System.Environment.Exit(1);
2017-02-01 18:08:40 +02:00
string s;
2017-02-07 01:02:19 +02:00
while ((s = Console.ReadLine()) != null)
2017-02-01 18:08:40 +02:00
{
var arguments = s.Split(' ');
var command = arguments[0].Trim();
switch (command.ToLower())
{
case "list":
Console.WriteLine("Tere");
break;
default:
Console.WriteLine("No command");
break;
}
}
2017-02-01 17:21:58 +02:00
}
2017-02-01 17:03:44 +02:00
2017-02-07 01:02:19 +02:00
static void readRecipesInformation() {
var x = new XmlSerializer(typeof(List<Recipe>));
FileStream fs = new FileStream(_xmllocation, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
var l = (List<Recipe>)x.Deserialize(reader);
foreach (var r in l) {
Console.WriteLine(r.Name);
}
}
2017-02-01 17:21:58 +02:00
static void writeReceipesInformation()
{
XDocument xdoc = XDocument.Load(_xmllocation);
var allReceipes = xdoc.Descendants("Recipe");
2017-02-07 01:02:19 +02:00
Console.WriteLine(allReceipes.Count());
2017-02-01 17:21:58 +02:00
foreach (var receipe in allReceipes)
2017-02-01 17:03:44 +02:00
{
2017-02-01 17:21:58 +02:00
Console.WriteLine(receipe.Element("Name").Value);
foreach (var ingredient in receipe.Element("Ingredients").Elements())
2017-02-01 17:03:44 +02:00
{
2017-02-01 17:21:58 +02:00
Console.WriteLine($" {ingredient.Element("Name").Value}: {ingredient.Element("Amount").Value}{ingredient.Element("Unit").Value}");
2017-02-01 17:03:44 +02:00
}
}
2017-02-01 16:06:52 +02:00
}
}
}