Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.CardLayout;
- import java.awt.Dimension;
- import java.awt.GridBagLayout;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Scanner;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class VampireManager {
- private static ArrayList<String> rawUserData = null;
- private static ArrayList<User> loginUserData = null;
- private static Inventory inventory = new Inventory();
- private static File userFile = new File("user.txt");
- private static FileWriter userText;
- private static File bloodFile = new File("blood.txt");
- private static FileWriter bloodText;
- private static Scanner sc = null;
- public static void main(String[] args) {
- //instatiating the manager
- VampireManager system = new VampireManager();
- //initialising the users
- System.out.println("File exists: " + userFile.exists());
- try {
- sc = new Scanner(new FileReader(userFile));
- }
- catch (FileNotFoundException e) {
- System.out.println("Could not find the File: " + e);
- }
- while(sc.hasNext()) {
- String currentUser = sc.nextLine();
- rawUserData.add(currentUser);
- loginUserData.add(parseUser(currentUser));
- }
- sc.close();
- //debug
- System.out.println("size " + loginUserData.size());
- //initialising the inventory
- System.out.println("File exists: " + bloodFile.exists());
- try {
- sc = new Scanner(new FileReader(bloodFile));
- }
- catch (FileNotFoundException e) {
- System.out.println("Could not find the File: " + e);
- }
- while(sc.hasNext()) {
- String currentBlood = sc.nextLine();
- Blood newBlood = parseBlood(currentBlood);
- switch(newBlood.getBloodType()){
- case "ABNeg": inventory.addABNeg(newBlood); break;
- case "ABPos": inventory.addABPos(newBlood); break;
- case "ANeg": inventory.addANeg(newBlood); break;
- case "APos": inventory.addAPos(newBlood); break;
- case "BNeg": inventory.addBNeg(newBlood); break;
- case "BPos": inventory.addBPos(newBlood); break;
- case "ONeg": inventory.addONeg(newBlood); break;
- case "OPos": inventory.addOPos(newBlood); break;
- default: System.out.println("Cause, baby, now we got bad blood, You know it used to be mad love - TayTay");
- }
- }
- sc.close();
- //debug
- System.out.println("size APos: " + inventory.getAPos().size());
- System.out.println("size BNeg: " + inventory.getBNeg().size());
- //initiating the windowframe
- int WINDOW_WIDTH = 1280;
- int WINDOW_HEIGHT = 720;
- JFrame windowFrame = new JFrame();
- JPanel mainPanel = new JPanel(new CardLayout());
- mainPanel.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
- windowFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- windowFrame.setVisible(true);
- windowFrame.add(mainPanel);
- // windowFrame.pack();
- //initiating first panel (login)
- Login loginObj = new Login(loginUserData, system);
- JPanel loginPanel = new LoginPanel(new GridBagLayout(), loginObj);
- mainPanel.add(loginPanel, "login");
- windowFrame.pack();
- }
- private VampireManager(){
- loginUserData = new ArrayList<User>();
- rawUserData = new ArrayList<String>();
- //initialising FileWriters
- try {
- FileWriter userText = new FileWriter(userFile.getAbsolutePath(), true);
- } catch (IOException e) {
- System.out.println("userText not found: " + e);;
- }
- try {
- FileWriter bloodText = new FileWriter(bloodFile.getAbsolutePath(), true);
- } catch (IOException e) {
- System.out.println("userText not found: " + e);;
- }
- }
- public void gotoProfile(User target){
- int id = target.getUserID();
- String rawString = "";
- for(String rData: rawUserData){
- String fields[] = rData.split(";");
- if (Integer.parseInt(fields[0]) == id){
- rawString = rData;
- break;
- }
- }
- if(rawString == ""){
- System.out.println("rawString with id could not be found");
- }else{
- String params[] = rawString.split(";");
- switch(target.getType()){
- case "PublicUser": gotoPublicUser(target, params) ; break;
- case "Batmobile": gotoBatmobile(target, params) ; break;
- case "MedicalFacility": gotoMedicalFacility(target, params) ; break;
- case "Employee": gotoEmployee(target, params) ; break;
- default: System.out.println("help me... i can't find the user type");
- }
- }
- }
- private void gotoPublicUser(User original, String[] params) {
- String firstn = params[4];
- String lastn = params[5];
- String location = params[6];
- //initialising blood array
- String history = params[7];
- ArrayList<Blood> bloodHistory = new ArrayList<Blood>();
- String simpleBloodIDs[] = history.split(",");
- ArrayList<String> bloodIDs = new ArrayList<String>( Arrays.asList(simpleBloodIDs));
- searchBlood(bloodIDs, bloodHistory, inventory.getABNeg());
- searchBlood(bloodIDs, bloodHistory, inventory.getABPos());
- searchBlood(bloodIDs, bloodHistory, inventory.getANeg());
- searchBlood(bloodIDs, bloodHistory, inventory.getAPos());
- searchBlood(bloodIDs, bloodHistory, inventory.getBNeg());
- searchBlood(bloodIDs, bloodHistory, inventory.getBPos());
- searchBlood(bloodIDs, bloodHistory, inventory.getONeg());
- searchBlood(bloodIDs, bloodHistory, inventory.getOPos());
- PublicUser target = new PublicUser(original, firstn, lastn, location, bloodHistory);
- */
- //CHANGE THE PANEL TO PUBLICUSER
- }
- private void gotoBatmobile(User original, String[] params) {
- String location = params[4];
- Batmobile target = new Batmobile(original, location, inventory);
- //CHANGE THE PANEL TO BATMOBILE
- }
- private void gotoMedicalFacility(User original, String[] params) {
- String name = params[4];
- String location = params[5];
- MedicalFacility target = new MedicalFacility(original, name, location, inventory);
- //CHANGE THE PANEL TO MEDICALFACILITY
- }
- private void gotoEmployee(User original, String[] params) {
- String fname = params[4];
- String lname = params[5];
- Employee target = new Employee(original, fname, lname, inventory);
- //CHANGE THE PANEL TO EMPLOYEE
- }
- public void addUser(PublicUser toAdd){
- //formation of the rawString
- String rawString = toAdd.getUserID()
- + ";" + toAdd.getUsername()
- + ";" + toAdd.getPassword()
- + ";" + toAdd.getType()
- + ";" + toAdd.getFirstName()
- + ";" + toAdd.getLastName()
- + ";" + toAdd.getLocation()
- + ";" + historyToString(toAdd.getHistory());
- //adding to the loginUser and rawData arrays
- User newUser = parseUser(rawString);
- loginUserData.add(newUser);
- rawUserData.add(rawString); //makes assumption it adds to the end of the array
- //adding to the text file
- try {
- userText.write(rawString);
- } catch (IOException e) {
- System.out.println("newUser wasn't added to the text file: " + e);
- }
- }
- //SEARCHERS
- private void searchBlood(ArrayList<String> bloodIDs, ArrayList<Blood> toAddto, ArrayList<Blood> toSearch){
- for(Blood currentBlood: toSearch){
- for (String currentID: bloodIDs){
- if(currentBlood.getID() == Integer.parseInt(currentID)){
- toAddto.add(currentBlood);
- }
- }
- }
- }
- //PARSERS and CONVERTERS
- private static String historyToString(ArrayList<Blood> toConvert){
- String retString = "";
- for(Blood toAdd: toConvert){
- retString += Integer.toString(toAdd.getID()) + ",";
- }
- return retString;
- }
- private static Blood parseBlood(String toParse){
- String params[] = toParse.split(";");
- //debugging println
- for(int i = 0; i < params.length; i++){
- System.out.println(params[i]);
- }
- //setting up blood
- int id = Integer.parseInt(params[0]);
- String bloodType = params[1];
- String location = params[2];
- Calendar date = parseDate(params[3]);
- String donorUsername = params[4];
- return new Blood(id, bloodType, location, date, donorUsername);
- }
- private static User parseUser(String toParse){
- String params[] = toParse.split(";");
- //debugging println
- for(int i = 0; i < params.length; i++){
- System.out.println(params[i]);
- }
- //setting up user parameters
- int id = Integer.parseInt(params[0]);
- String username = params[1];
- String password = params[2];
- String type = params[3];
- return new User(id, username, password, type);
- }
- //converts a string in the form dd-MMM-yyyy into a calendar object
- private static Calendar parseDate (String toParse) {
- String strDate = toParse;
- Date tmpDate = null;
- try {
- tmpDate = new SimpleDateFormat("dd-MMM-yyyy").parse(strDate);
- } catch (ParseException error) {
- System.out.println(error);
- }
- Calendar tempCal = Calendar.getInstance();
- tempCal.setTime(tmpDate);
- return tempCal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement