Commit 0a8641d6 authored by Kevin's avatar Kevin
Browse files

buat class sndri

parent 80d83063
......@@ -14,89 +14,6 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
class TCPServer {
public boolean signUp(String username, String password) throws IOException {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
return false;
} else {
JSONObject userObj = new JSONObject();
userObj.put("password", password);
userObj.put("position", "nanti_dipikirin");
userObj.put("r11", 0);
userObj.put("r12", 0);
userObj.put("r13", 0);
userObj.put("r14", 0);
userObj.put("r21", 0);
userObj.put("r22", 0);
userObj.put("r23", 0);
userObj.put("r31", 0);
userObj.put("r32", 0);
userObj.put("r41", 0);
jsonFile.put(username.toLowerCase(), userObj);
FileWriter file = new FileWriter(basePath + "//file_saved.json");
file.write(jsonFile.toJSONString());
file.flush();
file.close();
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public void editAtt(String username, String att, String newValue) {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase());
if (usrObj.containsKey(att.toLowerCase())) {
usrObj.put(att.toLowerCase(), newValue);
}
}
FileWriter file = new FileWriter(basePath + "//file_saved.json");
file.write(jsonFile.toJSONString());
file.flush();
file.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean login(String username, String password) {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase());
if (password.equals(usrObj.get("password").toString())) {
return true;
} else {
return false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package crudDB;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
/**
*
* @author Kevin Zhong Local
*/
public class crudDB {
public boolean signUp(String username, String password) throws IOException {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
return false;
} else {
JSONObject userObj = new JSONObject();
userObj.put("password", password);
userObj.put("position", "nanti_dipikirin");
userObj.put("r11", 0);
userObj.put("r12", 0);
userObj.put("r13", 0);
userObj.put("r14", 0);
userObj.put("r21", 0);
userObj.put("r22", 0);
userObj.put("r23", 0);
userObj.put("r31", 0);
userObj.put("r32", 0);
userObj.put("r41", 0);
jsonFile.put(username.toLowerCase(), userObj);
FileWriter file = new FileWriter(basePath + "//file_saved.json");
file.write(jsonFile.toJSONString());
file.flush();
file.close();
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public void editAtt(String username, String att, String newValue) {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase());
if (usrObj.containsKey(att.toLowerCase())) {
usrObj.put(att.toLowerCase(), newValue);
}
}
FileWriter file = new FileWriter(basePath + "//file_saved.json");
file.write(jsonFile.toJSONString());
file.flush();
file.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean login(String username, String password) {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase());
if (password.equals(usrObj.get("password").toString())) {
return true;
} else {
return false;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment