Advertisement
Guest User

Untitled

a guest
May 20th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. import java.awt.CardLayout;
  2. import java.awt.Dimension;
  3. import java.awt.GridBagLayout;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Calendar;
  14. import java.util.Date;
  15. import java.util.Scanner;
  16.  
  17. import javax.swing.JFrame;
  18. import javax.swing.JPanel;
  19.  
  20. public class VampireManager {
  21.  
  22. private static ArrayList<String> rawUserData = null;
  23. private static ArrayList<User> loginUserData = null;
  24. private static Inventory inventory = new Inventory();
  25. private static File userFile = new File("user.txt");
  26. private static FileWriter userText;
  27. private static File bloodFile = new File("blood.txt");
  28. private static FileWriter bloodText;
  29. private static Scanner sc = null;
  30.  
  31. public static void main(String[] args) {
  32.  
  33. //instatiating the manager
  34. VampireManager system = new VampireManager();
  35.  
  36.  
  37. //initialising the users
  38. System.out.println("File exists: " + userFile.exists());
  39. try {
  40. sc = new Scanner(new FileReader(userFile));
  41. }
  42. catch (FileNotFoundException e) {
  43. System.out.println("Could not find the File: " + e);
  44. }
  45. while(sc.hasNext()) {
  46. String currentUser = sc.nextLine();
  47. rawUserData.add(currentUser);
  48. loginUserData.add(parseUser(currentUser));
  49. }
  50. sc.close();
  51.  
  52. //debug
  53. System.out.println("size " + loginUserData.size());
  54.  
  55. //initialising the inventory
  56. System.out.println("File exists: " + bloodFile.exists());
  57. try {
  58. sc = new Scanner(new FileReader(bloodFile));
  59. }
  60. catch (FileNotFoundException e) {
  61. System.out.println("Could not find the File: " + e);
  62. }
  63. while(sc.hasNext()) {
  64. String currentBlood = sc.nextLine();
  65. Blood newBlood = parseBlood(currentBlood);
  66. switch(newBlood.getBloodType()){
  67. case "ABNeg": inventory.addABNeg(newBlood); break;
  68. case "ABPos": inventory.addABPos(newBlood); break;
  69. case "ANeg": inventory.addANeg(newBlood); break;
  70. case "APos": inventory.addAPos(newBlood); break;
  71. case "BNeg": inventory.addBNeg(newBlood); break;
  72. case "BPos": inventory.addBPos(newBlood); break;
  73. case "ONeg": inventory.addONeg(newBlood); break;
  74. case "OPos": inventory.addOPos(newBlood); break;
  75. default: System.out.println("Cause, baby, now we got bad blood, You know it used to be mad love - TayTay");
  76. }
  77. }
  78. sc.close();
  79.  
  80. //debug
  81. System.out.println("size APos: " + inventory.getAPos().size());
  82. System.out.println("size BNeg: " + inventory.getBNeg().size());
  83.  
  84. //initiating the windowframe
  85. int WINDOW_WIDTH = 1280;
  86. int WINDOW_HEIGHT = 720;
  87. JFrame windowFrame = new JFrame();
  88. JPanel mainPanel = new JPanel(new CardLayout());
  89. mainPanel.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
  90. windowFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  91. windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  92. windowFrame.setVisible(true);
  93. windowFrame.add(mainPanel);
  94. // windowFrame.pack();
  95.  
  96. //initiating first panel (login)
  97. Login loginObj = new Login(loginUserData, system);
  98. JPanel loginPanel = new LoginPanel(new GridBagLayout(), loginObj);
  99. mainPanel.add(loginPanel, "login");
  100. windowFrame.pack();
  101. }
  102.  
  103. private VampireManager(){
  104. loginUserData = new ArrayList<User>();
  105. rawUserData = new ArrayList<String>();
  106. //initialising FileWriters
  107. try {
  108. FileWriter userText = new FileWriter(userFile.getAbsolutePath(), true);
  109. } catch (IOException e) {
  110. System.out.println("userText not found: " + e);;
  111. }
  112. try {
  113. FileWriter bloodText = new FileWriter(bloodFile.getAbsolutePath(), true);
  114. } catch (IOException e) {
  115. System.out.println("userText not found: " + e);;
  116. }
  117. }
  118.  
  119. public void gotoProfile(User target){
  120. int id = target.getUserID();
  121. String rawString = "";
  122. for(String rData: rawUserData){
  123. String fields[] = rData.split(";");
  124. if (Integer.parseInt(fields[0]) == id){
  125. rawString = rData;
  126. break;
  127. }
  128. }
  129. if(rawString == ""){
  130. System.out.println("rawString with id could not be found");
  131. }else{
  132. String params[] = rawString.split(";");
  133. switch(target.getType()){
  134. case "PublicUser": gotoPublicUser(target, params) ; break;
  135. case "Batmobile": gotoBatmobile(target, params) ; break;
  136. case "MedicalFacility": gotoMedicalFacility(target, params) ; break;
  137. case "Employee": gotoEmployee(target, params) ; break;
  138. default: System.out.println("help me... i can't find the user type");
  139. }
  140. }
  141. }
  142.  
  143.  
  144.  
  145. private void gotoPublicUser(User original, String[] params) {
  146. String firstn = params[4];
  147. String lastn = params[5];
  148. String location = params[6];
  149. //initialising blood array
  150. String history = params[7];
  151. ArrayList<Blood> bloodHistory = new ArrayList<Blood>();
  152. String simpleBloodIDs[] = history.split(",");
  153. ArrayList<String> bloodIDs = new ArrayList<String>( Arrays.asList(simpleBloodIDs));
  154. searchBlood(bloodIDs, bloodHistory, inventory.getABNeg());
  155. searchBlood(bloodIDs, bloodHistory, inventory.getABPos());
  156. searchBlood(bloodIDs, bloodHistory, inventory.getANeg());
  157. searchBlood(bloodIDs, bloodHistory, inventory.getAPos());
  158. searchBlood(bloodIDs, bloodHistory, inventory.getBNeg());
  159. searchBlood(bloodIDs, bloodHistory, inventory.getBPos());
  160. searchBlood(bloodIDs, bloodHistory, inventory.getONeg());
  161. searchBlood(bloodIDs, bloodHistory, inventory.getOPos());
  162. PublicUser target = new PublicUser(original, firstn, lastn, location, bloodHistory);
  163. */
  164. //CHANGE THE PANEL TO PUBLICUSER
  165. }
  166.  
  167. private void gotoBatmobile(User original, String[] params) {
  168. String location = params[4];
  169. Batmobile target = new Batmobile(original, location, inventory);
  170. //CHANGE THE PANEL TO BATMOBILE
  171. }
  172.  
  173. private void gotoMedicalFacility(User original, String[] params) {
  174. String name = params[4];
  175. String location = params[5];
  176. MedicalFacility target = new MedicalFacility(original, name, location, inventory);
  177. //CHANGE THE PANEL TO MEDICALFACILITY
  178. }
  179.  
  180. private void gotoEmployee(User original, String[] params) {
  181. String fname = params[4];
  182. String lname = params[5];
  183. Employee target = new Employee(original, fname, lname, inventory);
  184. //CHANGE THE PANEL TO EMPLOYEE
  185. }
  186.  
  187. public void addUser(PublicUser toAdd){
  188. //formation of the rawString
  189. String rawString = toAdd.getUserID()
  190. + ";" + toAdd.getUsername()
  191. + ";" + toAdd.getPassword()
  192. + ";" + toAdd.getType()
  193. + ";" + toAdd.getFirstName()
  194. + ";" + toAdd.getLastName()
  195. + ";" + toAdd.getLocation()
  196. + ";" + historyToString(toAdd.getHistory());
  197.  
  198. //adding to the loginUser and rawData arrays
  199. User newUser = parseUser(rawString);
  200. loginUserData.add(newUser);
  201. rawUserData.add(rawString); //makes assumption it adds to the end of the array
  202. //adding to the text file
  203. try {
  204. userText.write(rawString);
  205. } catch (IOException e) {
  206. System.out.println("newUser wasn't added to the text file: " + e);
  207. }
  208. }
  209.  
  210. //SEARCHERS
  211. private void searchBlood(ArrayList<String> bloodIDs, ArrayList<Blood> toAddto, ArrayList<Blood> toSearch){
  212. for(Blood currentBlood: toSearch){
  213. for (String currentID: bloodIDs){
  214. if(currentBlood.getID() == Integer.parseInt(currentID)){
  215. toAddto.add(currentBlood);
  216.  
  217. }
  218. }
  219. }
  220. }
  221.  
  222. //PARSERS and CONVERTERS
  223. private static String historyToString(ArrayList<Blood> toConvert){
  224. String retString = "";
  225. for(Blood toAdd: toConvert){
  226. retString += Integer.toString(toAdd.getID()) + ",";
  227. }
  228. return retString;
  229. }
  230.  
  231. private static Blood parseBlood(String toParse){
  232. String params[] = toParse.split(";");
  233.  
  234. //debugging println
  235. for(int i = 0; i < params.length; i++){
  236. System.out.println(params[i]);
  237. }
  238.  
  239. //setting up blood
  240. int id = Integer.parseInt(params[0]);
  241. String bloodType = params[1];
  242. String location = params[2];
  243. Calendar date = parseDate(params[3]);
  244. String donorUsername = params[4];
  245. return new Blood(id, bloodType, location, date, donorUsername);
  246. }
  247.  
  248. private static User parseUser(String toParse){
  249. String params[] = toParse.split(";");
  250.  
  251. //debugging println
  252. for(int i = 0; i < params.length; i++){
  253. System.out.println(params[i]);
  254. }
  255.  
  256. //setting up user parameters
  257. int id = Integer.parseInt(params[0]);
  258. String username = params[1];
  259. String password = params[2];
  260. String type = params[3];
  261. return new User(id, username, password, type);
  262. }
  263.  
  264. //converts a string in the form dd-MMM-yyyy into a calendar object
  265. private static Calendar parseDate (String toParse) {
  266. String strDate = toParse;
  267. Date tmpDate = null;
  268. try {
  269. tmpDate = new SimpleDateFormat("dd-MMM-yyyy").parse(strDate);
  270. } catch (ParseException error) {
  271. System.out.println(error);
  272. }
  273. Calendar tempCal = Calendar.getInstance();
  274. tempCal.setTime(tmpDate);
  275. return tempCal;
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement