Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package loadFiles;
- 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.util.ArrayList;
- import buyers.Buyer;
- import employees.Administrator;
- import employees.Supplier;
- import enumeration.EnumGender;
- import enumeration.EnumVehicle;
- import user.Users;
- public class LoadE {
- private static ArrayList<Users> users = new ArrayList<Users>();
- private static ArrayList<Buyer> buyer = new ArrayList<Buyer>();
- private static ArrayList<Administrator> admin = new ArrayList<Administrator>();
- private static ArrayList<Supplier> supplier = new ArrayList<Supplier>();
- public LoadE() {
- LoadE.users = new ArrayList<Users>();
- LoadE.buyer = new ArrayList<Buyer>();
- LoadE.admin = new ArrayList<Administrator>();
- LoadE.supplier = new ArrayList<Supplier>();
- }
- public static ArrayList<Users> getUsers() {
- return users;
- }
- public static Users uName(String uname) {
- for(Users u: users) {
- if(u.getUsername().equals(uname)) {
- return u;
- }
- }
- return null;
- }
- public static ArrayList<Buyer> getBuyer() {
- return buyer;
- }
- public static Buyer bName(String uname) {
- for(Buyer b: buyer) {
- if(b.getUsername().equals(uname)) {
- return b;
- }
- }
- return null;
- }
- public static void deleteBuyer(Buyer index) {
- LoadE.buyer.remove(index);
- }
- public static ArrayList<Administrator> getAdmin() {
- return admin;
- }
- public static Administrator aName(String uname) {
- for(Administrator a:admin) {
- if(a.getUsername().equals(uname))
- return a;
- }
- return null;
- }
- public static void deleteAdmin(Administrator adm) {
- LoadE.admin.remove(adm);
- }
- public static ArrayList<Supplier> getSupplier() {
- return supplier;
- }
- public static Supplier sName(String uname) {
- for(Supplier s: supplier) {
- if(s.getUsername().equals(uname)) {
- return s;
- }
- }
- return null;
- }
- public static void deleteSupplier(Supplier supl) {
- LoadE.supplier.remove(supl);
- }
- public static void readBuyers() {
- try {
- File buyerFile = new File("src/projectData/buyers.txt");
- BufferedReader reader = new BufferedReader(new FileReader(buyerFile));
- String line;
- while((line = reader.readLine()) != null) {
- String[] splitLine = line.split("\\|");
- String name = splitLine[0];
- String surname = splitLine[1];
- int genderInt = Integer.parseInt(splitLine[2]);
- EnumGender gender = EnumGender.valueOf(genderInt);
- String username = splitLine[3];
- String password = splitLine[4];
- String address = splitLine[5];
- String phoneNum = splitLine[6];
- Buyer b = new Buyer(name, surname, gender, username, password, address, phoneNum);
- buyer.add(b);
- }
- reader.close();
- }
- catch(IOException e) {
- System.out.println("Greska prilikom ucitavanja fajla buyers.");
- }
- }
- public static void readAdmins() {
- try {
- File adminFile = new File("src/projectData/admins.txt");
- BufferedReader readAdmin = new BufferedReader(new FileReader(adminFile));
- String line1;
- while((line1 = readAdmin.readLine()) != null) {
- String[] splitAdmin = line1.split("\\|");
- String name = splitAdmin[0];
- String surname = splitAdmin[1];
- int genderInt = Integer.parseInt(splitAdmin[2]);
- EnumGender gender = EnumGender.valueOf(genderInt);
- String username = splitAdmin[3];
- String password = splitAdmin[4];
- String JMBG = splitAdmin[5];
- double salary = Double.parseDouble(splitAdmin[6]);
- Administrator a = new Administrator(name, surname, gender, username, password, JMBG, salary);
- admin.add(a);
- }
- readAdmin.close();
- }
- catch(IOException e) {
- System.out.println("Greska prilikom ucitavanja fajla admins.");
- }
- }
- public static void readSuppliers() {
- try {
- File supplierFile = new File("src/projectData/suppliers.txt");
- BufferedReader readSupplier = new BufferedReader(new FileReader(supplierFile));
- String line2;
- while((line2 = readSupplier.readLine()) != null) {
- String[] splitSupplier = line2.split("\\|");
- String name = splitSupplier[0];
- String surname = splitSupplier[1];
- int genderInt = Integer.parseInt(splitSupplier[2]);
- EnumGender gender = EnumGender.valueOf(genderInt);
- String username = splitSupplier[3];
- String password = splitSupplier[4];
- String JMBG = splitSupplier[5];
- double salary = Double.parseDouble(splitSupplier[6]);
- String driversLicence = splitSupplier[7];
- int vehicleTypeInt = Integer.parseInt(splitSupplier[8]);
- EnumVehicle vehicleType = EnumVehicle.valueOf(vehicleTypeInt);
- Supplier s = new Supplier(name, surname, gender, username, password, JMBG, salary, driversLicence, vehicleType);
- supplier.add(s);
- }
- readSupplier.close();
- }
- catch(IOException e) {
- System.out.println("Greska prilikom citanja fajla suppliers.");
- }
- }
- public static void addUsers() {
- readBuyers();
- readAdmins();
- readSuppliers();
- for(int i = 0; i < buyer.size(); i++) {
- System.out.println(buyer);
- users.addAll(buyer);
- }
- for(int i = 0; i < admin.size(); i++) {
- System.out.println(admin);
- users.addAll(admin);
- }
- for(int i = 0; i < supplier.size(); i++) {
- System.out.println(supplier);
- users.addAll(supplier);
- }
- }
- public static void writeBuyers() {
- try {
- File file = new File("src/projectData/buyers.txt");
- BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- if(!file.exists()) {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Kreiran novi fajl.");
- }else {
- System.out.println("Fajl vec postoji.");
- }
- String content = "";
- for(Buyer b: buyer) {
- content += b.getName() + "|" + b.getSurname() + "|" + EnumGender.toInt(b.getGender()) + "|" +
- b.getUsername() + "|" + b.getPassword() + "|" + b.getAddress() + "|" + b.getPhoneNum() + "\n";
- }
- writer.write(content);
- writer.flush();
- writer.close();
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- public static void writeAdmins() {
- try {
- File file = new File("src/projectData/buyers.txt");
- BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- if(!file.exists()) {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Kreiran novi fajl.");
- }else {
- System.out.println("Fajl vec postoji.");
- }
- String content = "";
- for(Administrator a: admin) {
- content += a.getName() + "|" + a.getSurname() + "|" + EnumGender.toInt(a.getGender()) + "|" +
- a.getUsername() + "|" + a.getPassword() + "|" + a.getJMBG() + "|" + a.getSalary() + "\n";
- }
- writer.write(content);
- writer.flush();
- writer.close();
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- public static void writeSuppliers() {
- try {
- File file = new File("src/projectData/buyers.txt");
- BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- if(!file.exists()) {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Kreiran novi fajl.");
- }else {
- System.out.println("Fajl vec postoji.");
- }
- String content = "";
- for(Supplier s: supplier) {
- content += s.getName() + "|" + s.getSurname() + "|" + EnumGender.toInt(s.getGender()) + "|" +
- s.getUsername() + "|" + s.getPassword() + "|" + s.getJMBG() + "|" + s.getSalary() + "|" +
- s.getDriversLicence() + "|" + s.getVehicleType() + "\n";
- }
- writer.write(content);
- writer.flush();
- writer.close();
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement