NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Code

There are two seprate files

Stundent class that implements Comprable interface

package guistudentsort;
public class Student implements Comparable<Student>
{
private int id;
private String name;
/**
* @param id
* @param name
*/
public Student(int id, String name) {
this.id = id;
this.name = name;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return id + "t" + name+"n";
}

@Override
public int compareTo(Student o) {
if(id<o.getId())
return -1;
else if(id>o.getId())
return 1;
return 0;
}
}

For GUI

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package guistudentsort;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
/**
*
* @author JOSEE
*/
public class GUIStudentSort extends Application {
private Button btnLoad = new Button("Load");
private Button btnSort = new Button("Sort");
private Button btnSave = new Button("Save");
@Override
public void start(Stage primaryStage) {

ArrayList<Student>students=new ArrayList<>();
btnLoad.setMaxSize(100, 200);
btnSort.setMaxSize(100, 200);
btnSave.setMaxSize(100, 200);
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(20);
Alert a = new Alert(AlertType.NONE);
gridPane.add(btnLoad,0,0);
gridPane.add(btnSort,0,1);
gridPane.add(btnSave,0,2);
gridPane.setAlignment(Pos.CENTER);

btnLoad.setOnAction((event) -> {
Scanner scan;
try
{
scan = new Scanner(new File("unsortedStudents.txt"));
int id;
String name;
while(scan.hasNext())
{
students.add(new Student(scan.nextInt(),scan.next()));
}
scan.close();
a.setAlertType(AlertType.CONFIRMATION);
a.setContentText("All student data have been recorded.");
a.show();
}
catch (FileNotFoundException ex) {
a.setAlertType(AlertType.WARNING);
a.setContentText("unsortedStudents.txt file not found.");
a.show();
}

});

btnSort.setOnAction((event) -> {

if(students.isEmpty())
{
a.setAlertType(AlertType.WARNING);
a.setContentText("List is empty. Please load the student data..");
a.show();
}
else
{
Collections.sort(students);
a.setAlertType(AlertType.CONFIRMATION);
a.setContentText("Student data has been sorted by student id.");
a.show();
}
});
btnSave.setOnAction((event) -> {

if(students.isEmpty())
{
a.setAlertType(AlertType.WARNING);
a.setContentText("List is empty. Please load the student data..");
a.show();
}
else
{
FileWriter fileWriter;
try {
fileWriter = new FileWriter("StudentsSortedById.txt");
PrintWriter printWriter = new PrintWriter(fileWriter);
for(Student s:students)
printWriter.write(s.toString());
printWriter.close();
fileWriter.close();
a.setAlertType(AlertType.CONFIRMATION);
a.setContentText("Student data saved to "StudentsSortedById.txt" successfully.");
a.show();
}
catch (IOException ex)
{
a.setAlertType(AlertType.WARNING);
a.setContentText("unsortedStudents.txt file not found.");
a.show();
}


}
});
Scene scene = new Scene(gridPane, 300, 150);
primaryStage.setTitle("Investment Calculator"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.