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