jeudi 5 avril 2018

Where to close FileWriter when reusing it in different methods?

I have encountered a problem: I need to be able to filewrite after I have added to the array (dock) and removed from the array (undock) on the fly. But I do not know where to put the flush() and close(). I get errors when I but it after the write function wherever I put them because they have already closed the filewriter. Can you help?

try {
    portLog.flush();
} catch (IOException e) {
    e.printStackTrace();
}


try {
    portLog.close();
} catch (IOException e) {
    e.printStackTrace();
}

Here is my code:

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

static Scanner scan = new Scanner(System.in);

private static Ship[] dock1 = new Ship[10];
private static Ship[] waitingList = new Ship[10];
static FileWriter portLog;
static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
//get current date time with Date()
static Date date = new Date();

static {
    try {
        portLog = new FileWriter("\\Users\\Smith\\Desktop\\PortLog.txt", true);
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public static void main(String[] args) {
    menu();
}

public static void menu() {

    Scanner scan = new Scanner(System.in);

    while (true) {

        System.out.println("Choose an option: 1-3");
        System.out.println("1. Dock");
        System.out.println("2. Undock");
        System.out.println("3. Status");

        int menu = scan.nextInt();
        switch (menu) {
            case 1:
                System.out.println("1. Dock");
                dock();
                break;
            case 2:
                System.out.println("2. Undock");
                undock();
                break;
            case 3:
                System.out.println("3. Status");
                printDock();
                printWaitingList();
                break;
            case 4:
                System.out.println("4. Exit");
                System.exit(0);
            default:
                System.out.println("No such option");
                break;
        }
    }
}


public static void dock() {

    System.out.println("Enter ship's name: ");
    String name = scan.nextLine();

    System.out.println("Enter ship's size: ");
    String size = scan.nextLine();

    System.out.println("Enter the ships dock:");
    //Check if the dock number is valid
    int i = Integer.valueOf(scan.nextLine());
    if (i >= 0 && i < 10 && dock1[i] == null) {
        int c = 0;
        int co = 0;
        int sco = 0;
        for (int j = 0; j < dock1.length; j++) {
            if (dock1[j] != null && dock1[j].getShipSize().equals("Cargo")) {
                c++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Container")) {
                co++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Super-Container")) {
                sco++;
            }
        }

        if (c < 10 && co < 5 && sco < 2) {
            //Add ship to the dock
            dock1[i] = new Ship(name, size);
            System.out.println("Enough space you can dock");
            System.out.println("Ship has been docked");


            try {
                portLog.write("\n" + " Docked: " + dock1[i].getShipName() + " Size: " + dock1[i].getShipSize() + " at " + dateFormat.format(date));
            } catch (IOException e) {
                e.printStackTrace();
            }


        } else {
            System.out.println("You cannot dock");
            waitingList(name, size);
        }

    } else {
        System.out.println("Couldn't dock");
        waitingList(name, size);
    }

}


public static void undock() {
    System.out.println("Status of ships: ");
    printDock();
    System.out.println("Enter ship's name to undock: ");
    String name = scan.nextLine();

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) {

            try {
                portLog.write("\n" + "Undocked: " + dock1[i].getShipName() + " Size: " + dock1[i].getShipSize() + " at " + dateFormat.format(date));
            } catch (IOException e) {
                e.printStackTrace();
            }

            dock1[i] = null;
            System.out.println("Ship removed");

            /// HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < waitingList.length; j++) {
                if (dock1[i] == null && waitingList[j] != null) {
                    // Add ship to the dock
                    dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                    System.out.println("Move ship from waiting list to dock 1");
                    waitingList[j] = null;
                    return;
                } else {
                  return;
                }
            }
        } else {
        }
    }
    System.out.println("Ship not found");
}

public static void waitingList(String name, String size) {

    System.out.println("Dock 1 is full, ship will try to be added to Waiting List");
    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            //Add ship to the dock
            waitingList[i] = new Ship(name, size);
            System.out.println("Enough space added to waiting list");
            return;
        } else {

        }
    }
    System.out.println("No space on waiting list, ship turned away.");
}

public static void printDock() {

    System.out.println("Docks:");

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
        }
    }
}

private static void printWaitingList() {

    System.out.println("Waiting List:");

    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + waitingList[i].getShipName() + " " + waitingList[i].getShipSize());
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire