Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kurzdev.vertretungsplanapp.basics;
- import com.kurzdev.vertretungsplanapp.basics.UserHandler;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Config {
- public Config(){
- }
- public void loadData(String username, String password){
- this.getData(username, password);
- }
- private String loadJSON(){
- try {
- File json_file = new File(System.getProperty("user.dir") + "/src/Config.json");
- String json_raw = new String(Files.readAllBytes(Paths.get(json_file.toURI())), "UTF-8");
- return json_raw;
- } catch(IOException e){
- System.out.println("An IOException occured: " + e.getMessage());
- return null;
- } catch(Exception e){
- System.out.println("An unknown exception occured: " + e.getMessage());
- return null;
- }
- }
- /*
- Method to write data to an desired JSON file
- @param username: Argument used to log into Moodle
- @param password: Argument to authenticate on Moodle
- @param class_data: Argument regarding data about a certain class/grade
- @param course_data: Argument/List consisting of one's courses - to-be-used in MyFilter
- */
- @SuppressWarnings("unchecked")
- private void writeJSON(String username, String password, String class_data, ArrayList<String> course_data){
- JSONObject obj = new JSONObject();
- if(username.equals("")){
- obj.put("username", "");
- }else if(password.equals("")){
- obj.put("password", "");
- }else if(class_data.equals("")) {
- obj.put("class", "");
- }else if(course_data.isEmpty()){
- obj.put("courses", "");
- }else{
- obj.put("username", username);
- obj.put("password", password);
- obj.put("class", class_data);
- JSONArray course_list = new JSONArray();
- for(String elements : course_data){
- course_list.put(elements);
- }
- obj.put("courses", course_list);
- try(FileWriter file = new FileWriter(System.getProperty("user.dir") + "/src/Config.json")){
- file.write(obj.toString());
- file.flush();
- }catch(IOException e){
- System.out.println("An IOException occured: " + e.getMessage());
- }catch(Exception e){
- System.out.println("An unknown exception occured: " + e.getMessage());
- }
- }
- }
- /*
- Method used to get desired data for JSON file
- @param username: Argument used to log into Moodle
- @param password: Argument to authenticate on Moodle
- */
- private void getData(String username, String password){
- ArrayList<String> course_list = new ArrayList<>();
- course_list.add("Wer das liest ist dumm.");
- this.writeJSON(username, password, "3", course_list);
- }
- private Boolean checkJSON(){
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement