Advertisement
Mitra23

Entiteti

May 28th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. package loadFiles;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10.  
  11. import buyers.Buyer;
  12. import employees.Administrator;
  13. import employees.Supplier;
  14. import enumeration.EnumGender;
  15. import enumeration.EnumVehicle;
  16. import user.Users;
  17.  
  18. public class LoadE {
  19.  
  20.  
  21. private static ArrayList<Users> users = new ArrayList<Users>();
  22. private static ArrayList<Buyer> buyer = new ArrayList<Buyer>();
  23. private static ArrayList<Administrator> admin = new ArrayList<Administrator>();
  24. private static ArrayList<Supplier> supplier = new ArrayList<Supplier>();
  25.  
  26. public LoadE() {
  27. LoadE.users = new ArrayList<Users>();
  28. LoadE.buyer = new ArrayList<Buyer>();
  29. LoadE.admin = new ArrayList<Administrator>();
  30. LoadE.supplier = new ArrayList<Supplier>();
  31. }
  32.  
  33.  
  34. public static ArrayList<Users> getUsers() {
  35. return users;
  36. }
  37.  
  38. public static Users uName(String uname) {
  39. for(Users u: users) {
  40. if(u.getUsername().equals(uname)) {
  41. return u;
  42. }
  43. }
  44. return null;
  45. }
  46.  
  47.  
  48. public static ArrayList<Buyer> getBuyer() {
  49. return buyer;
  50. }
  51.  
  52. public static Buyer bName(String uname) {
  53. for(Buyer b: buyer) {
  54. if(b.getUsername().equals(uname)) {
  55. return b;
  56. }
  57. }
  58. return null;
  59. }
  60.  
  61.  
  62. public static void deleteBuyer(Buyer index) {
  63. LoadE.buyer.remove(index);
  64. }
  65.  
  66. public static ArrayList<Administrator> getAdmin() {
  67. return admin;
  68. }
  69.  
  70. public static Administrator aName(String uname) {
  71. for(Administrator a:admin) {
  72. if(a.getUsername().equals(uname))
  73. return a;
  74. }
  75. return null;
  76. }
  77.  
  78. public static void deleteAdmin(Administrator adm) {
  79. LoadE.admin.remove(adm);
  80. }
  81.  
  82. public static ArrayList<Supplier> getSupplier() {
  83. return supplier;
  84. }
  85.  
  86. public static Supplier sName(String uname) {
  87. for(Supplier s: supplier) {
  88. if(s.getUsername().equals(uname)) {
  89. return s;
  90. }
  91. }
  92. return null;
  93. }
  94.  
  95. public static void deleteSupplier(Supplier supl) {
  96. LoadE.supplier.remove(supl);
  97. }
  98.  
  99. public static void readBuyers() {
  100.  
  101. try {
  102. File buyerFile = new File("src/projectData/buyers.txt");
  103. BufferedReader reader = new BufferedReader(new FileReader(buyerFile));
  104. String line;
  105. while((line = reader.readLine()) != null) {
  106. String[] splitLine = line.split("\\|");
  107. String name = splitLine[0];
  108. String surname = splitLine[1];
  109. int genderInt = Integer.parseInt(splitLine[2]);
  110. EnumGender gender = EnumGender.valueOf(genderInt);
  111. String username = splitLine[3];
  112. String password = splitLine[4];
  113. String address = splitLine[5];
  114. String phoneNum = splitLine[6];
  115. Buyer b = new Buyer(name, surname, gender, username, password, address, phoneNum);
  116. buyer.add(b);
  117.  
  118.  
  119. }
  120. reader.close();
  121. }
  122. catch(IOException e) {
  123. System.out.println("Greska prilikom ucitavanja fajla buyers.");
  124. }
  125.  
  126. }
  127.  
  128. public static void readAdmins() {
  129. try {
  130. File adminFile = new File("src/projectData/admins.txt");
  131. BufferedReader readAdmin = new BufferedReader(new FileReader(adminFile));
  132. String line1;
  133. while((line1 = readAdmin.readLine()) != null) {
  134. String[] splitAdmin = line1.split("\\|");
  135. String name = splitAdmin[0];
  136. String surname = splitAdmin[1];
  137. int genderInt = Integer.parseInt(splitAdmin[2]);
  138. EnumGender gender = EnumGender.valueOf(genderInt);
  139. String username = splitAdmin[3];
  140. String password = splitAdmin[4];
  141. String JMBG = splitAdmin[5];
  142. double salary = Double.parseDouble(splitAdmin[6]);
  143. Administrator a = new Administrator(name, surname, gender, username, password, JMBG, salary);
  144. admin.add(a);
  145. }
  146. readAdmin.close();
  147. }
  148. catch(IOException e) {
  149. System.out.println("Greska prilikom ucitavanja fajla admins.");
  150. }
  151. }
  152.  
  153. public static void readSuppliers() {
  154. try {
  155. File supplierFile = new File("src/projectData/suppliers.txt");
  156. BufferedReader readSupplier = new BufferedReader(new FileReader(supplierFile));
  157. String line2;
  158. while((line2 = readSupplier.readLine()) != null) {
  159. String[] splitSupplier = line2.split("\\|");
  160. String name = splitSupplier[0];
  161. String surname = splitSupplier[1];
  162. int genderInt = Integer.parseInt(splitSupplier[2]);
  163. EnumGender gender = EnumGender.valueOf(genderInt);
  164. String username = splitSupplier[3];
  165. String password = splitSupplier[4];
  166. String JMBG = splitSupplier[5];
  167. double salary = Double.parseDouble(splitSupplier[6]);
  168. String driversLicence = splitSupplier[7];
  169. int vehicleTypeInt = Integer.parseInt(splitSupplier[8]);
  170. EnumVehicle vehicleType = EnumVehicle.valueOf(vehicleTypeInt);
  171. Supplier s = new Supplier(name, surname, gender, username, password, JMBG, salary, driversLicence, vehicleType);
  172. supplier.add(s);
  173.  
  174. }
  175. readSupplier.close();
  176. }
  177. catch(IOException e) {
  178. System.out.println("Greska prilikom citanja fajla suppliers.");
  179. }
  180. }
  181.  
  182. public static void addUsers() {
  183. readBuyers();
  184. readAdmins();
  185. readSuppliers();
  186. for(int i = 0; i < buyer.size(); i++) {
  187. System.out.println(buyer);
  188. users.addAll(buyer);
  189. }
  190. for(int i = 0; i < admin.size(); i++) {
  191. System.out.println(admin);
  192. users.addAll(admin);
  193. }
  194. for(int i = 0; i < supplier.size(); i++) {
  195. System.out.println(supplier);
  196. users.addAll(supplier);
  197. }
  198. }
  199.  
  200. public static void writeBuyers() {
  201. try {
  202. File file = new File("src/projectData/buyers.txt");
  203. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  204. if(!file.exists()) {
  205. try {
  206. file.createNewFile();
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. }
  210. System.out.println("Kreiran novi fajl.");
  211. }else {
  212. System.out.println("Fajl vec postoji.");
  213. }
  214. String content = "";
  215. for(Buyer b: buyer) {
  216. content += b.getName() + "|" + b.getSurname() + "|" + EnumGender.toInt(b.getGender()) + "|" +
  217. b.getUsername() + "|" + b.getPassword() + "|" + b.getAddress() + "|" + b.getPhoneNum() + "\n";
  218. }
  219. writer.write(content);
  220. writer.flush();
  221. writer.close();
  222. }
  223. catch(IOException e) {
  224. e.printStackTrace();
  225. }
  226.  
  227. }
  228.  
  229. public static void writeAdmins() {
  230. try {
  231. File file = new File("src/projectData/buyers.txt");
  232. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  233. if(!file.exists()) {
  234. try {
  235. file.createNewFile();
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. }
  239. System.out.println("Kreiran novi fajl.");
  240. }else {
  241. System.out.println("Fajl vec postoji.");
  242. }
  243. String content = "";
  244. for(Administrator a: admin) {
  245. content += a.getName() + "|" + a.getSurname() + "|" + EnumGender.toInt(a.getGender()) + "|" +
  246. a.getUsername() + "|" + a.getPassword() + "|" + a.getJMBG() + "|" + a.getSalary() + "\n";
  247. }
  248. writer.write(content);
  249. writer.flush();
  250. writer.close();
  251. }
  252. catch(IOException e) {
  253. e.printStackTrace();
  254. }
  255. }
  256.  
  257. public static void writeSuppliers() {
  258. try {
  259. File file = new File("src/projectData/buyers.txt");
  260. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  261. if(!file.exists()) {
  262. try {
  263. file.createNewFile();
  264. } catch (IOException e) {
  265. e.printStackTrace();
  266. }
  267. System.out.println("Kreiran novi fajl.");
  268. }else {
  269. System.out.println("Fajl vec postoji.");
  270. }
  271. String content = "";
  272. for(Supplier s: supplier) {
  273. content += s.getName() + "|" + s.getSurname() + "|" + EnumGender.toInt(s.getGender()) + "|" +
  274. s.getUsername() + "|" + s.getPassword() + "|" + s.getJMBG() + "|" + s.getSalary() + "|" +
  275. s.getDriversLicence() + "|" + s.getVehicleType() + "\n";
  276. }
  277. writer.write(content);
  278. writer.flush();
  279. writer.close();
  280. }
  281. catch(IOException e) {
  282. e.printStackTrace();
  283. }
  284. }
  285.  
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement