Skip to content
Snippets Groups Projects
Commit 5523a113 authored by evatarr's avatar evatarr
Browse files

Adding existing code Needs to be tested more and two commands need to be...

Adding existing code Needs to be tested more and two commands need to be finished -addHotkey -currentProfiles
parent 4d82a6d9
No related branches found
No related tags found
2 merge requests!45Prototype to Master for Milestone 5,!43Feature profiles
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
package model.profiles.commands;
/**
* Add a hot key to an existing profile
* TO DO
*/
public class addHotkey extends CommandStatus {
}
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;
}
}
}
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
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;
}
}
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;
}
}
}
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);
}
}
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;
}
}
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
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment