Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- namespace Task2
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Customer> listCustomers = new List<Customer>();
- bool exit = false;
- do
- {
- Console.WriteLine("Choose an option:");
- Console.WriteLine("1 - Add some customers");
- Console.WriteLine("2 - See what we have in the list");
- Console.WriteLine("3 - Delete a customer");
- Console.WriteLine("4 - Write all customer info in a file");
- Console.WriteLine("5 - Write data in a file");
- Console.WriteLine("6 - Read data from a file");
- Console.WriteLine("7 - Exit");
- if (int.TryParse(Console.ReadLine(), out int option))
- {
- switch (option)
- {
- case 1:
- AddToListOfCustomers(listCustomers);
- break;
- case 2:
- PrintListOfCustomers(listCustomers);
- break;
- case 3:
- DeleteACustomer(listCustomers);
- break;
- case 4:
- WriteCustomersInfo(listCustomers);
- break;
- case 5:
- WriteTextDataInMyDocs();
- break;
- case 6:
- ReadTextDataFromMyDocs();
- break;
- case 7:
- exit = true;
- break;
- }
- }
- }
- while (!exit);
- Console.WriteLine("You are out!");
- }
- static void AddToListOfCustomers(List<Customer> listCustomers)
- {
- int id = listCustomers.Count > 0 ? listCustomers.Select(c => c.Id).Max() : 0;
- Console.WriteLine("How many customers?");
- int customers = Int32.Parse(Console.ReadLine());
- for (int i = 0; i < customers; i++)
- {
- Customer currentCustomer = new Customer();
- currentCustomer.Id = ++id;
- Console.WriteLine("Name of client №{0}", i + 1);
- currentCustomer.Name = Console.ReadLine();
- Console.WriteLine("Address of client №{0}", i + 1);
- currentCustomer.Address = Console.ReadLine();
- Console.WriteLine("Phone of client №{0}", i + 1);
- currentCustomer.Phone = Console.ReadLine();
- listCustomers.Add(currentCustomer);
- }
- }
- static void PrintListOfCustomers(List<Customer> list)
- {
- foreach (Customer customer in list)
- {
- Console.WriteLine($"-Id {customer.Id}, Name {customer.Name}, Address {customer.Address}, Phone {customer.Phone}");
- }
- }
- static void DeleteACustomer(List<Customer> list)
- {
- Console.WriteLine("Select an Id to delete");
- int id = int.Parse(Console.ReadLine());
- Customer customerToRemove = list.Where(c => c.Id == id).FirstOrDefault();
- list.Remove(customerToRemove);
- }
- static void WriteTextDataInMyDocs()
- {
- Console.WriteLine("Select a name for the file:");
- string fileName = Console.ReadLine();
- string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- var sw = new StreamWriter($@"{directory}\{fileName}.txt");
- using (sw)
- {
- Console.WriteLine("Press any key to begin and Esc to stop");
- Console.ReadKey(true);
- while (Console.ReadKey(true).Key != ConsoleKey.Escape)
- {
- sw.WriteLine(Console.ReadLine());
- }
- }
- sw.Close();
- }
- static void ReadTextDataFromMyDocs()
- {
- Console.WriteLine("Select a file to read: ");
- string fileName = Console.ReadLine();
- string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- var sr = new StreamReader($@"{directory}\{fileName}.txt");
- using (sr)
- {
- Console.WriteLine("Content of file \n");
- Console.WriteLine(sr.ReadToEnd());
- }
- }
- static void WriteCustomersInfo(List<Customer> customers)
- {
- Console.WriteLine("Select a name for the file:");
- string fileName = Console.ReadLine();
- string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- var sw = new StreamWriter($@"{directory}\{fileName}.txt");
- using (sw)
- {
- foreach (var customer in customers)
- {
- sw.WriteLine($"-Id {customer.Id}, Name {customer.Name}, Address {customer.Address}, Phone {customer.Phone}");
- }
- }
- sw.Close();
- Console.WriteLine($@"Customer info recorded in {directory}\{fileName}.txt!");
- }
- }
- public class Customer
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string Phone { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement