Запись в файл (дописать код) - C#
Формулировка задачи:
Ребят, помогите пожалуйста, есть код программы, нужно дописать case 4, чтобы можно было изменять введенную информацию (city, marka).
с get и set разобрался, нужен именно сам case 4
using System;
using System.IO;
using System.Linq;
namespace ConsoleApplication12
{
class M
{
private string _fio;
private string _city;
private string _marka;
public string fio { get { return _fio; } }
public string city { get { return _city; } set { _city = value; } }
public string marka { get { return _marka; } set { _marka = value; } }
public M(string marka, string city, string fio)
{
this._marka = marka;
this._city = city;
this._fio = fio;
}
public override string ToString()
{
return _fio + Environment.NewLine + _marka + Environment.NewLine + _city + Environment.NewLine + "$" + Environment.NewLine;
}
}
static class Program
{
static void Main(string[] args)
{
for (; ; )
{
Menu();
int res;
while (!int.TryParse(Console.ReadLine(), out res) || res > 4 | res < 1)
{
Console.Clear();
Menu();
}
switch (res)
{
case 1:
using (StreamWriter writer = new StreamWriter("baza.txt", true))
{
Console.Write("ФИО:");
string fio = Console.ReadLine();
Console.Write("Марка машины:");
string marka = Console.ReadLine();
Console.Write("Город:");
string city = Console.ReadLine();
M m = new M(marka, city, fio);
writer.Write(m.ToString());
}
break;
case 2:
if (!File.Exists("baza.txt"))
{
Console.WriteLine("Файл baza.txt не найден!");
continue;
}
var str = File.ReadAllText("baza.txt").Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string t in str)
{
Console.WriteLine(t);
}
break;
case 3:
Console.WriteLine("Марка машины");
string z = Console.ReadLine();
var tmp = File.ReadAllText("baza.txt").Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries).Where(m => m.Contains(z));
foreach (string t in tmp)
{
Console.WriteLine(t);
}
break;
case 4:
case 5:
return;
}
}
}
static void Menu()
{
Console.WriteLine("Меню");
Console.WriteLine("1.Добавить в файл");
Console.WriteLine("2.Просмотреть все записи");
Console.WriteLine("3.Поиск по марке");
Console.WriteLine("4.Выход");
}
}
}Решение задачи: «Запись в файл (дописать код)»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ConsoleApplication12
{
class M
{
private string _fio;
private string _city;
private string _marka;
public string fio { get { return _fio; } }
public string city { get { return _city; } set { _city = value; } }
public string marka { get { return _marka; } set { _marka = value; } }
public M(string marka, string city, string fio)
{
this._marka = marka;
this._city = city;
this._fio = fio;
}
public override string ToString()
{
return _fio + Environment.NewLine + _marka + Environment.NewLine + _city + Environment.NewLine + $ + Environment.NewLine;
}
}
static class Program
{
static void Main(string[] args)
{
Liststring spisok = new Liststring();
Liststring markaList = new Liststring();
Liststring cityList = new Liststring();
bool vivod = false;
for (; ; )
{
Menu();
int res;
while (!int.TryParse(Console.ReadLine(), out res) res 4 res 1)
{
Console.Clear();
Menu();
}
switch (res)
{
case 1
using (StreamWriter writer = new StreamWriter(baza.txt, true))
{
Console.Write(ФИО);
string fio = Console.ReadLine();
Console.Write(Марка машины);
string marka = Console.ReadLine();
Console.Write(Город);
string city = Console.ReadLine();
M m = new M(marka, city, fio);
writer.Write(m.ToString());
}
break;
case 2
int numberPeople = 1;
Console.WriteLine();
Console.WriteLine(numberPeople+.);
numberPeople++;
if (vivod == false)
{
if (!File.Exists(baza.txt))
{
Console.WriteLine(Файл baza.txt не найден!);
continue;
}
var str = File.ReadAllText(baza.txt).Split(new char[] { 'r' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string t in str)
{
spisok.Add(t);
if (t != n$)
Console.Write(t);
else
{
Console.WriteLine();
Console.WriteLine();
Console.Write(numberPeople + .);
numberPeople++;
}
}
Console.WriteLine();
Console.WriteLine();
for (int i = 1; i spisok.Count; i++)
{
if (i == 1)
markaList.Add(spisok[i].Substring(1, spisok[i].Length - 1));
if (spisok[i] == n$)
{
i = i + 2;
markaList.Add(spisok[i].Substring(1, spisok[i].Length - 1));
}
}
for (int i = 2; i spisok.Count; i++)
{
if (i == 2)
cityList.Add(spisok[i].Substring(1, spisok[i].Length - 1));
if (spisok[i] == n$)
{
i = i + 3;
cityList.Add(spisok[i].Substring(1, spisok[i].Length - 1));
}
}
vivod = true;
}
else
{
spisok[1] = n + spisok[1];
spisok[2] = n + spisok[2];
for (int i = 0; i spisok.Count;i++ )
{
if (spisok[i] != n$)
Console.Write(spisok[i]);
else
{
Console.WriteLine();
Console.WriteLine();
Console.Write(numberPeople + .);
numberPeople++;
}
}
Console.WriteLine();
Console.WriteLine();
}
break;
case 3
Console.WriteLine(Марка машины);
string z = Console.ReadLine();
var tmp = File.ReadAllText(baza.txt).Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries).Where(m = m.Contains(z));
foreach (string t in tmp)
{
Console.WriteLine(t);
}
break;
case 4
Console.WriteLine(Введите номер человека);
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Введите новую машину);
string newCar = Console.ReadLine();
Console.WriteLine(Введите новый город);
string newCity = Console.ReadLine();
markaList[number - 1] = newCar;
cityList[number - 1] = newCity;
Замена машин в главном списке
int numberCar = 1;
for (int i = 1; i spisok.Count; i++)
{
if (i == 1)
spisok[i] = markaList[0];
if (spisok[i] == n$)
{
i = i + 2;
spisok[i] = n+markaList[numberCar];
numberCar++;
}
}
int numberCity=1;
for (int i = 2; i spisok.Count; i++)
{
if (i == 2)
spisok[i] = cityList[0];
if (spisok[i] == n$)
{
i = i + 3;
spisok[i] = n+cityList[numberCity];
numberCity++;
}
}
var tpm = File.ReadAllText(baza.txt).Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries).Where(m = m.Contains(p));
foreach (string t in tpm)
{
Console.Write(Марка машины);
string u = Console.ReadLine();
}
break;
case 5
return;
}
}
}
static void Menu()
{
Console.WriteLine(Меню);
Console.WriteLine(1.Добавить в файл);
Console.WriteLine(2.Просмотреть все записи);
Console.WriteLine(3.Поиск по марке);
Console.WriteLine(4.Изменить);
Console.WriteLine(5.Выход);
}
}
}