can you write the documentation of the code? public class Login { static boolean
ID: 3838991 • Letter: C
Question
can you write the documentation of the code?
public class Login {
static boolean result;
private String name;
private DataOutputStream fileOut;
private File file;
private Button input = new Button("GO");
private Button output= new Button("INTRO");
private HBox btPane = new HBox(5);
public boolean Login() {
VBox LoginPane = new VBox();
Stage fileStage = new Stage();
TextField inputFileName = new TextField();
input.setAlignment(Pos.CENTER);
output.setAlignment(Pos.CENTER);
LoginPane.setAlignment(Pos.CENTER);
user.setAlignment(Pos.CENTER);
btPane.setPrefWidth(400);
output.setMinWidth(btPane.getPrefWidth()/2);
input.setMinWidth(btPane.getPrefWidth()/2);
LoginPane.setPrefWidth(500);
LoginPane.setMinWidth(LoginPane.getPrefWidth()/2);
Scene fileScene = new Scene(LoginPane, 500, 500);
LoginPane.setPadding(new Insets(150, 150, 150, 150));
LoginPane.setSpacing(40);
LoginPane.getChildren().addAll(user, inputFileName, input, output);
input.setOnAction(e -> {
name = inputFileName.getText() + ".dat";
file = new File(name);
try {
fileOut = new DataOutputStream(new FileOutputStream(name ));
}
catch (FileNotFoundException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
result = true;
fileStage.close();
});
output.setOnAction(e -> {
name = inputFileName.getText() + ".txt";
file = new java.io.File(name );
try {
fileStage.setScene(FileInput());
} catch (IOException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
})
fileStage.setScene(fileScene);
fileStage.showAndWait();
return result;
}
public Scene FileInput() throws IOException {
Label status = new Label();
HBox showContent = new HBox();
showContent.getChildren().add(status);
Scene contentScene = new Scene(showContent, 700, 500);
java.io.File Login = new java.io.File(name);
byte[] fileData = new byte[(int) Login.length()];
DataInputStream input = new DataInputStream(new FileInputStream(Login));
input.readFully(fileData);
input.close();
status.setText(new String(fileData));
return contentScene;
}
public void SaveFile(String status) throws IOException {
fileOut.writeUTF(status + " ");
}}
Explanation / Answer
/**
*Login class
**/
public class Login {
// the scene contains two buttons - Go and Intro
static boolean result;
private String name;
private DataOutputStream fileOut;
private File file;
private Button input = new Button("GO");
private Button output= new Button("INTRO");
private HBox btPane = new HBox(5);
/**
* constructor that initializes and sets up the stage
* Sets the alignments to center
* adds a tet field for the user to provide the file name
*
**/
public boolean Login() {
VBox LoginPane = new VBox();
Stage fileStage = new Stage();
TextField inputFileName = new TextField();
input.setAlignment(Pos.CENTER);
output.setAlignment(Pos.CENTER);
LoginPane.setAlignment(Pos.CENTER);
user.setAlignment(Pos.CENTER);
btPane.setPrefWidth(400);
output.setMinWidth(btPane.getPrefWidth()/2);
input.setMinWidth(btPane.getPrefWidth()/2);
LoginPane.setPrefWidth(500);
LoginPane.setMinWidth(LoginPane.getPrefWidth()/2);
Scene fileScene = new Scene(LoginPane, 500, 500);
LoginPane.setPadding(new Insets(150, 150, 150, 150));
LoginPane.setSpacing(40);
LoginPane.getChildren().addAll(user, inputFileName, input, output);
// when the input button is clicked, the file name is read and the file is opened
input.setOnAction(e -> {
name = inputFileName.getText() + ".dat";
file = new File(name);
//reads the .dat file from here
try {
fileOut = new DataOutputStream(new FileOutputStream(name ));
}
catch (FileNotFoundException ex) {
// incase the file is not found
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
// boolean is set true if the file is found and read
result = true;
fileStage.close();
});
// event set for output
// creates a file, scene us changed to display output related items
output.setOnAction(e -> {
name = inputFileName.getText() + ".txt";
file = new java.io.File(name );
try {
fileStage.setScene(FileInput());
} catch (IOException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
})
fileStage.setScene(fileScene);
fileStage.showAndWait();
return result;
}
// creates a scene with label and updates the status
// prints the data from the file on to the status
public Scene FileInput() throws IOException {
Label status = new Label();
HBox showContent = new HBox();
showContent.getChildren().add(status);
Scene contentScene = new Scene(showContent, 700, 500);
java.io.File Login = new java.io.File(name);
byte[] fileData = new byte[(int) Login.length()];
DataInputStream input = new DataInputStream(new FileInputStream(Login));
input.readFully(fileData);
input.close();
status.setText(new String(fileData));
return contentScene;
}
// writes the status to the file
public void SaveFile(String status) throws IOException {
fileOut.writeUTF(status + " ");
}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.