Add src/main/java/dev/sillyangel/calc/CalculatorHistory.java
Some checks failed
Build the Jar / build (push) Failing after 6s
Some checks failed
Build the Jar / build (push) Failing after 6s
This commit is contained in:
138
src/main/java/dev/sillyangel/calc/CalculatorHistory.java
Normal file
138
src/main/java/dev/sillyangel/calc/CalculatorHistory.java
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
package dev.sillyangel.calc;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages calculation history for the calculator application.
|
||||||
|
* Stores calculations in a file and provides methods to retrieve and manage history.
|
||||||
|
*/
|
||||||
|
public class CalculatorHistory {
|
||||||
|
private final List<String> calculationHistory = new ArrayList<>();
|
||||||
|
private final Path historyFilePath;
|
||||||
|
private static final String HISTORY_DIR = System.getProperty("user.home") + File.separator + ".calculator";
|
||||||
|
private static final String HISTORY_FILE = "calc_history.txt";
|
||||||
|
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
public CalculatorHistory() {
|
||||||
|
historyFilePath = Paths.get(HISTORY_DIR, HISTORY_FILE);
|
||||||
|
initializeHistoryFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the history file and directory structure.
|
||||||
|
* Creates the directory and file if they don't exist, and loads existing history.
|
||||||
|
*/
|
||||||
|
private void initializeHistoryFile() {
|
||||||
|
try {
|
||||||
|
// Create directory if it doesn't exist
|
||||||
|
Path dir = Paths.get(HISTORY_DIR);
|
||||||
|
if (!Files.exists(dir)) {
|
||||||
|
Files.createDirectories(dir);
|
||||||
|
System.out.println("Created history directory: " + HISTORY_DIR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create file if it doesn't exist
|
||||||
|
if (!Files.exists(historyFilePath)) {
|
||||||
|
Files.createFile(historyFilePath);
|
||||||
|
System.out.println("Created history file: " + historyFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load existing history
|
||||||
|
loadHistory();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error initializing history file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves a calculation to the history file.
|
||||||
|
* @param calculation The calculation string to save (e.g., "5 + 3 = 8")
|
||||||
|
*/
|
||||||
|
public void saveToHistory(String calculation) {
|
||||||
|
try {
|
||||||
|
String timestamp = LocalDateTime.now().format(formatter);
|
||||||
|
String entry = "[" + timestamp + "] " + calculation;
|
||||||
|
|
||||||
|
// Add to memory list
|
||||||
|
calculationHistory.add(entry);
|
||||||
|
|
||||||
|
// Append to file
|
||||||
|
Files.write(historyFilePath,
|
||||||
|
(entry + System.lineSeparator()).getBytes(),
|
||||||
|
StandardOpenOption.APPEND);
|
||||||
|
|
||||||
|
System.out.println("Saved to history: " + entry);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error saving to history: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads history from the file into memory.
|
||||||
|
*/
|
||||||
|
private void loadHistory() {
|
||||||
|
try {
|
||||||
|
if (Files.exists(historyFilePath)) {
|
||||||
|
List<String> lines = Files.readAllLines(historyFilePath);
|
||||||
|
calculationHistory.addAll(lines);
|
||||||
|
System.out.println("Loaded " + lines.size() + " history entries");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error loading history: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a copy of all calculation history entries.
|
||||||
|
* @return A list of history entries with timestamps
|
||||||
|
*/
|
||||||
|
public List<String> getCalculationHistory() {
|
||||||
|
return new ArrayList<>(calculationHistory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the most recent N history entries.
|
||||||
|
* @param count The number of entries to return
|
||||||
|
* @return A list of the most recent history entries
|
||||||
|
*/
|
||||||
|
public List<String> getRecentHistory(int count) {
|
||||||
|
int size = calculationHistory.size();
|
||||||
|
int fromIndex = Math.max(0, size - count);
|
||||||
|
return new ArrayList<>(calculationHistory.subList(fromIndex, size));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all history entries from memory and file.
|
||||||
|
*/
|
||||||
|
public void clearHistory() {
|
||||||
|
try {
|
||||||
|
calculationHistory.clear();
|
||||||
|
Files.write(historyFilePath, new byte[0], StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
|
System.out.println("History cleared");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error clearing history: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of history entries.
|
||||||
|
* @return The count of history entries
|
||||||
|
*/
|
||||||
|
public int getHistoryCount() {
|
||||||
|
return calculationHistory.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the path to the history file.
|
||||||
|
* @return The file path as a string
|
||||||
|
*/
|
||||||
|
public String getHistoryFilePath() {
|
||||||
|
return historyFilePath.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user