I am new to c # and I have gaps in programming in it.
I need your help in implementing the program logic.
I am trying to store a value in a list that will be added after the entered command in the console.
The value is added, but when I repeat the command, the new value is not written to the list, it disappears.
public interface IDataSource{
MainRecord Save(MainRecord record);
MainRecord Get(int id);
bool Delete(int id);
List<MainRecord> GetAll();
}
abstract public class MainRecord{
public int id;
public string address;
public string activity;
public string data_1;
public string data_2;
}
public class MemoryDataSource : IDataSource{
private List<MainRecord> record = new List<MainRecord>();
public MainRecord Save(MainRecord records){
records.id = record.Count;
record.Add(records);
return records;
}
public MainRecord Get(int id){
return record[id];
}
public bool Delete(int id){
if(record[id] != null){
record.RemoveAt(id);
return true;
}else{
return false;
}
}
public List<MainRecord> GetAll(){
return record;
}
}
public class BussinessLogic {
private IDataSource dataSource;
public BussinessLogic(IDataSource source){
dataSource = source;
}
public List<MainRecord> GetList(){
List<MainRecord> list = dataSource.GetAll();
return list;
}
public List<MainRecord> SaveList(MainRecord record){
List<MainRecord> list = dataSource.GetAll();
list.Add(dataSource.Save(record));
return list;
}
}
class Program: MainRecord {
static BussinessLogic logic;
static void PrintMenu(){
Console.WriteLine("1 - save, 2 - output by id , 3 - output all List, delete - delete by id , exit - exit programm");
}
static void SaveListId(){
Console.WriteLine("Addres: ");
string Newaddress = Console.ReadLine();
Console.WriteLine("List of planned events: ");
string Newactivity = Console.ReadLine();
Console.WriteLine("Date 1: ");
string Newdata_1 = Console.ReadLine();
Console.WriteLine("Date 2: ");
string Newdata_2 = Console.ReadLine();
List<MainRecord> list = logic.SaveList(new Program{address = Newaddress, activity = Newactivity, data_1 = Newdata_1, data_2 = Newdata_2});
Main();
}
static void PrintListId(){
Main();
}
static void Delete(){
Main();
}
static void PrintList(){
List<MainRecord> list = logic.GetList();
foreach(MainRecord record in list){
Console.WriteLine(record.id);
}
Main();
}
static void Main(){
logic = new BussinessLogic(new MemoryDataSource());
bool exit = false;
while(!exit){
PrintMenu();
string command = Console.ReadLine();
switch(command){
case "1":
SaveListId();
break;
case "2":
PrintListId();
break;
case "3":
PrintList();
break;
case "delete":
Delete();
break;
case "exit":
return;
}
}
}
}
i guess it is because of this line
private List<MainRecord> record = new List<MainRecord>();
if so, how can the code be implemented to make the recording work correctly?
CodePudding user response:
Your problem is because when you call SaveListId
it calls back to Main()
at the end of the method. This in turn creates a new object, logic = new BussinessLogic(new MemoryDataSource());
. I would start by removing the Main()
call, since just returning to the caller would be more appropriate, since I assume recursion was not intended.