0% found this document useful (0 votes)
40 views

Student Example

The document defines a student management system with classes for Student, Menu, ManageStudent, and SortName. Student stores name and age attributes. Menu displays options to add, view, edit, sort, and save students. ManageStudent handles adding, searching, sorting and saving students to a file. SortName implements comparing students by name for sorting. The main method runs the menu interface.

Uploaded by

Cong Cong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Student Example

The document defines a student management system with classes for Student, Menu, ManageStudent, and SortName. Student stores name and age attributes. Menu displays options to add, view, edit, sort, and save students. ManageStudent handles adding, searching, sorting and saving students to a file. SortName implements comparing students by name for sorting. The main method runs the menu interface.

Uploaded by

Cong Cong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

package studentp;

public class Student {

//data members

private String name;

private int age;

//Constructors

public Student() {

this.name = "";

this.age = 0;

public Student(String name, int age) {

this.name = name;

this.age = age;

public String getName() {

return name;

public int getAge() {

return age;

public void setName(String name) {

this.name = name;

}
public void setAge(int age) {

this.age = age;

@Override

public String toString() {

return name + ", " + age;

package studentp;

import java.util.Scanner;

public class Menu {

public Menu()

int choice;

ManageStudent st = new ManageStudent();

do

System.out.println("---Menu------");

System.out.println("0/Read data from text file");

System.out.println("1/Add student info");

System.out.println("11/Edit student info");

System.out.println("2/List All students");


System.out.println("3/Search student by name");

System.out.println("4/Sort by name");

System.out.println("41/Sort by name and age");

System.out.println("5/Save to text file");

System.out.println("6/Quit");

System.out.println("---------------------");

System.out.print("Choose function:");

Scanner sc = new Scanner(System.in);

choice = Integer.parseInt(sc.nextLine());

switch(choice)

case 0: st.ReadData(); break;

case 1: st.Add(); break;

case 2: st.ListAll();break;

case 3: System.out.print("Enter name:");

Scanner s = new Scanner(System.in);

String name = s.nextLine();

Student x = st.Search(name);

if (x == null)

System.out.print("not found");

else

st.View(x);

break;

case 4: st.SortByName();

st.ListAll();

break;

case 5: st.SaveData();break;

case 6: break;

default: System.out.println("Invalid function");


}

}while (choice != 6);

package studentp;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import java.io.*;

import java.util.Collections;

public class ManageStudent {

static List<Student> list = new ArrayList<Student>();

//Methods

public void Add()

Scanner sc = new Scanner(System.in);

System.out.print("Enter name:");

String name = sc.nextLine();

System.out.print("Enter age:");

int age = Integer.parseInt(sc.nextLine());

list.add(new Student(name,age));

public void ReadData()

{
String name;

int age;

try

BufferedReader br = new BufferedReader(new FileReader("d:\\java\\StudentP\\student.dat"));

while (br!= null)

String line = br.readLine();

String [] value = line.split(";");

name = value[0];

age = Integer.parseInt(value[1]);

list.add(new Student(name,age));

br.close();

catch(Exception e)

System.out.println(e.getMessage());

public void SaveData()

try

BufferedWriter writer = new BufferedWriter(new FileWriter("d:\\java\\StudentP\\student.dat"));

for (Student x:list)

writer.write(x.getName()+ ";" + x.getAge());

writer.newLine(); //make new line


}

writer.close();

}catch(Exception e){

System.out.println(e.getMessage());

public void ListAll()

for (Student x:list) //foreach

System.out.println(x.toString());

public void SortByName()

Collections.sort(list, new SortName());

public Student Search(String name)

for(Student x: list)

if (x.getName().contains(name))

return x;

}
return null;

public void View (Student x)

System.out.print(x.toString());

package studentp;

import java.util.Comparator;

public class SortName implements Comparator<Student> {

public int compare(Student s1, Student S2)

return s1.getName().compareTo(S2.getName());

package studentp;

public class StudentP {

public static void main(String[] args) {

// TODO code application logic here

Menu menu = new Menu();


}

You might also like