using System;
using System.Collections.Generic;
using System.IO;
namespace Workplace
{
public class BlackList
{
///
/// List of forbidden black listed items
///
public IList Codes;
///
/// Default constructor
///
public BlackList()
{
Codes = new List();
}
///
/// Constructor from a list of black listed codes
///
///
public BlackList(IList codes)
{
Codes = codes;
}
///
/// Read a black listed codes from a file.
/// Each line of text contains one item/code.
///
/// File name
/// BlackList object with codes when successful or null
public static BlackList FromFile(string fileName)
{
try
{
IList codes = new List();
TextReader rdr = new StreamReader(fileName);
string line = rdr.ReadLine();
while (!string.IsNullOrEmpty(line))
{
codes.Add(line);
line = rdr.ReadLine();
}
return new BlackList(codes);
}
catch (Exception)
{
return null;
}
}
///
/// Write a black listed codes to a file.
/// Each line of text contains one item/code.
///
/// File name
/// true = successful
public bool ToFile(string fileName)
{
try
{
using (TextWriter wrtr = new StreamWriter(fileName))
{
foreach (var code in Codes)
{
wrtr.WriteLine(code);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
}
}