Hello,
below is the program written to enter the details of grocery products purchased and find the totalamount.
namespace grocery_billing
{
public class program
{
public static void Main()
{
int choice, key;
float Price, TotalCost;
string ItemDescription;
DateTime Date;
File.Create(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "grocery billing.txt");
Console.WriteLine("Press 1 to read data from file");
Console.WriteLine("press 2 to write data to file");
Console.WriteLine("press 3 to enter data");
Console.WriteLine("press 4 to exit");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\ramya\desktop\grocery billing.txt");
System.Console.WriteLine("Contents of grocery billing.txt :");
foreach (string line in lines)
{
Console.Write(line);
Console.Write(",", line);
}
break;
}
case 2:
{
break;
}
case 3:
{
do
{
Console.Write("Enter the item:");
ItemDescription = Console.ReadLine();
Console.Write("Enter the Date of purchse:");
Date = DateTime.Parse(Console.ReadLine());
Console.Write("Enter the cost:");
Price = float.Parse(Console.ReadLine());
TotalCost += Price;
TotalCost = float.Parse(console.ReadLine();
StreamWriter fileWriter = new StreamWriter("grocery billing.txt");
fileWriter.Close();
FileStream fs = new FileStream(@"C:\Users\ram\desktop\grocery billing.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("the item:" + ItemDescription);
sw.WriteLine("date of purchase:" + Date);
sw.WriteLine("the amount:" + Price);
sw.WriteLine("the total amount:" +TotalCost);
sw.Flush();
sw.Close();
Console.WriteLine("If you want to enter more data press 3");
key = int.Parse(Console.ReadLine());
} while (key == 3);
break;
}
}
}
}
}
1) can any one help me how to write a different function to write the data into the text file. As itemdescription , date and Price are local to do while loop i can't write the filestream outside the loop.
2)is there any way to display the Totalcost only once i.e., before exiting the application.
3)i am able to read the data from grocery billing.txt file .but i want to read the data as key value pairs is it possible?help me.