Advertisement
Guest User

Config.java

a guest
Apr 7th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package com.kurzdev.vertretungsplanapp.basics;
  2.  
  3. import com.kurzdev.vertretungsplanapp.basics.UserHandler;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import java.io.*;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. public class Config {
  14.  
  15.     public Config(){
  16.     }
  17.  
  18.     public void loadData(String username, String password){
  19.  
  20.         this.getData(username, password);
  21.     }
  22.  
  23.     private String loadJSON(){
  24.  
  25.         try {
  26.             File json_file = new File(System.getProperty("user.dir") + "/src/Config.json");
  27.             String json_raw = new String(Files.readAllBytes(Paths.get(json_file.toURI())), "UTF-8");
  28.  
  29.             return json_raw;
  30.  
  31.         } catch(IOException e){
  32.             System.out.println("An IOException occured: " + e.getMessage());
  33.             return null;
  34.         } catch(Exception e){
  35.             System.out.println("An unknown exception occured: " + e.getMessage());
  36.             return null;
  37.         }
  38.     }
  39.  
  40.     /*
  41.     Method to write data to an desired JSON file
  42.     @param username: Argument used to log into Moodle
  43.     @param password: Argument to authenticate on Moodle
  44.     @param class_data: Argument regarding data about a certain class/grade
  45.     @param course_data: Argument/List consisting of one's courses - to-be-used in MyFilter
  46.      */
  47.     @SuppressWarnings("unchecked")
  48.     private void writeJSON(String username, String password, String class_data, ArrayList<String> course_data){
  49.  
  50.         JSONObject obj = new JSONObject();
  51.  
  52.         if(username.equals("")){
  53.             obj.put("username", "");
  54.         }else if(password.equals("")){
  55.             obj.put("password", "");
  56.         }else if(class_data.equals("")) {
  57.             obj.put("class", "");
  58.         }else if(course_data.isEmpty()){
  59.             obj.put("courses", "");
  60.         }else{
  61.             obj.put("username", username);
  62.             obj.put("password", password);
  63.             obj.put("class", class_data);
  64.  
  65.             JSONArray course_list = new JSONArray();
  66.             for(String elements : course_data){
  67.                 course_list.put(elements);
  68.             }
  69.             obj.put("courses", course_list);
  70.  
  71.             try(FileWriter file = new FileWriter(System.getProperty("user.dir") + "/src/Config.json")){
  72.                 file.write(obj.toString());
  73.                 file.flush();
  74.             }catch(IOException e){
  75.                 System.out.println("An IOException occured: " + e.getMessage());
  76.             }catch(Exception e){
  77.                 System.out.println("An unknown exception occured: " + e.getMessage());
  78.             }
  79.  
  80.         }
  81.     }
  82.  
  83.     /*
  84.      Method used to get desired data for JSON file
  85.      @param username: Argument used to log into Moodle
  86.      @param password: Argument to authenticate on Moodle
  87.      */
  88.     private void getData(String username, String password){
  89.  
  90.         ArrayList<String> course_list = new ArrayList<>();
  91.         course_list.add("Wer das liest ist dumm.");
  92.  
  93.         this.writeJSON(username, password, "3", course_list);
  94.     }
  95.  
  96.     private Boolean checkJSON(){
  97.         return null;
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement