Skip to content

Instantly share code, notes, and snippets.

@d630
Last active July 13, 2018 02:19
Show Gist options
  • Save d630/f0c0052691d9a82a2742bbe23d8f6f25 to your computer and use it in GitHub Desktop.
Save d630/f0c0052691d9a82a2742bbe23d8f6f25 to your computer and use it in GitHub Desktop.
Tag 13: Baum
namespace Baum
{
public class Baum
{
private int nr;
private double x;
private double y;
private short sta;
private float kro;
private string art;
public Baum(int nr, double x, double y, short sta, float kro, string art)
{
this.nr = nr;
this.x = x;
this.y = y;
this.sta = sta;
this.kro = kro;
this.art = art;
}
public int Nr { get; set; }
public double X { get; set; }
public double Y { get; set; }
public short Sta { get; set; }
public float Kro { get; set; }
public string Art { get; set; }
public override string ToString()
{
return this.nr.ToString() + ";" + this.x.ToString() + ";" + this.y.ToString() + ";" + this.sta.ToString() + ";" + this.kro.ToString() + ";" + this.art;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
namespace Baum
{
public class Kataster
{
private List<Baum> liste = new List<Baum>();
public void clearRecords()
{
this.liste.Clear();
}
public void printRecords()
{
foreach (Baum b in this.liste)
Console.WriteLine(b.ToString());
}
public void writeKataster()
{
FileStream fs = null;
StreamWriter sw = null;
try
{
fs = new FileStream("Baumliste.txt", FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs);
foreach (Baum b in liste)
sw.WriteLine(b.ToString());
}
catch (FileNotFoundException fnfeEx)
{
Console.WriteLine(fnfeEx.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
if (sw != null)
sw.Close();
if (fs != null)
fs.Close();
}
}
public void readKataster()
{
FileStream fs = null;
StreamReader sr = null;
try
{
fs = new FileStream("Baumliste.txt", FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
string[] fields;
while (sr.Peek() != -1)
{
fields = sr.ReadLine().Split(';');
this.liste.Add(new Baum(int.Parse(fields[0]), double.Parse(fields[1]), double.Parse(fields[2]), short.Parse(fields[3]), float.Parse(fields[4]), fields[5]));
}
}
catch (FileNotFoundException fnfeEx)
{
Console.WriteLine(fnfeEx.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
if (sr != null)
sr.Close();
if (fs != null)
fs.Close();
}
}
public void addRecord()
{
int nr;
double x;
double y;
short sta;
float kro;
string art;
try
{
Console.Write("Nr (int): ");
nr = int.Parse(Console.ReadLine());
Console.Write("X (double): ");
x = double.Parse(Console.ReadLine());
Console.Write("Y (double): ");
y = double.Parse(Console.ReadLine());
Console.Write("Sta (short): ");
sta = short.Parse(Console.ReadLine());
Console.Write("Kro (float): ");
kro = float.Parse(Console.ReadLine());
Console.Write("Art (string): ");
art = Console.ReadLine();
this.liste.Add(new Baum(nr, x, y, sta, kro, art));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
using System;
namespace Baum
{
class Program
{
static void Main(string[] args)
{
Kataster k = new Kataster();
k.readKataster();
k.printRecords();
//k.clearRecords();
k.addRecord();
//k.addRecord();
k.writeKataster();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment