Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package unimap;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class executer
- {
- BufferedReader reader;
- int choice;
- student s;
- product p;
- ArrayList<student> students;
- public executer()
- {
- reader=new BufferedReader(new InputStreamReader(System.in));
- choice=0;
- students=new ArrayList<student>();
- menu();
- }
- public static void clearScreen()
- {
- for (int i=1; i<=10; i++)
- System.out.println("\n");
- }
- public void menu()
- {
- do
- {
- clearScreen();
- System.out.println("Welcome !");
- System.out.println("1. Add Student");
- System.out.println("2. List Students");
- System.out.println("3. Search Students");
- System.out.println("4. Write To File");
- System.out.println("5. Read From File");
- System.out.println("0. Exit");
- System.out.println("Enter your choice");
- try
- {
- choice=Integer.parseInt(reader.readLine());
- if(choice==1)
- {
- getstudent();
- }
- else if(choice==2)
- {
- liststudents();
- }
- else if(choice==3)
- {
- searchstudent();
- }
- else if(choice==4)
- {
- writetofile();
- }
- else if(choice==5)
- {
- readfromfile();
- }
- else
- {;}
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }while(choice!=0);
- }
- private void searchstudent()
- {
- try
- {
- String sname;
- boolean flag=false;
- System.out.println("Student Name");
- sname=reader.readLine();
- for(student s:students)
- {
- if(s.sname.equals(sname))
- {
- flag=true;
- System.out.println("Found..");
- System.out.println(s.snumber+","+s.sname+","+s.sbcode+","+s.sbname);
- }
- }
- if(flag==false)
- {
- System.out.println("Not Found..");
- }
- reader.readLine();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- private void getstudent()
- {
- try
- {
- int snumber;
- String sname,sbcode,sbname;
- System.out.println("Student Number");
- snumber=Integer.parseInt(reader.readLine());
- System.out.println("Student Name");
- sname=reader.readLine();
- System.out.println("Subject Code");
- sbcode=reader.readLine();
- System.out.println("Subject Name");
- sbname=reader.readLine();
- s=new student(snumber,sname,sbcode,sbname);
- students.add(s);
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- private void readfromfile()
- {
- try
- {
- File file = new File("file.txt");
- FileReader fileReader = new FileReader(file);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line;
- while ((line = bufferedReader.readLine()) != null)
- {
- System.out.println(line);
- }
- bufferedReader.close();
- reader.readLine();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- private void writetofile()
- {
- try
- {
- File file =new File("file.txt");
- FileWriter fileWritter = new FileWriter(file.getName(),true);
- BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
- for(student s:students)
- {
- String student=s.snumber+","+s.sname+","+s.sbcode+","+s.sbname;
- bufferWritter.write(student);
- bufferWritter.newLine();
- }
- bufferWritter.close();
- System.out.println("Write Done..");
- reader.readLine();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- private void liststudents()
- {
- for(student s:students)
- {
- System.out.println(s.snumber+","+s.sname+","+s.sbcode+","+s.sbname);
- }
- try
- {
- reader.readLine();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- public static void main(String k[])
- {
- new executer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement