SHARE
TWEET

Untitled

a guest Apr 24th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8.  
  9. import javax.swing.JOptionPane;
  10.  
  11. public class LeaderBoard {
  12.     static String directory = System.getProperty("user.dir");  
  13.     static String fileName = "Score.txt";  
  14.     static String absolutePath = directory + File.separator + fileName;
  15.    
  16.     static Player[] pl =new Player[3];
  17.    
  18.     private void read() {
  19.        
  20.         try(BufferedReader bufferedReader = new BufferedReader(new FileReader(absolutePath))) {  
  21.             String line = bufferedReader.readLine();
  22.             String line1 = bufferedReader.readLine();
  23.            
  24.             for(int i=0;i<3;i++) {
  25.                 pl[i]= new Player(line,line1);
  26.                 line = bufferedReader.readLine();
  27.                 line1 = bufferedReader.readLine();
  28.                 }
  29.            
  30.            
  31.         } catch (FileNotFoundException e) {
  32.             // exception handling
  33.         } catch (IOException e) {
  34.             // exception handling
  35.         }  
  36.    
  37.     }
  38.    
  39.    
  40.     private void write(Player p1, Player p2, Player p3) {
  41.        
  42.         try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(absolutePath))) {
  43.            
  44.                 bufferedWriter.write(p1.getName());
  45.                 bufferedWriter.write(p1.getPoints());
  46.                 bufferedWriter.write(p2.getName());
  47.                 bufferedWriter.write(p2.getPoints());
  48.                 bufferedWriter.write(p3.getName());
  49.                 bufferedWriter.write(p3.getPoints());
  50.            
  51.            
  52.         } catch (IOException e) {
  53.             // exception handling
  54.         }
  55.        
  56.        
  57.     }
  58.    
  59.     public void check(String name, int points) {
  60.        
  61.         read();
  62.         Player newPlayer = new Player(name,String.valueOf(points));
  63.        
  64.         if(points > pl[0].getPointsInt()) {
  65.             write(newPlayer,pl[0],pl[1]);
  66.             JOptionPane.showMessageDialog(null, "Congratz, New Record!!!\nYou placed 1st with " + points + " points");
  67.         }
  68.         else
  69.             if(points > pl[1].getPointsInt()) {
  70.                 write(pl[0],newPlayer,pl[1]);
  71.                 JOptionPane.showMessageDialog(null, "Congratz, New Record!!!\nYou placed 2nd with " + points + " points");
  72.             }
  73.             else
  74.                 if(points > pl[2].getPointsInt()) {
  75.                 write(pl[0],pl[1],newPlayer);
  76.                 JOptionPane.showMessageDialog(null, "Congratz, New Record!!!\nYou placed 3rd with " + points + " points");
  77.             }
  78.    
  79.             read();
  80.            
  81.                
  82.     }
  83.                
  84.        
  85.        
  86.        
  87.        
  88.        
  89.    
  90.    
  91.    
  92.     public void showLeaderboard() {
  93.        
  94.         read();
  95.         JOptionPane.showMessageDialog(null,"1st Place: "  + pl[0].getName2()  + " ----- " + pl[0].getPoints2() + " points\n\n"+
  96.                                                  "2nd Place: "  + pl[1].getName2()  + " ----- " + pl[1].getPoints2() + " points\n\n"+
  97.                                                  "3rd Place: "  + pl[2].getName2()  + " ----- " + pl[2].getPoints2() + " points\n\n" , "Leaderboards",JOptionPane.PLAIN_MESSAGE);
  98.        
  99.        
  100.     }
  101.    
  102.    
  103.    
  104.    
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top