05 April 2016

ArrayList Serializable java...1


Student Management
1. Add Student
2. Display all
3. Save to File .txt

CODE 

test Package

Test.java

package test;

import java.io.IOException;

public class Test {
   
    public static void main(String[] args) throws IOException {
        StudentConsole console = new StudentConsole();
        console.start();
    }
   
}

StudentConsole.java

package test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class StudentConsole {
   
    private StudentManagement control;
    Scanner scan;
   
    public StudentConsole(){
        scan = new Scanner(System.in);
        control = new StudentManagement();
    }
   
    public void start() throws IOException{
       
        Student a = new Student(1, "Teddy", "25 Vu Ngoc Phan", 113);
        Student b = new Student(2, "Jimmy", "85 Xa Dan", 114);
        Student c = new Student(3, "Gordon", "77 My Dinh", 115);
       
        this.control.addStudent(a);
        this.control.addStudent(b);
        this.control.addStudent(c);
       
        while(true){
            int choice = menu();
            switch(choice){
                case 1:
                    addStudent();
                    break;
                case 2:
                    displayStudent();
                    break;
                case 3:
                    save();
                    break;
                case 4:
                    System.exit(0);           
            }
        }
    }
   
    public int menu(){
        System.out.println("---------------------------------");
        System.out.println("Student Management by Hoang Bui");
        System.out.println("1.Add student records");
        System.out.println("2.Display student records");
        System.out.println("3.Save");
        System.out.println("4.Exit");
        System.out.println("-----------------Your choice?");
       
        int choice = readInt();
        return choice;
    }
   
    private int readInt(){
        int integer = 0;
        try{
            integer = Integer.parseInt(scan.nextLine());
        }catch (Exception e){
            e.printStackTrace();
        }
        return integer;       
    }

    private void addStudent() {
        System.out.println("Insert Student's ID:");
        int stuid = readInt();
        System.out.println("Name?");
        String name = scan.nextLine();
        System.out.println("Address?");
        String address = scan.nextLine();
        System.out.println("And phone?");
        int phone = readInt();
       
        Student a = new Student(stuid, name, address, phone);
        control.addStudent(a);
       
        System.out.println("Student Added");
    }

    private void save() throws IOException {
        control.save();
    }

    private void displayStudent() {
        control.displayList();
    }
}

StudentManagement.java

package test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

class StudentManagement {

    ArrayList<Student> listStudent;
   
    public StudentManagement() {
        listStudent = new ArrayList();
    }
   
    public void displayList(){
        for (Object object : listStudent) {
            System.out.println(object.toString());
        }
    }

    public void addStudent(Student a) {
        listStudent.add(a);
    }
   
    public void save() throws IOException{
        BufferedWriter bw = new BufferedWriter(new FileWriter("Student.txt"));
        for (Student st : listStudent) {
            bw.write(st.toStringToFile());
        }
        bw.close();
    }
}

Student.java

package test;

public class Student {
    private int StudentID;
    private String name;
    private String address;
    private int phone;

    public Student(int StudentID, String name, String address, int phone) {
        this.StudentID = StudentID;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    public int getStudentID() {
        return StudentID;
    }

    public void setStudentID(int StudentID) {
        this.StudentID = StudentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "StudentID : " + StudentID + "; Name : " + name + "; Address : " + address + "; Phone : " + phone;
    }
   
    public String toStringToFile() {
        return StudentID + "," + name + "," + address + "," + phone + "\n";
    }
   
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang