Advertisement
Guest User

saveGame.java

a guest
Dec 5th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class SaveGame {
  5.    
  6.     private static Scanner x;
  7.  
  8.     public static void main(String[] args) {
  9.      File newfile = new File("temp.txt");
  10.      
  11.      if(!newfile.exists()) {
  12.          try {
  13.             newfile.createNewFile();
  14.         }
  15.          catch (IOException e) {
  16.             // TODO Auto-generated catch block
  17.             e.printStackTrace();
  18.         }
  19.      }     
  20.         String user = "June";//rewrite as String user = String.toString(Game.getUser())
  21.         String compTime1 = "7";//rewrite as String compTime1 = String.toString(Game.getCompTime1())
  22.         String compTime2 = "0";
  23.         String compTime3 = "0";
  24.         String compLevel = "2";
  25.         String tokenCollected = "3";
  26.         String flipperCollected = "2";
  27.         String firebootsCollected = "4";
  28.         String wingbootsCollected = "3";
  29.         String keysCollected = "3";
  30.        
  31.         saveGame(user,compTime1,compTime2,compTime3,compLevel, tokenCollected, flipperCollected,firebootsCollected,wingbootsCollected,keysCollected);
  32.  
  33.     }
  34.  
  35.     public static void saveGame(String user, String compTime1, String compTime2, String compTime3, String compLevel, String tokenCollected, String flipperCollected, String firebootsCollected, String wingbootsCollected, String keysCollected) {
  36.        
  37.         try
  38.         {
  39.             String olduser = "";//rewrite as String user = String.toString(Game.user)
  40.             String oldcompTime1 = "";
  41.             String oldcompTime2 = "";
  42.             String oldcompTime3 = "";
  43.             String oldcompLevel = "";
  44.             String oldtokenCollected = "";
  45.             String oldflipperCollected = "";
  46.             String oldfirebootsCollected = "";
  47.             String oldwingbootsCollected = "";
  48.             String oldkeysCollected = "";
  49.             String tempFile = "temp.txt";
  50.             File oldFile = new File("Game.txt");
  51.             File newFile = new File("temp.txt");
  52.             FileWriter fw = new FileWriter(oldFile,true);
  53.             BufferedWriter bw = new BufferedWriter(fw);
  54.             PrintWriter pw = new PrintWriter(bw);
  55.             FileWriter fw1 = new FileWriter(newFile,true);
  56.             BufferedWriter bw1 = new BufferedWriter(fw1);
  57.             PrintWriter pw1 = new PrintWriter(bw1);
  58.             x = new Scanner(new File("Game.txt"));
  59.             x.useDelimiter("[,\n]");
  60.             boolean yes = true;
  61.            
  62.             while(x.hasNext())
  63.             {
  64.                  olduser = x.next();
  65.                  oldcompTime1 = x.next();
  66.                  oldcompTime2 = x.next();
  67.                  oldcompTime3 = x.next();
  68.                  oldcompLevel = x.next();
  69.                  oldtokenCollected = x.next();
  70.                  oldflipperCollected = x.next();
  71.                  oldfirebootsCollected = x.next();
  72.                  oldwingbootsCollected = x.next();
  73.                  oldkeysCollected = x.next();
  74.                  
  75.                  if(!olduser.contentEquals(user)){
  76.                      pw1.print("\n" +user + "," + compTime1 + "," + compTime2 + "," + compTime3 + "," + compLevel + "," + tokenCollected + "," + flipperCollected + "," + firebootsCollected + "," + wingbootsCollected + "," + keysCollected);
  77.                     yes = false;
  78.                  }
  79.             }
  80.             x.close();
  81.             pw1.flush();
  82.             pw1.close();
  83.             oldFile.delete();
  84.             File dump = new File("Game.txt");
  85.             newFile.renameTo(dump);
  86.            
  87.             if(yes) {
  88.             pw.print("\n" +user + "," + compTime1 + "," + compTime2 + "," + compTime3 + "," + compLevel + "," + tokenCollected + "," + flipperCollected + "," + firebootsCollected + "," + wingbootsCollected + "," + keysCollected);
  89.             pw.flush();
  90.             pw.close();
  91.             }
  92.            
  93.         }catch(Exception e) {
  94.             e.printStackTrace();
  95.             System.out.println("Record not saved");
  96.            
  97.            
  98.            
  99.         }
  100.        
  101.        
  102.        
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement