Untitled

 avatar
unknown
plain_text
4 months ago
2.2 kB
9
Indexable
package FileManagement;

import java.io.*;
import java.util.LinkedList;

public class FileManager {
	private LinkedList <String> managed ;
	private static String baselocation = "/home/ammar/Downloads/LocalDataBase/";
	private static String AllUsersDataPath = "/home/ammar/Downloads/LocalDataBase/AllUsersData";
	public FileManager () {
		managed = new LinkedList<String>();
	}
	public void add (String neww) {
		managed.add(neww);
	}
	public void remove (String rm) {
		managed.remove(rm);
	}
	public String get(int idx) {
		return managed.get(idx);
	}
	@SuppressWarnings("unchecked")
	public static LinkedList<User> UploadAllData(){
		LinkedList<User> data;
		File file = new File(AllUsersDataPath);
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
			data = (LinkedList<User>)ois.readObject();
			return data;
		} catch (IOException e) {
			System.err.println(e.getMessage());
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
		return null;		
	}
	public static void addUserInSystem(String uname) {
		String location = baselocation + uname;
		File file = new File(location);
		file.mkdir();
	}
	public static void deleteUserFromSystem(String uname) {
		String location = baselocation + uname;
		File file = new File(location);
		file.delete();
	}
	public static void updateAllUsersData(LinkedList<User> newData) {
		File file =  new File(AllUsersDataPath);
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(newData);
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	public static boolean isUserExist (User user) {
		LinkedList <User> data = FileManager.UploadAllData();
		int idx = 0;
		while(data.get(idx) != null) {
			if(data.get(idx).equals(user))return true;
			idx++;
		}		
		return false;
	}
	public static boolean updateUserData(User olduser , User newuser) {
		LinkedList <User> data = FileManager.UploadAllData();
		if(! FileManager.isUserExist(olduser)) return false;
		int idx = 0;
		while(data.get(idx) != null) {
			if(data.get(idx).equals(olduser)) {
				data.set(idx, newuser);
			}
			idx++;			
		}
		FileManager.updateAllUsersData(data);
		return true;
	}
}
Editor is loading...
Leave a Comment