Commit 911ecb63 authored by Magnus Schjølberg's avatar Magnus Schjølberg
Browse files

Fix SelfCheckout-example file writing for Windows

parent ea70ec84
......@@ -3,6 +3,7 @@ package selfcheckout;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.io.File;
import java.nio.file.Path;
import java.util.Scanner;
......@@ -10,7 +11,7 @@ public class HomeFolderReceiptHandler implements IReceiptHandler {
@Override
public SelfCheckout readReceipt(String filename, SelfCheckout selfCheckout) throws IOException {
try (Scanner scanner = new Scanner(getReceiptPath(filename).toFile())) {
try (Scanner scanner = new Scanner(getReceiptFile(filename))) {
String phoneNumber = scanner.nextLine();
if (!phoneNumber.equals("null")) {
selfCheckout.registerPhoneNumber(phoneNumber);
......@@ -26,7 +27,7 @@ public class HomeFolderReceiptHandler implements IReceiptHandler {
@Override
public void writeReceipt(String filename, SelfCheckout selfCheckout) throws IOException {
Files.createDirectories(getReceiptFolderPath());
try (PrintWriter writer = new PrintWriter(getReceiptPath(filename).toFile())) {
try (PrintWriter writer = new PrintWriter(getReceiptFile(filename))) {
writer.println(selfCheckout.getPhoneNumber());
for (Item item : selfCheckout.getShoppingCartItems()) {
writer.println(String.format("%s;%s;%s", item.getName(), item.getPrice(), item.getCategory()));
......@@ -36,8 +37,8 @@ public class HomeFolderReceiptHandler implements IReceiptHandler {
}
@Override
public Path getReceiptPath(String filename) {
return getReceiptFolderPath().resolve(filename + ".txt");
public File getReceiptFile(String filename) {
return getReceiptFolderPath().resolve(filename + ".txt").toFile();
}
private static Path getReceiptFolderPath() {
......
package selfcheckout;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
public interface IReceiptHandler {
......@@ -9,6 +9,6 @@ public interface IReceiptHandler {
void writeReceipt(String filename, SelfCheckout selfCheckout) throws IOException;
Path getReceiptPath(String filename);
File getReceiptFile(String filename);
}
package selfcheckout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.Scanner;
public class TemporaryFileReceiptHandler implements IReceiptHandler {
@Override
public SelfCheckout readReceipt(String filename, SelfCheckout selfCheckout) throws FileNotFoundException {
try (Scanner scanner = new Scanner(getReceiptPath(filename).toFile())) {
try (Scanner scanner = new Scanner(getReceiptFile(filename))) {
String phoneNumber = scanner.nextLine();
if (!phoneNumber.equals("null")) {
selfCheckout.registerPhoneNumber(phoneNumber);
......@@ -24,7 +24,8 @@ public class TemporaryFileReceiptHandler implements IReceiptHandler {
@Override
public void writeReceipt(String filename, SelfCheckout selfCheckout) throws FileNotFoundException {
try (PrintWriter writer = new PrintWriter(getReceiptPath(filename).toFile())) {
try (PrintWriter writer = new PrintWriter(getReceiptFile(filename))) {
System.out.println(getReceiptFile(filename));
writer.println(selfCheckout.getPhoneNumber());
for (Item item : selfCheckout.getShoppingCartItems()) {
writer.println(String.format("%s;%s;%s", item.getName(), item.getPrice(), item.getCategory()));
......@@ -34,8 +35,8 @@ public class TemporaryFileReceiptHandler implements IReceiptHandler {
}
@Override
public Path getReceiptPath(String filename) {
return Path.of(TemporaryFileReceiptHandler.class.getResource("receipts/").getFile() + filename + ".txt");
public File getReceiptFile(String filename) {
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