Answer to Question #50521 in C# for pooja

Question #50521
Elina has started developing the application. She needs to ensure that the application meets the
following requirements:
When the application executes for the first time, it should perform the following tasks:
Display a message to the user to specify the path of the file in which the customer details are to
be stored.
Create the customer details file at the user-defined location.
Create a configuration file and store the path of the customer details file in the configuration file.
This configuration file needs to be used by the application to retrieve the location of the customer
details file.
The user should be able to save data to the customer details file. Write the code that Elina
should use to save data to the customer details file.
The user should be able to view the data stored in the customer details file. Write the code that
Elina should use to read the data stored in the customer details file and display it to the user.
1
Expert's answer
2015-01-30T03:36:00-0500
Program.cs
using System;
using System.IO;
using System.Text;
namespace task
{
class Program
{
static void Main(string[] args)
{
do
{
Console.Clear();
Console.WriteLine("Choose appropriate line (ESC-EXIT):");
switch (MyPatterns.Singleton.CreateMenu.ShowMenu("Change the path to file", "Show Content", "Add data"))
{
case 0:
Console.WriteLine("Please enter the path to the folder with Cus_detail.txt:");
string path = Console.ReadLine();
if (!Directory.Exists(path))
{
Console.WriteLine("The folder doesn't exist! Press any key to continue...");
Console.ReadKey();
continue;
}
StreamWriter swtr = new StreamWriter(Directory.GetCurrentDirectory() + @"\conf_file.txt", false, Encoding.GetEncoding(1251));
swtr.Write(path + @"\Cus_detail.txt");
swtr.Close();
if (!File.Exists(path + @"\Cus_detail.txt"))
{
FileStream fs = new FileStream(path + @"\Cus_detail.txt", FileMode.Create);
fs.Close();
// Console.WriteLine(path + @"\Cus_detail.txt" + " was successfully created");
Console.WriteLine("Please enter the path to the folder with Cus_detail.txt:");
path = Console.ReadLine();
if (!Directory.Exists(path))
{
Console.WriteLine("The folder doesn't exist! Press any key to continue...");
Console.ReadKey();
continue;
}
Console.ReadKey();
continue;
}
Console.WriteLine("Saved! Press any key to continue...");
Console.ReadKey();
break;
case 1:
if (!File.Exists(Directory.GetCurrentDirectory() + @"\conf_file.txt"))
{

StreamWriter strw = new StreamWriter(Directory.GetCurrentDirectory() + @"\conf_file.txt", false);
strw.Close();
Console.WriteLine("Configuration file is empty! Press any key...");
Console.ReadKey();
continue;
}
StreamReader strd = new StreamReader(Directory.GetCurrentDirectory() + @"\conf_file.txt");
string file_path = strd.ReadToEnd();
strd.Close();
if (File.Exists(file_path))
{
strd = new StreamReader(file_path);
Console.WriteLine("The content from file:");
Console.WriteLine("/////////////////////////////////////////////////////////");
Console.WriteLine(strd.ReadToEnd());
Console.WriteLine("/////////////////////////////////////////////////////////");
strd.Close();
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
else
{
Console.WriteLine(file_path + " Doesn't exist. Press any key to continue...");
Console.ReadKey();
continue;
}
break;
case 2:
if (!File.Exists(Directory.GetCurrentDirectory() + @"\conf_file.txt"))
{
File.Create(Directory.GetCurrentDirectory() + @"\conf_file.txt");
Console.WriteLine("Configuration file is empty! Press any key...");
Console.ReadKey();
continue;
}
StreamReader strd1 = new StreamReader(Directory.GetCurrentDirectory() + @"\conf_file.txt");
string file_path1 = strd1.ReadToEnd();
strd1.Close();
if (File.Exists(file_path1))
{
StreamWriter swr = new StreamWriter(file_path1, true, Encoding.GetEncoding(1251));
Console.WriteLine("Please enter the data to add:");
swr.Write(Console.ReadLine());
swr.Close();
Console.WriteLine("Successfully saved. Press any key to continue...");
Console.ReadKey();
}
else
{
Console.WriteLine("Firstly Use \"Settings\" menu to fill config file.");
Console.ReadKey();
}
break;
default: return;
}
} while (true);
}
}
}

Singleton.cs
using System;
namespace MyPatterns
{
class Singleton
{
private static Singleton Menu;
public static Singleton CreateMenu
{
get
{
if (Menu == null) Menu = new Singleton();
return Menu;
}
}
private Singleton()
{
}
public int ShowMenu(params string[] lines)
{
if (lines.Length < 2) return -1;
int starPos = 0;
for (int i = 0; i < lines.Length; i++)
if (lines[i].Length > starPos) starPos = lines[i].Length;
starPos += 5;
int upLinePos = Console.CursorTop + 1;
int lowLinePos = upLinePos + lines.Length - 1;
int curLine = upLinePos;
Console.WriteLine();
foreach (var line in lines)
{
Console.WriteLine(line);
}
Console.SetCursorPosition(starPos, upLinePos);
Console.Write("*");
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.UpArrow:
Console.SetCursorPosition(starPos, curLine);
Console.Write(" ");
if (curLine == upLinePos) curLine = lowLinePos;
else curLine--;
Console.SetCursorPosition(starPos, curLine);
Console.Write("*");
break;
case ConsoleKey.DownArrow:
Console.SetCursorPosition(starPos, curLine);
Console.Write(" ");
if (curLine == lowLinePos) curLine = upLinePos;
else curLine++;
Console.SetCursorPosition(starPos, curLine);
Console.Write("*");
break;
case ConsoleKey.Escape:
Console.SetCursorPosition(0, lowLinePos + 2);
return -1;
}
} while (key.Key != ConsoleKey.Enter);
Console.SetCursorPosition(0, lowLinePos + 2);
return curLine - upLinePos;
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS