Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1.  
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.Scanner;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /**
  19.  *
  20.  * @author lmoentje
  21.  */
  22. public class Facebook {
  23.    
  24.    
  25.     private HashMap<String, Account> accounts;
  26.    
  27.     public Facebook(){
  28.         this.accounts= new HashMap<>();
  29.      }
  30.  
  31.     public HashMap<String, Account> getAccounts() {
  32.         return accounts;
  33.     }
  34.    
  35.     public Facebook(String accountsFileName)  throws FileNotFoundException, FacebookException//vergeet de throws FBE niet, anders E niet gedeclareerd
  36.     {
  37.         accounts= new HashMap<>();
  38.          
  39.          Scanner inputstream = null;
  40.          File bestand = new File(accountsFileName);
  41.            try
  42.            {
  43.                inputstream = new Scanner(bestand);  
  44.            }
  45.            catch(FileNotFoundException e)
  46.             {
  47.                 throw new FileNotFoundException(accountsFileName);
  48.             }
  49.            
  50.          
  51.            
  52.            while(inputstream.hasNextLine())
  53.            {
  54.                String line = inputstream.nextLine();
  55.                String[] ary = line.split(";");
  56.                if(ary.length != 6)
  57.                    throw new FacebookException(bestand);
  58.                
  59.                     String firstName = ary[0];
  60.                     String lastName = ary[1];
  61.                     String gender = ary[2];
  62.                     String mail = ary[3];
  63.                     String login = ary[4];
  64.                     String password = ary[5];
  65.                     if(accounts.containsKey(login))
  66.                         throw new FacebookException(bestand);
  67.                     addAccount(firstName, lastName, gender, mail, login, password);
  68.                
  69.            }
  70.        
  71.        
  72.     }
  73.    
  74.     public void addAccount(String first, String last, String gender, String email, String login, String password) throws FacebookException{
  75.         Account a = new Account(first, last, gender, email, login, password);
  76.        
  77.         if(accounts.containsKey(a.getLogin()))
  78.             throw new FacebookException(a);
  79.        
  80.         accounts.put(login, a);
  81.     }
  82.    
  83.     public void addFriendship(Account ac1, String login2) throws FacebookException{
  84.         if(!accounts.containsKey(login2)){
  85.             throw new FacebookException(login2);
  86.         }
  87.        
  88.         if(!accounts.containsValue(ac1))
  89.             throw new FacebookException(ac1.getLogin());
  90.        
  91.         ac1.getFriends().add(accounts.get(login2));
  92.         accounts.get(login2).getFriends().add(ac1);
  93.            
  94.        
  95.     }
  96.    
  97.     public boolean removeFriendship(String login1, String login2) throws FacebookException{
  98.         boolean rem = false;
  99.         if(!accounts.containsKey(login2)){
  100.             throw new FacebookException(login2);
  101.         }
  102.         if(!accounts.containsKey(login1)){
  103.             throw new FacebookException(login1);
  104.         }
  105.        
  106.         if(accounts.get(login1).getFriends().contains(accounts.get(login2)) && accounts.get(login2).getFriends().contains(accounts.get(login1))){
  107.         accounts.get(login1).getFriends().remove(accounts.get(login2));
  108.         accounts.get(login2).getFriends().remove(accounts.get(login1));
  109.         rem = true;
  110.         }
  111.         return rem;
  112.     }
  113.    
  114.    
  115.    
  116.     public void addPost(String loginOfWallToPost, String postText, Account postCreator)throws FacebookException{
  117.         Post post = new Post(postCreator, postText);
  118.        
  119.         if(!accounts.containsKey(loginOfWallToPost))
  120.             throw new FacebookException(loginOfWallToPost);
  121.        
  122.         if(!accounts.containsValue(postCreator))
  123.             throw new FacebookException(postCreator.getLogin());
  124.        
  125.         if((accounts.get(loginOfWallToPost).getFriends().contains(postCreator))||(postCreator.getLogin().equals(loginOfWallToPost))){
  126.                 accounts.get(loginOfWallToPost).getWall().add(post);
  127.         }      
  128.         else
  129.         throw new FacebookException(post);
  130.    
  131.    
  132.     }
  133.    
  134.     //ALS PARAMETER Integer GEBRUIKEN EN NIET int
  135.     //BIJ PO.GETPOSTID EQUALS GEBRUIKEN EN NIET ==
  136.     public Post search(Integer postID){
  137.         Post pos = null;
  138.         for(Account a : accounts.values()){
  139.             for(Post po : a.getWall()){
  140.                 if(po.getPostID().equals(postID))
  141.                     pos = po;
  142.             }
  143.         }
  144.        
  145.         return pos;
  146.     }
  147.    
  148.     // MAAK GEBRUIK VAN MEHTODES UIT DEZELFDE KLASSE (SEARCH)
  149.     // BIJ LIKES WORDT DE POSTID OPGESLAGEN!
  150.     public void like(String login, Post post) throws FacebookException{
  151.        
  152.         if(!accounts.containsKey(login))
  153.             throw new FacebookException(login);
  154.         if(search(post.getPostID())==null)
  155.             throw new FacebookException(post.getPostID());
  156.        
  157.         accounts.get(login).getLikes().add(post.getPostID());
  158.        
  159.     }
  160.    
  161.     public boolean removeLike(String login, Integer postID) throws FacebookException{
  162.        
  163.         boolean rem = false;
  164.         if(!accounts.containsKey(login)){
  165.             throw new FacebookException(login);
  166.            
  167.         }
  168.        
  169.         if(search(postID)==null){
  170.             throw new FacebookException(postID);
  171.            
  172.         }
  173.         if(accounts.get(login).getLikes().contains(postID)){
  174.             accounts.get(login).getLikes().remove(postID);
  175.             rem = true;
  176.         }
  177.        
  178.         return rem;
  179.     }
  180.    
  181.     public ArrayList<Post> getNewsFeed(Account account)throws FacebookException{
  182.         ArrayList<Post> lijst = new ArrayList<>();
  183.         if(!accounts.containsKey(account.getLogin()))
  184.             throw new FacebookException(account.getLogin());
  185.        
  186.         for(Post post : account.getWall()){
  187.             lijst.add(post);
  188.         }
  189.        
  190.         for(Account a : account.getFriends()){
  191.             for(Post post : a.getWall()){
  192.                 lijst.add(post);
  193.             }
  194.         }
  195.        
  196.         Collections.sort(lijst);
  197.        
  198.         return lijst;
  199.     }
  200.    
  201.     // OPGAVE: De methode GOOIT een IOException indien het bestand niet uitgeschreven kan worden.
  202.     //GOOIT EEN EXCEPTION DUS IN THROWS?
  203.     public void exportAccountsDataFile(String filename)throws IOException{
  204.        
  205.        
  206.             ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename));
  207.             output.writeObject(accounts);
  208.        
  209.     }
  210.    
  211.     public boolean importAccountsDataFile(String filename){
  212.         boolean imp = false;
  213.         try {
  214.             ObjectInputStream input = new ObjectInputStream(new FileInputStream(filename));
  215.             HashMap<String, Account> accounts2 = (HashMap<String, Account>) input.readObject();
  216.             accounts.putAll(accounts2);
  217.             imp = true;
  218.         }
  219.         catch (FileNotFoundException ex) {
  220.             System.out.println("Het bestand "+filename+" bestaat niet!");
  221.         }
  222.         catch (IOException ex) {
  223.             System.out.println("Het bestand "+filename+" kan niet ingelezen worden!");
  224.         } catch (ClassNotFoundException ex) {
  225.             System.out.println("Het bestand "+filename+" kan niet ingelezen worden!");
  226.         }
  227.        
  228.        
  229.         return imp;
  230.     }
  231.    
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement