Example: ArrayList , Scanner, Serializable, Exception , Loop java
Student Management
-Comparator
-ArrayList
-Scanner
-Serializable
-File
Student Management
-Comparator
-ArrayList
-Scanner
-Serializable
-File
MainPage.xaml
Windows Store 2017
package javaapplication2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
StudentConsole cs = new StudentConsole();
int choice = 0;
do {
cs.Menu();
System.out.println("Enter Your Choice :");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
cs.AddStudent();
break;
case 2:
cs.Modify();
break;
case 3:
cs.Display();
break;
case 4:
System.exit(0);
default:
System.out.println("Choice from 1 - 4");
}
} while (choice != 5);
}
}
class Student implements Serializable {
private String id;
private String name;
private String address;
private String phone;
public Student() {
}
public Student(String id, String name, String address, String phone) {
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return id + "" + name + "" + address + "" + phone;
}
}
class StudentConsole {
Student std = null;
StudentList test = null;
Scanner sc = null;
public StudentConsole() {
std = new Student();
test = new StudentList();
sc = new Scanner(System.in);
}
//add student
public void AddStudent() {
String answer = "";
do {
System.out.println("Enter Student ID :");
String id = sc.nextLine();
std = test.FindByID(id);
if (std != null) {
System.out.println("Student ID Exist !");
} else {
System.out.println("Enter Student Name :");
String name = sc.nextLine();
System.out.println("Enter Student Address :");
String address = sc.nextLine();
System.out.println("Enter Student Phone :");
String phone = sc.nextLine();
std = new Student(id, name, address, phone);
test.Add(std);
test.Save();
System.out.println("Continue [Y/N] ?");
answer = sc.nextLine();
}
} while (answer.equalsIgnoreCase("y"));
}
//Modify Student
public void Modify() {
System.out.println("Modification is not allowed ");
}
//Display
public void Display() {
test.Load();
Student[] arr = test.showAll();
System.out.printf("%-15s\t%-15s\t%-15s\t%-15s\n", "ID", "Name", "Address", "Phone");
for (int i = 0; i < arr.length; i++) {
System.out.printf("%-15s\t%-15s\t%-15s\t%-15s\n", arr[i].getId(), arr[i].getName(), arr[i].getAddress(), arr[i].getPhone());
}
}
//menu
public void Menu() {
System.out.println("------------------");
System.out.println("1. Add Student ");
System.out.println("2. Modify Student ");
System.out.println("3. Display Student Record ");
System.out.println("4.Exit");
System.out.println("------------------");
}
}
class StudentList {
ArrayList<Student> list = null;
Student std = null;
public StudentList() {
list = new ArrayList<Student>();
std = new Student();
}
//add Student
public void Add(Student st) {
if (st != null) {
list.add(st);
}
}
//find by ID
public Student FindByID(String id) {
if (id != null) {
for (int i = 0; i < list.size(); i++) {
if (id.equalsIgnoreCase(list.get(i).getId())) {
return list.get(i);
}
}
}
return null;
}
//show all
public Student[] showAll() {
return list.toArray(new Student[list.size()]);
}
//save
public void Save() {
String filename = "student.dat";
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
int count = list.size();
out.writeInt(count);
out.writeObject(list);
out.flush();
out.close();
} catch (IOException ex) {
Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
}
}
//load
public void Load() {
ObjectInputStream ois = null;
try {
String filename = "student.dat";
ois = new ObjectInputStream(new FileInputStream(filename));
int count = list.size();
count = ois.readInt();
try {
list = (ArrayList<Student>) ois.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(StudentList.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
0 nhận xét:
Post a Comment