Thread: Donation Manager "Donator Membership"

Page 1 of 3 123 LastLast
Results 1 to 10 of 29
  1. #1 Donation Manager "Donator Membership" 
    Member
    Nour's Avatar
    Join Date
    May 2007
    Posts
    472
    Thanks given
    127
    Thanks received
    61
    Rep Power
    569
    Purpose: To add a simple system of membership or "donator membership"

    Difficulty: -1

    Assumed Knowledge: Copy and paste

    Classes Modified: Client / Command classes?


    For awhile now I've seen some pretty odd looking donation management systems this one is very basic, but it's an improvement over most.

    Code:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.HashMap;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    /**
     * Donation Manager; supports only lifetime memberships
     * 
     * @author Nour
     * 
     */
    
    public class DonationManager {
    	
    	public DonationManager() {
    		donators = new HashMap<String, String>();
    		try {
    			importDonators();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public HashMap<String, String> donators;
    	
    	public static String FILE_PATH = ""; //path to donators.xml
    	public static boolean EXPORT_AFTER_ADDITION = true; //Recommended true, false if you have other ideas for saving
    	
    	
    	public int random(int range) {
    		return (int) (java.lang.Math.random() * (range + 1));
    	}
    	
    	public boolean isDonator(String name) {
    		return donators.containsValue(name);
    	}
    	
    	public String generateKey(int length) {
    		String choices = "abcdefghijklmnopqrstuvwxyz1234567890";
    		StringBuilder sb = new StringBuilder();
    		for(int i = 0; i < length; i++) {
    			sb.append(choices.charAt(random(choices.length() - 1)));
    		}
    		return sb.toString();
    	}
    	
    	
    	public void exportDonators() {
    		try {
    			BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_PATH+"donators.xml"));
    			String ENCODING = "ISO-8859-1";
    			bw.write("<?xml version=\"1.0\" encoding=\""+ENCODING+"\"?>");
    			bw.newLine();
    			bw.write("<DONATIONS>");
    			bw.newLine();
    			for(String s : donators.keySet()) {
    				bw.write("<DONATION><KEY>"+s+"</KEY><USER>"+donators.get(s)+"</USER></DONATION>");
    				bw.newLine();
    			}
    			bw.write("</DONATIONS>");
    			bw.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void addDonator(String name, int keylength) {
    		String key = generateKey(keylength);
    		if(donators.containsKey(key)) {
    			key = generateKey(keylength + 1);
    		}
    		donators.put(key, name);
    		if(EXPORT_AFTER_ADDITION)
    			exportDonators();
    	}
    	
    	public void addDonator(String name) {
    		String key = generateKey(8);
    		if(donators.containsKey(key)) {
    			key = generateKey(8 + random(4));
    		}
    		donators.put(key, name);
    		if(EXPORT_AFTER_ADDITION)
    			exportDonators();
    	}
    	
    	public void importDonators() throws ParserConfigurationException, SAXException, IOException {
    		donators.clear();
    		File in = new File(FILE_PATH+"donators.xml");
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		Document doc = db.parse(in);
    		doc.getDocumentElement().normalize();
    		NodeList nodeLst = doc.getElementsByTagName("DONATION");
    		 for (int i = 0; i < nodeLst.getLength(); i++) {
    			    Node fstNode = nodeLst.item(i);
    			    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
    			           Element fstElmnt = (Element) fstNode;
    			           NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("KEY");
    			           Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    			           NodeList fstNm = fstNmElmnt.getChildNodes();
    			           String key = ((Node) fstNm.item(0)).getNodeValue();
    			           NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("USER");
    			           Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
    			           NodeList lstNm = lstNmElmnt.getChildNodes();
    			           String donator = ((Node) lstNm.item(0)).getNodeValue();
    			           donators.put(key, donator);
    			    }
    		 }
    	}
    	
    }
    Save that as DonationManager.java

    The only other step besides that is to add your own way of creating a way of adding the donators into the donation list. You can use the addDonator method to do so.

    I'll make a command for an example.

    Code:
    if(command.startsWith("ad")) {
    	String name = command.substring(3);
    	donationManagerObject.addDonator(name);
    }
    RIP Emps-scape 2006-2013.
    [SPOIL]
    [/SPOIL]

    i need 2 herd sum bots
    i need 2 steal sum zombies
    i need 2 open many slots
    to hold my ****in zombies
    Reply With Quote  
     

  2. #2  
    Banned
    Join Date
    Feb 2009
    Age
    26
    Posts
    273
    Thanks given
    4
    Thanks received
    4
    Rep Power
    0
    Looks nice, thanks chris i appreciate it.
    Reply With Quote  
     

  3. #3  
    Banned

    Join Date
    Jul 2009
    Posts
    915
    Thanks given
    1
    Thanks received
    10
    Rep Power
    0
    Not bad...nice to see people working on this. I prefer xml loading though or even mysql works best
    Reply With Quote  
     

  4. #4  
    Donator

    tj007razor's Avatar
    Join Date
    Feb 2007
    Posts
    895
    Thanks given
    2
    Thanks received
    9
    Rep Power
    210
    What is the point of your key? You don't actually use it for anything. You sould just use a set of names instead of a set of member keys then names, since your isDonator method just checks values anyway(You aren't using the key at all).

    [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #5  
    Member
    Nour's Avatar
    Join Date
    May 2007
    Posts
    472
    Thanks given
    127
    Thanks received
    61
    Rep Power
    569
    Quote Originally Posted by Pytherion View Post
    Not bad...nice to see people working on this. I prefer xml loading though or even mysql works best
    This is XML....?


    Quote Originally Posted by tj007razor View Post
    What is the point of your key? You don't actually use it for anything. You sould just use a set of names instead of a set of member keys then names, since your isDonator method just checks values anyway(You aren't using the key at all).
    I had that in there because i designed this originally for the key to be a "conformation code" of sorts, in this i left it in because some people might use it for account recovery, etc.
    RIP Emps-scape 2006-2013.
    [SPOIL]
    [/SPOIL]

    i need 2 herd sum bots
    i need 2 steal sum zombies
    i need 2 open many slots
    to hold my ****in zombies
    Reply With Quote  
     

  6. #6  
    Donator

    tj007razor's Avatar
    Join Date
    Feb 2007
    Posts
    895
    Thanks given
    2
    Thanks received
    9
    Rep Power
    210
    Ah, that makes more sense.

    [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. #7  
    Member
    Nour's Avatar
    Join Date
    May 2007
    Posts
    472
    Thanks given
    127
    Thanks received
    61
    Rep Power
    569
    Quote Originally Posted by tj007razor View Post
    Ah, that makes more sense.
    Yeah, a friend of mine runs a server that uses a similar method for account recovery.
    RIP Emps-scape 2006-2013.
    [SPOIL]
    [/SPOIL]

    i need 2 herd sum bots
    i need 2 steal sum zombies
    i need 2 open many slots
    to hold my ****in zombies
    Reply With Quote  
     

  8. #8  
    Registered Member
    Join Date
    Nov 2009
    Posts
    50
    Thanks given
    0
    Thanks received
    0
    Rep Power
    1
    GJ. I'm new to using XML files but I'm starting to understand them.
    Reply With Quote  
     

  9. #9  
    Officially Retired

    Huey's Avatar
    Join Date
    Jan 2008
    Age
    19
    Posts
    16,489
    Thanks given
    3,387
    Thanks received
    7,726
    Rep Power
    5000
    Nice

    Listen children don't become this guy.
    Quote Originally Posted by Owner Spikey View Post
    Why can I attack lower level npc's in a matter of a mouse hover but for a higher level npc the only choice to attack is by right clicking option attack?

    Reply With Quote  
     

  10. #10  
    Extreme Donator


    Join Date
    Mar 2009
    Age
    26
    Posts
    954
    Thanks given
    25
    Thanks received
    15
    Rep Power
    213
    Nice, I see how this can be used as an "account recovery."
    Quote Originally Posted by Note
    Why do you quote yourself?
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •