Skip to content

Instantly share code, notes, and snippets.

There Are Only Two Roles of Code 1
public class Calculator
{
private readonly IStorageService storageService;
private List<int> history = new List<int>();
private int sessionNumber = 1;
private bool newSession;
public Calculator(IStorageService storageService)
{
this.storageService = storageService;
}
public int Add(int firstNumber, int secondNumber)
{
if(newSession)
{
sessionNumber++;
newSession = false;
}
var result = firstNumber + secondNumber;
history.Add(result);
return result;
}
public List<int> GetHistory()
{
if (storageService.IsServiceOnline())
return storageService.GetHistorySession(sessionNumber);
return new List<int>();
}
public int Done()
{
if (storageService.IsServiceOnline())
{
foreach(var result in history)
storageService.Store(result, sessionNumber);
}
newSession = true;
return sessionNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment