N
N
nordwind20132018-06-21 06:39:19
ASP.NET
nordwind2013, 2018-06-21 06:39:19

Calculator on mvc with transaction history?

In the task, you need to implement a calculator with a log that displays previous calculations. Can you tell me how to implement this log?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Nemiro, 2018-06-21
@nordwind2013

If for the web and specifically on ASP.NET , then you can:
1. Implement a structure or class to store a data element (history) and use a static class, property or field (for example, at the controller class level) for the entire history:

public class HistoryItem 
{

  public double X { get; set; }

  public double Y { get; set; }

  public string Operation { get; set; }

  // предполагается, что журнал подразумевает хранение истории вычислений,
  // и хотя можно повторно провести вычисления, в журнале такого по идее быть не должно,
  public double Result { get; set; }

}

// ...

public static List<HistoryItem> History = new List<HistoryItem>();

// ...

History.Add(new HistoryItem { X = 123, Y = 456, Operation = "+", Result =  579 });
History.Add(new HistoryItem { X = 5, Y = 5, Operation = "*", Result = 25 });

or easier, without HistoryItem , write as strings:
public static List<string> History = new List<string>();

// ...

History.Add("2 * 2 = 4");
History.Add("3 + 4 = 7");

You can make allowance for multithreading and use something thread -safe :
using System.Collections.Concurrent;

// ...

public static ConcurrentBag<string> History = new ConcurrentBag<string>();

// ...

History.Add("123 + 345 = 468");
History.Add("7 * 7 = 49");

But with this implementation, the history will be available only within the life cycle of the application.
2. Write to a text file:
// добавить
System.IO.File.AppendText("history.log", "5 * 5 = 25");
// прочитать историю
// System.IO.File.ReadAllText("history.log");

With this implementation, the history file will be available within the life cycle of the storage device until the last backup is lost :-)
3. Write to the database.
Approximately the same option as with writing to a file, but more complicated.
---
If the conditions of the task allow, I would probably do it on the client side ( JavaScript ) and use localStorage :
var historyData = window.localStorage.getItem('history');
var history = historyData ? JSON.parse(historyData) : [];

history.push('1 + 1 = 2');
history.push('2 + 2 = 4');
history.push('4 + 4 = 8');

window.localStorage.setItem('history', JSON.stringify(history));

E
Eugene, 2018-06-21
@evgshk

For the log, you can use the Memento pattern: https://en.wikipedia.org/wiki/Memento_pattern

M
Manuchehr Jalolov, 2018-06-21
@kinglostov

Well, I don’t know how others will say, I would use the structure to write everything as a large array
https://docs.microsoft.com/en-us/dotnet/csharp/pro...

Example
public struct CoOrds
{
    public int x, y;

    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}
class TestCoOrds
{
    static void Main()
    {
        // Initialize:   
        CoOrds coords1 = new CoOrds();
        CoOrds coords2 = new CoOrds(10, 10);

        // Display results:
        Console.Write("CoOrds 1: ");
        Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);

        Console.Write("CoOrds 2: ");
        Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

CoOrds 1: x = 0, y = 0
CoOrds 2: x = 10, y = 10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question