Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. namespace Task2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<Customer> listCustomers = new List<Customer>();
  14.             bool exit = false;
  15.  
  16.             do
  17.             {
  18.                 Console.WriteLine("Choose an option:");
  19.                 Console.WriteLine("1 - Add some customers");
  20.                 Console.WriteLine("2 - See what we have in the list");
  21.                 Console.WriteLine("3 - Delete a customer");
  22.                 Console.WriteLine("4 - Write all customer info in a file");
  23.                 Console.WriteLine("5 - Write data in a file");
  24.                 Console.WriteLine("6 - Read data from a file");
  25.                 Console.WriteLine("7 - Exit");
  26.  
  27.                 if (int.TryParse(Console.ReadLine(), out int option))
  28.                 {
  29.                     switch (option)
  30.                     {
  31.  
  32.                         case 1:
  33.                             AddToListOfCustomers(listCustomers);
  34.                             break;
  35.                         case 2:
  36.                             PrintListOfCustomers(listCustomers);
  37.                             break;
  38.                         case 3:
  39.                             DeleteACustomer(listCustomers);
  40.                             break;
  41.                         case 4:
  42.                             WriteCustomersInfo(listCustomers);
  43.                             break;
  44.                         case 5:
  45.                             WriteTextDataInMyDocs();
  46.                             break;
  47.                         case 6:
  48.                             ReadTextDataFromMyDocs();
  49.                             break;
  50.                         case 7:
  51.                             exit = true;
  52.                             break;
  53.  
  54.                     }
  55.                 }
  56.             }
  57.             while (!exit);
  58.             Console.WriteLine("You are out!");
  59.         }
  60.  
  61.  
  62.  
  63.         static void AddToListOfCustomers(List<Customer> listCustomers)
  64.         {
  65.             int id = listCustomers.Count > 0 ? listCustomers.Select(c => c.Id).Max() : 0;
  66.  
  67.             Console.WriteLine("How many customers?");
  68.             int customers = Int32.Parse(Console.ReadLine());
  69.  
  70.             for (int i = 0; i < customers; i++)
  71.             {
  72.                 Customer currentCustomer = new Customer();
  73.                 currentCustomer.Id = ++id;
  74.  
  75.                 Console.WriteLine("Name of client №{0}", i + 1);
  76.                 currentCustomer.Name = Console.ReadLine();
  77.  
  78.                 Console.WriteLine("Address of client №{0}", i + 1);
  79.                 currentCustomer.Address = Console.ReadLine();
  80.  
  81.                 Console.WriteLine("Phone of client №{0}", i + 1);
  82.                 currentCustomer.Phone = Console.ReadLine();
  83.  
  84.                 listCustomers.Add(currentCustomer);
  85.             }
  86.         }
  87.  
  88.         static void PrintListOfCustomers(List<Customer> list)
  89.         {
  90.             foreach (Customer customer in list)
  91.             {
  92.                 Console.WriteLine($"-Id {customer.Id}, Name {customer.Name}, Address {customer.Address}, Phone {customer.Phone}");
  93.             }
  94.         }
  95.  
  96.         static void DeleteACustomer(List<Customer> list)
  97.         {
  98.             Console.WriteLine("Select an Id to delete");
  99.             int id = int.Parse(Console.ReadLine());
  100.  
  101.             Customer customerToRemove = list.Where(c => c.Id == id).FirstOrDefault();
  102.             list.Remove(customerToRemove);
  103.         }
  104.  
  105.         static void WriteTextDataInMyDocs()
  106.         {
  107.             Console.WriteLine("Select a name for the file:");
  108.             string fileName = Console.ReadLine();
  109.             string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  110.  
  111.             var sw = new StreamWriter($@"{directory}\{fileName}.txt");
  112.  
  113.             using (sw)
  114.             {
  115.                 Console.WriteLine("Press any key to begin and Esc to stop");
  116.                 Console.ReadKey(true);
  117.  
  118.                 while (Console.ReadKey(true).Key != ConsoleKey.Escape)
  119.                 {
  120.                     sw.WriteLine(Console.ReadLine());
  121.                 }
  122.             }
  123.  
  124.             sw.Close();
  125.         }
  126.  
  127.         static void ReadTextDataFromMyDocs()
  128.         {
  129.             Console.WriteLine("Select a file to read: ");
  130.             string fileName = Console.ReadLine();
  131.             string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  132.  
  133.             var sr = new StreamReader($@"{directory}\{fileName}.txt");
  134.  
  135.             using (sr)
  136.             {
  137.                 Console.WriteLine("Content of file \n");
  138.                 Console.WriteLine(sr.ReadToEnd());
  139.             }
  140.         }
  141.  
  142.         static void WriteCustomersInfo(List<Customer> customers)
  143.         {
  144.             Console.WriteLine("Select a name for the file:");
  145.             string fileName = Console.ReadLine();
  146.             string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  147.  
  148.             var sw = new StreamWriter($@"{directory}\{fileName}.txt");
  149.  
  150.             using (sw)
  151.             {
  152.                 foreach (var customer in customers)
  153.                 {
  154.                     sw.WriteLine($"-Id {customer.Id}, Name {customer.Name}, Address {customer.Address}, Phone {customer.Phone}");
  155.                 }
  156.             }
  157.  
  158.             sw.Close();
  159.  
  160.             Console.WriteLine($@"Customer info recorded in {directory}\{fileName}.txt!");
  161.         }
  162.     }
  163.  
  164.     public class Customer
  165.     {
  166.         public int Id { get; set; }
  167.         public string Name { get; set; }
  168.         public string Address { get; set; }
  169.         public string Phone { get; set; }
  170.  
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement