stoyanoff

json

Jul 4th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package com.company;
  2.  
  3. import com.google.gson.JsonObject;
  4. import com.google.gson.JsonParser;
  5.  
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.Reader;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12.  
  13. public class json {
  14.  
  15.     private double balance;
  16.     private double newBalance;
  17.  
  18.     public json(double newBalance) throws IOException {
  19.  
  20.         this.newBalance = new Account().setBalance();
  21.     }
  22.  
  23.  
  24.     public json() {
  25.         this.balance = balance;
  26.     }
  27.  
  28.  
  29.     public double getBalanceJson() {
  30.         Reader reader = null;
  31.         try {
  32.  
  33.             reader = new FileReader("./src/account.json");
  34.  
  35.             JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
  36.  
  37.             balance = jsonObject.get("balance").getAsDouble();
  38.  
  39.             reader.close();
  40.         } catch (IOException e) {
  41.             e.printStackTrace();
  42.         }
  43.         return balance;
  44.     }
  45.  
  46.     public void setBalanceJson() throws IOException {
  47.         Reader reader = null;
  48.         try {
  49.  
  50.             reader = new FileReader("./src/account.json");
  51.  
  52.             JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
  53.             jsonObject.addProperty("balance", newBalance);
  54.  
  55.             FileWriter fileWriter = new FileWriter("./src/account.json");
  56.             fileWriter.write(jsonObject.toString());
  57.             fileWriter.flush();
  58.  
  59.  
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.     }
  64. }
Add Comment
Please, Sign In to add comment