Untitled
unknown
plain_text
3 years ago
6.7 kB
9
Indexable
package v06f;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
*
* @author kiet
*/
public class ManageProfile {
ArrayList<Profile> profileList;
Scanner sc = new Scanner(System.in);
private static final String VALID_ID = "^(?!0000)\\d{4}$";
/**
* Class constructor.
*
*/
public ManageProfile() {
this.profileList = new ArrayList<>(); //cap bo nho cho arrayList
}
/**
* Method used to add a profile.
*
*/
public void addProfile() {
System.out.println("-------------Add Profile------------");
Profile pf = new Profile(); //create new profile
pf.setID(getInputID(sc, "Enter ID: "));
pf.setName(getInputString(sc, "Enter name: "));
pf.setYearOfRegistration(getInputInt(sc, "Enter year of registration: "));
pf.setAddress(getInputString(sc, "Enter address: "));
this.profileList.add(pf); //add to array
System.out.println("Add a successful profile ");
}
/**
* method used to display the information of the profiles stored in the
* array.
*
*
*/
public void displayProfile() {
if (this.profileList.isEmpty()) { //check array already
System.out.println("There are no profiles yet!");
return;
}
System.out.println("ID Name Year Of Registration Addres"); //8/20/8/25
for (int i = 0; i < this.profileList.size(); ++i) { //approve profile in arraylist
Profile pf = this.profileList.get(i);
pf.disPlayInfo(); //show profile
}
}
/**
* method to load the file containing the profile information.
*
* @throws IOException
* @throws ClassNotFoundException
*/
public void loadFile() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("data.txt"); //create file
ObjectInputStream ois = new ObjectInputStream(fis); // create read file
this.profileList = (ArrayList<Profile>) ois.readObject(); //read file in array
ois.close(); // close ois
fis.close();// close ois
}
/**
* save file.
*
* @throws IOException
*/
public void saveFile() throws IOException {
File f = new File("data.txt");
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.profileList); //write file
System.out.println("Save file successfull!");
}
/**
* create file check if file already
*
* @throws IOException
* @throws java.lang.ClassNotFoundException
*/
public void createFileOrLoad() throws IOException, ClassNotFoundException {
File f = new File("data.txt");
if (f.exists()) {
loadFile();
System.out.println("Load file successfull!");
} else {
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.profileList);
System.out.println("create file successfull!");
}
}
/**
* check input integer.
*
* @param scanner
* @param message
* @return
*/
public int getInputInt(Scanner scanner, String message) {
Integer input = null;
while (input == null) {
try {
input = Integer.parseInt(getInputString(scanner, message)); //convert string to integer
if (input <= 1900) { // check input must greater 1900
System.out.println("Error: Year of registration must be greater than 1900!");
input = null;
}
} catch (NumberFormatException e) { //check if input character
System.out.println("Error: Your input must be a number !");
}
}
return input;
}
/**
* check if id already exists.
*
* @param string
* @return
*/
public boolean checkID(String string) {
for (int i = 0; i < this.profileList.size(); ++i) {
Profile pf = this.profileList.get(i);
if (string.equals(pf.getID())) { //check xem ID already
return true;
}
}
return false;
}
/**
* Input ID.
*
* @param scanner
* @param message
* @return
*/
public String getInputID(Scanner scanner, String message) {
String string = getInputString(scanner, message);
while (!string.matches(VALID_ID)) { //must have 4 characters
System.out.println("ID only allows input of 4 digits");
string = getInputString(scanner, message);
}
while (checkID(string)) { //check ID already exists!
System.out.println("The ID already exists!");
string = getInputString(scanner, message);
}
return string;
}
/**
* Input String.
*
* @param scanner
* @param message
* @return
*/
public String getInputString(Scanner scanner, String message) {
String string;
do {
System.out.print(message);
string = scanner.nextLine().trim();
if (string.equals("")) { //check empty
System.out.println("Error: Your input can not be empty!");
}
} while (string.equals("")); //if !empty
return string;
}
/**
* Input limit
*
* @return
*/
public int checkInputIntLimit() {
//loop until user input correct
while (true) {
try {
int input = Integer.parseInt(sc.nextLine().trim());
if (input < 0 || input > 3) {
throw new NumberFormatException();
}
return input;
} catch (NumberFormatException e) {
System.out.println("Please input number in rage [0-2]");
System.out.print("Enter again: ");
}
}
}
}
Editor is loading...