From 5523a113cfaf74a16971152f1b7aa25cdd50ddf4 Mon Sep 17 00:00:00 2001 From: evatarr <elt783@usask.ca> Date: Mon, 8 Mar 2021 13:33:45 -0600 Subject: [PATCH] Adding existing code Needs to be tested more and two commands need to be finished -addHotkey -currentProfiles --- .../profiles/commands/CommandStatus.java | 19 +++++ src/model/profiles/commands/addHotkey.java | 9 +++ src/model/profiles/commands/addProfile.java | 26 +++++++ src/model/profiles/commands/checkActive.java | 20 +++++ .../profiles/commands/currentProfiles.java | 24 ++++++ .../profiles/commands/deleteProfile.java | 20 +++++ src/model/profiles/commands/removeHotkey.java | 24 ++++++ src/model/profiles/commands/setActive.java | 19 +++++ .../profiles/database/profileDatabase.java | 40 ++++++++++ src/model/profiles/entities/profile.java | 75 +++++++++++++++++++ 10 files changed, 276 insertions(+) create mode 100644 src/model/profiles/commands/CommandStatus.java create mode 100644 src/model/profiles/commands/addHotkey.java create mode 100644 src/model/profiles/commands/addProfile.java create mode 100644 src/model/profiles/commands/checkActive.java create mode 100644 src/model/profiles/commands/currentProfiles.java create mode 100644 src/model/profiles/commands/deleteProfile.java create mode 100644 src/model/profiles/commands/removeHotkey.java create mode 100644 src/model/profiles/commands/setActive.java create mode 100644 src/model/profiles/database/profileDatabase.java create mode 100644 src/model/profiles/entities/profile.java diff --git a/src/model/profiles/commands/CommandStatus.java b/src/model/profiles/commands/CommandStatus.java new file mode 100644 index 0000000..2dcc4aa --- /dev/null +++ b/src/model/profiles/commands/CommandStatus.java @@ -0,0 +1,19 @@ +package model.profiles.commands; + +/** + * This is used to handle errors and avoid crashes + */ +public class CommandStatus { + protected boolean successful = false; + protected String errorMessage; + + public boolean wasSuccessful(){ + return successful; + } + public String getErrorMessage(){ + if(wasSuccessful()){ + throw new RuntimeException("Something went wrong :("); + } + return errorMessage; + } +} \ No newline at end of file diff --git a/src/model/profiles/commands/addHotkey.java b/src/model/profiles/commands/addHotkey.java new file mode 100644 index 0000000..ad50236 --- /dev/null +++ b/src/model/profiles/commands/addHotkey.java @@ -0,0 +1,9 @@ +package model.profiles.commands; + +/** + * Add a hot key to an existing profile + * TO DO + */ +public class addHotkey extends CommandStatus { + +} diff --git a/src/model/profiles/commands/addProfile.java b/src/model/profiles/commands/addProfile.java new file mode 100644 index 0000000..5f23ac7 --- /dev/null +++ b/src/model/profiles/commands/addProfile.java @@ -0,0 +1,26 @@ +package model.profiles.commands; + +import model.profiles.database.profileDatabase; +import model.profiles.entities.profile; + + +/** + * Used to add a profile to our database if the name given isn't already in our database + * This ONLY makes a profile with a name + * To add a Hotkey to our new profile use "addHotKey" command + */ +public class addProfile extends CommandStatus{ + public void AddProfile(String name){ + if(profileDatabase.database().containsKey(name)){ + successful = false; + errorMessage = "Profile with name "+ name +" already exists"; + } + else{ + //create a new profile containing the given name + profile add = new profile(name); + //add the newly made profile to our database + profileDatabase.database().put(name,add); + successful = true; + } + } +} diff --git a/src/model/profiles/commands/checkActive.java b/src/model/profiles/commands/checkActive.java new file mode 100644 index 0000000..df2c96f --- /dev/null +++ b/src/model/profiles/commands/checkActive.java @@ -0,0 +1,20 @@ +package model.profiles.commands; + +import model.profiles.database.profileDatabase; +import model.profiles.entities.profile; +/** + * Used to check what active profile we are using if any + */ +public class checkActive extends CommandStatus{ + public profile CheckActive(){ + profile p = new profile("No Active Profile"); + if(profileDatabase.getActive() == null){ + successful = false; + errorMessage = "No active profile"; + //Return an error profile + return p; + } + successful = true; + return profileDatabase.getActive(); + } +} \ No newline at end of file diff --git a/src/model/profiles/commands/currentProfiles.java b/src/model/profiles/commands/currentProfiles.java new file mode 100644 index 0000000..40c64e4 --- /dev/null +++ b/src/model/profiles/commands/currentProfiles.java @@ -0,0 +1,24 @@ +package model.profiles.commands; + +import java.util.Collection; +import model.profiles.entities.profile; +import model.profiles.database.profileDatabase; + +/** + * Get all the current profiles in the form of a String + * NOT DONE + */ +public class currentProfiles extends CommandStatus{ + public String findAllProfiles(){ + String allProfiles = ""; + Collection<profile> profiles = profileDatabase.database().values(); + for(profile p : profiles){ + if( p == profileDatabase.getActive()){ + allProfiles = allProfiles + "ACTIVE: "; + } + allProfiles = allProfiles + p + "\n"; + } + successful = true; + return allProfiles; + } +} diff --git a/src/model/profiles/commands/deleteProfile.java b/src/model/profiles/commands/deleteProfile.java new file mode 100644 index 0000000..204b83c --- /dev/null +++ b/src/model/profiles/commands/deleteProfile.java @@ -0,0 +1,20 @@ +package model.profiles.commands; + +import model.profiles.database.profileDatabase; +/** + * Remove an existing profile and use our error handling command CommandStatus + */ +public class deleteProfile extends CommandStatus { + public void DeleteProfile(String name){ + if(!profileDatabase.database().containsKey(name)){ + successful = false; + errorMessage = "Profile with name "+ name +" does not exist"; + } + else{ + //remove the profile from our database + profileDatabase.database().remove(name); + successful = true; + } + } + +} diff --git a/src/model/profiles/commands/removeHotkey.java b/src/model/profiles/commands/removeHotkey.java new file mode 100644 index 0000000..1059b6b --- /dev/null +++ b/src/model/profiles/commands/removeHotkey.java @@ -0,0 +1,24 @@ +package model.profiles.commands; + +import model.profiles.database.profileDatabase; +import model.profiles.entities.profile; + +/** + * Remove a hotkey from a profile + * NOT DONE + */ +public class removeHotkey extends CommandStatus{ + /** + * Remove a hot key from the given profiles hotkey treemap + * @param name name of the profile to remove from + * @param id id of the hotkey to be removed + */ + public void RemoveHotkey(String name, int id){ + if(!profileDatabase.database().containsKey(name)){ + successful = false; + errorMessage = "No profile with name: " + name; + } + profile p = profileDatabase.database().get(name); + p.removeHotKey(id); + } +} diff --git a/src/model/profiles/commands/setActive.java b/src/model/profiles/commands/setActive.java new file mode 100644 index 0000000..5ed6216 --- /dev/null +++ b/src/model/profiles/commands/setActive.java @@ -0,0 +1,19 @@ +package model.profiles.commands; + +import model.profiles.database.profileDatabase; +import model.profiles.entities.profile; + +/** + * Set the active profile to the given one with name = "name" + */ +public class setActive extends CommandStatus{ + public void SetActive(String name){ + if(!profileDatabase.database().containsKey(name)){ + successful = false; + errorMessage = "Profile with name "+ name +" does not exist"; + } + profile makeActive = profileDatabase.database().get(name); + profileDatabase.setActive(makeActive); + successful = true; + } +} diff --git a/src/model/profiles/database/profileDatabase.java b/src/model/profiles/database/profileDatabase.java new file mode 100644 index 0000000..5da5e5a --- /dev/null +++ b/src/model/profiles/database/profileDatabase.java @@ -0,0 +1,40 @@ +package model.profiles.database; + +import java.util.TreeMap; +import model.profiles.entities.profile; + +/** + * This is used to store all of our profiles + * We will be storing them in a TreeMap so removing and adding will be easier + */ +public class profileDatabase { + private profileDatabase(){}; + + //Active profile + private static profile active; + //Database of all our profiles + private static TreeMap<String,profile> database; + + public static TreeMap<String, profile> database() { + //Create our initial database if it doesn't exist + if (database == null) { + database = new TreeMap<String, profile>(); + } + return database; + } + /** + * Set active profile to be p + * @param p profile to be set as active + */ + public static void setActive(profile p){ + active = p; + } + + /** + * Get the active profile + * @return active profile + */ + public static profile getActive(){ + return active; + } +} \ No newline at end of file diff --git a/src/model/profiles/entities/profile.java b/src/model/profiles/entities/profile.java new file mode 100644 index 0000000..67fc183 --- /dev/null +++ b/src/model/profiles/entities/profile.java @@ -0,0 +1,75 @@ +package model.profiles.entities; + +import model.profiles.commands.*; +import model.Hotkey; + +import java.util.TreeMap; + +public class profile { + public String name; + private static TreeMap<Integer,Hotkey> HKeys; + /** + * Initializes an instance of a profile with no hotkeys + * @param name the name of our profile + */ + public profile(String name) { + this.name = name; + //Organized by their ID + HKeys = new TreeMap<Integer, Hotkey>(); + } + /** + * Get the name of the entities.profile + * @return the name of the entities.profile + */ + public String getName() { + + return this.name; + } + + /** + * Get a hotkey using its ID + * @param id the unique ID for the hotkey in our profile + * @return + */ + public Hotkey getHotKey(int id){ + + return HKeys.get(id); + } + + public void removeHotKey(int id){ + HKeys.remove(id); + } + + public static void main(String[] args) { + //Testing for profile class and getName() + String name1 = "Eva"; + String name2 = ""; + String name3 = "123456"; + String name4 = "\n"; + profile one = new profile(name1); + profile two = new profile(name2); + profile three = new profile(name3); + profile four = new profile(name4); + //System.out.println(one.getName()); + //System.out.println(two.getName()); + //System.out.println(three.getName()); + //System.out.println(four.getName()); + + //Testing for addProfile() and deleteProfile() + addProfile add = new addProfile(); + add.AddProfile(name1); + add.AddProfile(name3); + + //Set and check active account + setActive set = new setActive(); + set.SetActive(name1); + checkActive check = new checkActive(); + System.out.println(check.CheckActive().getName()); + + set.SetActive(name3); + System.out.println(check.CheckActive().getName()); + + + + } +} -- GitLab