Advertisement
LaCaraDeLaVerga

TestFinadeRanking jere Feat Jorge

Sep 15th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  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. public class RankingJere {
  10.     // Le pasamos el archivo como parametro para que pueda escribir en el y el
  11.     // puntaje de la partida actual;
  12.     public static void setPuntaje(File scoreFile, String highScore) {
  13.         FileWriter writeFile = null;
  14.         BufferedWriter writer = null;
  15.         try {
  16.             writeFile = new FileWriter(scoreFile);
  17.             writer = new BufferedWriter(writeFile);
  18.  
  19.             // writer.write(this.highScore);
  20.  
  21.             writer.write(highScore);
  22.             // writer.write(puntaje);
  23.         } catch (Exception e) {
  24.             // errores
  25.         } finally {
  26.             try {
  27.                 if (writer != null) {
  28.                     writer.close();
  29.                 }
  30.             } catch (Exception e) {
  31.                 // errosr
  32.             }
  33.         }
  34.     }
  35.  
  36.     //
  37.     // EL ENCARGADO DEL LEER EL ARCHIVO CREADO POR EL JUEGO
  38.     public static String getPuntaje() throws FileNotFoundException {
  39.         String ret = "";
  40.         FileReader readFile = null;
  41.         BufferedReader reader = null;
  42.         // formato = Jorge: 666
  43.         try {
  44.             readFile = new FileReader("highscore.txt");
  45.             reader = new BufferedReader(readFile);
  46.             return reader.readLine();
  47.         }
  48.  
  49.         catch (Exception e) {
  50.             ret = " nadie :  0";
  51.         } finally {
  52.  
  53.             try {
  54.                 if (reader != null) {
  55.                     reader.close();
  56.                 }
  57.             } catch (Exception e2) {
  58.                 e2.printStackTrace();
  59.             }
  60.         }
  61.         return ret;
  62.     }
  63.     //
  64.  
  65.     // EL TEST CONSISTE EN CAMBIAR LAS VARIABLES Y VER QUE EL ARCHIVO QUE SE
  66.     // GENERA SE VA SOBRE ESCRIBIENDO
  67.     // PISANDO ASI EL PUNTAJE ANTERIOR
  68.  
  69.     // FALTA COMPARAR EL puntajeMain CON EL VALOR ENTERO DEL ARCHIVO
  70.     // ("highScore.txt")
  71.     // SI putajeMain > VALOR_ENTERO_DENTRO_DEL_STRING
  72.  
  73.     public static void main(String[] args) throws FileNotFoundException {
  74.         // variables
  75.         String nombreJugador = "jorge";
  76.         int puntaje = 1220;
  77.         String highScoreMain = "";
  78.         highScoreMain = nombreJugador + ":" + puntaje;
  79.  
  80.         // crea archivo
  81.         File scoreFile = new File("highscore.txt");
  82.         if (!scoreFile.exists()) {
  83.             try {
  84.                 scoreFile.createNewFile();
  85.             } catch (IOException e) {
  86.                 // TODO Auto-generated catch block
  87.                 e.printStackTrace();
  88.             }
  89.         }
  90.         // fin de la creacion del archivo
  91.         // variable que supone ya saque el valor dentro del String del archivo
  92.  
  93.         String puntajeMasAltoEver = getPuntaje();
  94.         String puntajeS = "";
  95.         boolean asd = false;
  96.         for (int i = 0; i < puntajeMasAltoEver.length(); i++) {
  97.             if(asd == true){
  98.                 puntajeS+= ""+puntajeMasAltoEver.charAt(i);
  99.             }
  100.             if(puntajeMasAltoEver.charAt(i) == ':'){
  101.                 asd = true;
  102.             }
  103.            
  104.         }
  105.         Integer valor = new Integer(puntajeS);
  106.         System.out.println(puntajeS);
  107.  
  108.         if (puntaje > valor) {
  109.             setPuntaje(scoreFile, highScoreMain);
  110.             System.out.println();
  111.  
  112.             // Integer parseado = new Integer (highScoreMain":"[1]);
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement