small fixes

This commit is contained in:
Ksan 2025-08-04 23:01:59 +02:00
parent 12ab19b864
commit 8548cf0acc
2 changed files with 27 additions and 10 deletions

View File

@ -1,20 +1,18 @@
package dev.ksan.travelpathoptimizer.app; package dev.ksan.travelpathoptimizer.app;
import java.io.IOException;
import dev.ksan.travelpathoptimizer.util.TicketPrinter; import dev.ksan.travelpathoptimizer.util.TicketPrinter;
import java.io.IOException;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Stage; import javafx.stage.Stage;
public class TravelPathOptimizerApplication extends Application { public class TravelPathOptimizerApplication extends Application {
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
TicketPrinter.loadCounter(); TicketPrinter.loadCounter();
System.out.println(TicketPrinter.getTotalProfit() + " " + TicketPrinter.getTicketsSoldNum());
FXMLLoader fxmlLoader = FXMLLoader fxmlLoader =
new FXMLLoader(TravelPathOptimizerApplication.class.getResource("main.fxml")); new FXMLLoader(TravelPathOptimizerApplication.class.getResource("main.fxml"));
@ -33,4 +31,3 @@ public class TravelPathOptimizerApplication extends Application {
launch(); launch();
} }
} }

View File

@ -10,7 +10,7 @@ import java.util.List;
public class TicketPrinter { public class TicketPrinter {
private static final String COUNTER_FILE = "ticket_counter.txt"; private static final String COUNTER_FILE = "ticket_counter.txt";
private static final String RECEIPTS_DIRECTORY = "receipts"; private static final String RECEIPTS_DIRECTORY = "receipts";
private static final double totalProfit = 0.0; private static double totalProfit = 0.0;
private static int ticketIdCounter = 0; private static int ticketIdCounter = 0;
public static void generateTicketReceipt(List<Departure> departures, double totalPrice) { public static void generateTicketReceipt(List<Departure> departures, double totalPrice) {
@ -44,7 +44,7 @@ public class TicketPrinter {
receipt.append("-----------------------------------------------------\n"); receipt.append("-----------------------------------------------------\n");
receipt.append("Total Price: $").append(String.format("%.2f", totalPrice)).append("\n"); receipt.append("Total Price: $").append(String.format("%.2f", totalPrice)).append("\n");
totalProfit += totalPrice;
receipt.append("=====================================================\n"); receipt.append("=====================================================\n");
receipt.append("Thank you for choosing our service!\n"); receipt.append("Thank you for choosing our service!\n");
folderExists(); folderExists();
@ -78,20 +78,40 @@ public class TicketPrinter {
try { try {
Path path = Paths.get(COUNTER_FILE); Path path = Paths.get(COUNTER_FILE);
if (Files.exists(path)) { if (Files.exists(path)) {
String counter = new String(Files.readAllBytes(path)).trim(); List<String> lines = Files.readAllLines(path);
ticketIdCounter = Integer.parseInt(counter);
for (String line : lines) {
String[] parts = line.split("=");
if ("ticketIdCounter".equals(parts[0].trim())) {
ticketIdCounter = Integer.parseInt(parts[1].trim());
} else if ("totalProfit".equals(parts[0].trim())) {
totalProfit = Double.parseDouble(parts[1].trim());
}
}
} else { } else {
ticketIdCounter = 0; ticketIdCounter = 0;
totalProfit = 0.0;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ticketIdCounter = 0; ticketIdCounter = 0;
totalProfit = 0.0;
} }
} }
public static double getTotalProfit() {
return totalProfit;
}
public static int getTicketsSoldNum() {
return ticketIdCounter;
}
private static void saveCounter() { private static void saveCounter() {
try { try {
Files.write(Paths.get(COUNTER_FILE), String.valueOf(ticketIdCounter).getBytes()); String str = "ticketIdCounter=" + ticketIdCounter + "\n" + "totalProfit=" + totalProfit;
Files.write(Paths.get(COUNTER_FILE), str.getBytes());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }