0% found this document useful (0 votes)
22 views3 pages

Method Save To File: Bool String If New Foreach in String

The document describes methods for saving, loading, and parsing inventory data from a file. The SaveToFile method saves each inventory item as a line in the file by calling InventoryToLine to format the data and writing it. LoadFromFile parses the file line-by-line using ParseInventory, adding valid items to a collection. PopulateList adds sample inventory items to the collection. InventoryToLine formats an item's fields into a string separated by pipes. ParseInventory splits a string on pipes and assigns the values to an inventory item object's properties.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Method Save To File: Bool String If New Foreach in String

The document describes methods for saving, loading, and parsing inventory data from a file. The SaveToFile method saves each inventory item as a line in the file by calling InventoryToLine to format the data and writing it. LoadFromFile parses the file line-by-line using ParseInventory, adding valid items to a collection. PopulateList adds sample inventory items to the collection. InventoryToLine formats an item's fields into a string separated by pipes. ParseInventory splits a string on pipes and assigns the values to an inventory item object's properties.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Method Save to File

bool SaveToFile(string Filepath)


{
if (File.Exists(Filepath))
File.Delete(Filepath);
System.IO.StreamWriter file = new System.IO.StreamWriter(Filepath);

foreach (Inventory item in _colInventory)


{
string Line = InventoryToLine(item);
file.WriteLine(Line);
}

file.Close();

return true;

Method Inventory to Line


string InventoryToLine(Inventory item)
{
StringBuilder sb = new StringBuilder();
sb.Append(item.Id);
sb.Append('|');
sb.Append(item.Name);
sb.Append('|');
sb.Append(item.SerialNumber);
sb.Append('|');
sb.Append(item.ImagePath);

Debug.WriteLine(sb.ToString());

return sb.ToString ();


}

Method Read from File

bool LoadFromFile(string Filepath)


{
if (!File.Exists(Filepath))
return false;

string line;
System.IO.StreamReader file = new System.IO.StreamReader(Filepath);

while ((line = file.ReadLine()) != null)


{
Inventory item;

if (ParseInventory(line, out item))


_colInventory.Add(item);
}
file.Close();

return true;

Method Parse Inventory


bool ParseInventory(string Text, out Inventory item)//utk splitkan data by bar
{
item = new Inventory();

string[] arr = Text.Split('|');


if (arr.Length != 4)
return false;

item.Id = arr[0];
item.Name = arr[1];
item.SerialNumber = arr[2];
item.ImagePath = arr[3];

return true;
}

Method Populate List

void PopulateList()
{

Inventory first = new Inventory();


first.Id ="1";
first.Name ="First Item Name";
first.SerialNumber ="First Item Serial Number";
first.ImagePath = "C:\\Users\\user\\Desktop\\ibuDeeja.jpg";
_colInventory.Add(first);

Inventory second = new Inventory();


second.Id = "2";
second.Name = "Second Item Name";
second.SerialNumber = "Second Item Serial Number";
second.ImagePath = "C:\\Users\\user\\Pictures\\2013-02-24\\003.jpg";
_colInventory.Add(second);

Inventory third = new Inventory();


third.Id = "3";
third.Name = "Third Item Name";
third.SerialNumber = "Third Item Serial Number";
third.ImagePath = "C:\\Users\\user\\Pictures\\2013-02-24\\011.jpg";
_colInventory.Add(third);

Method Parse Inventory

bool ParseInventory(string Text, out Inventory item)//utk splitkan data by bar


{
item = new Inventory();

string[] arr = Text.Split('|');


if (arr.Length != 4)
return false;

item.Id = arr[0];
item.Name = arr[1];
item.SerialNumber = arr[2];
item.ImagePath = arr[3];

return true;
}

Method Inventory to Line


string InventoryToLine(Inventory item)
{
StringBuilder sb = new StringBuilder();
sb.Append(item.Id);
sb.Append('|');
sb.Append(item.Name);
sb.Append('|');
sb.Append(item.SerialNumber);
sb.Append('|');
sb.Append(item.ImagePath);

Debug.WriteLine(sb.ToString());

return sb.ToString ();


}

You might also like