SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Rank {
  5.    
  6.     //Instances
  7.    
  8.     private ArrayList <String> gameTags = new ArrayList<String>();
  9.     private ArrayList <Integer> Scores = new ArrayList<Integer>();
  10.    
  11.     //Constructor
  12.    
  13.     public Rank(ArrayList<String>gameTags,ArrayList<Integer>Scores) {
  14.        
  15.         this.gameTags = gameTags;
  16.         this.Scores = Scores;
  17.     }
  18.    
  19.    
  20.    
  21.     //Writing file (doesn't need to be called every time because after each methods it is called)
  22.     /*
  23.      *
  24.      */
  25.     public void FileWriter() throws FileNotFoundException{
  26.        
  27.         File file = new File("Score.txt");
  28.         PrintWriter out = new PrintWriter(file);
  29.        
  30.         if(gameTags.size() != Scores.size()) {
  31.             System.out.println("ERROR");
  32.         }
  33.        
  34.         for(int i = 0; i < this.gameTags.size(); i++) {
  35.             out.print(this.gameTags.get(i) + "<" + this.Scores.get(i) + ">" + System.lineSeparator());
  36.         }
  37.         out.close();
  38.        
  39.     }
  40.    
  41.     //Add new gameTag if is not present in the file other wise prints an error
  42.    
  43.     public void addUser(String newTag) throws FileNotFoundException {
  44.        
  45.             if(!(this.gameTags.contains(newTag))) {
  46.                 this.gameTags.add(newTag);
  47.                 this.Scores.add(0);
  48.                
  49.             }
  50.             else {
  51.                 System.out.println("Error");      
  52.             }
  53.            
  54.             FileWriter();
  55.            
  56.     }
  57.    
  58.     //Add Scores to players if it is higher that the one that they score before
  59.    
  60.     public void addScore(String gameTag,int newScore) throws FileNotFoundException {
  61.        
  62.         if(this.gameTags.contains(gameTag)) {
  63.             int x = this.gameTags.indexOf(gameTag);
  64.             if(this.Scores.get(x) < newScore) {
  65.             this.Scores.set(x, newScore);
  66.             }
  67.         }
  68.         else {
  69.             System.out.println("The GameTag is not present");
  70.            
  71.         }  
  72.        
  73.         FileWriter();
  74.        
  75.     }
  76.    
  77.     //Get Score from the GameTag returns the score of certain gameTag if the tag exist
  78.    
  79.     public int getScore(String gameTag) {
  80.        
  81.         int out = 0;
  82.        
  83.         if(this.gameTags.contains(gameTag)) {
  84.             int x = this.gameTags.indexOf(gameTag);
  85.             out = this.Scores.get(x);
  86.         }
  87.         else {
  88.             System.out.println("The GameTag is not present");
  89.         }
  90.         return out;
  91.        
  92.     }
  93.    
  94.     public void getHighScore() {
  95.          System.out.println(Collections.max(Scores));
  96.        
  97.     }
  98.    
  99. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top