Commit 036b0ac6 authored by Magnus Schjølberg's avatar Magnus Schjølberg
Browse files

Add WIP solution to final project lecture

parent 1e364d09
package selfcheckout;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
public class HomeFolderReceiptHandler implements IReceiptHandler {
@Override
public SelfCheckout readReceipt(String filename, SelfCheckout selfCheckout) throws IOException {
try (Scanner scanner = new Scanner(getReceiptFolderPath())) {
String phoneNumber = scanner.nextLine();
if (!phoneNumber.equals("null")) {
selfCheckout.registerPhoneNumber(phoneNumber);
}
while (scanner.hasNextLine()) {
String[] properties = scanner.nextLine().split(";");
selfCheckout.scanItem(new Item(properties[0], Double.parseDouble(properties[1]), properties[2]));
}
}
return selfCheckout;
}
@Override
public void writeReceipt(String filename, SelfCheckout selfCheckout) throws IOException {
Files.createDirectories(getReceiptFolderPath());
try (PrintWriter writer = new PrintWriter(getReceiptFolderPath().resolve(filename + ".txt").toFile())) {
writer.println(selfCheckout.getPhoneNumber());
for (Item item : selfCheckout.getShoppingCartItems()) {
writer.println(String.format("%s;%s;%s", item.getName(), item.getPrice(), item.getCategory()));
}
}
}
private static Path getReceiptFolderPath() {
return Path.of(System.getProperty("user.home"), "tdt4100files", "receipts");
}
}
package selfcheckout;
import java.io.FileNotFoundException;
import java.io.IOException;
public interface IReceiptHandler {
SelfCheckout readReceipt(String filename, SelfCheckout selfCheckout) throws FileNotFoundException;
SelfCheckout readReceipt(String filename, SelfCheckout selfCheckout) throws IOException;
void writeReceipt(String filename, SelfCheckout selfCheckout) throws FileNotFoundException;
void writeReceipt(String filename, SelfCheckout selfCheckout) throws IOException;
}
package selfcheckout;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
......@@ -22,7 +23,7 @@ import javafx.scene.text.Font;
public class SelfCheckoutController {
private SelfCheckout selfCheckout;
private IReceiptHandler checkoutReceipt = new ReceiptHandler();
private IReceiptHandler checkoutReceipt = new HomeFolderReceiptHandler();
private List<Item> selectableItemsList;
@FXML
......@@ -89,8 +90,9 @@ public class SelfCheckoutController {
phoneNumber.clear();
phoneNumber.disableProperty().set(false);
login.disableProperty().set(false);
} catch (FileNotFoundException e) {
showErrorMessage("Kvitteringen kunne ikke skrives til feil!");
} catch (IOException e) {
System.out.println(e);
showErrorMessage("Kvitteringen kunne ikke skrives til fil!");
} catch (NoSuchElementException e) {
// Do nothing
}
......@@ -151,7 +153,7 @@ public class SelfCheckoutController {
phoneNumber.disableProperty().set(true);
login.disableProperty().set(true);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
showErrorMessage("Kvitteringen ble ikke funnet!");
selfCheckout = tempCheckout;
} catch (NoSuchElementException e) {
......
......@@ -5,7 +5,7 @@ import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReceiptHandler implements IReceiptHandler {
public class TemporaryFileReceiptHandler implements IReceiptHandler {
@Override
public SelfCheckout readReceipt(String filename, SelfCheckout selfCheckout) throws FileNotFoundException {
......@@ -34,7 +34,7 @@ public class ReceiptHandler implements IReceiptHandler {
}
private static File getFile(String filename) {
return new File("selfcheckout-example\\src\\main\\resources\\selfcheckoutmal3\\receipts\\" + filename + ".txt");
return new File(TemporaryFileReceiptHandler.class.getResource("receipts/").getFile() + filename + ".txt");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment