Commit 80d83063 authored by Kevin's avatar Kevin
Browse files

pindah ke kelas yg bener

parent 385cb87e
......@@ -17,90 +17,6 @@ import org.json.simple.JSONObject;
import static org.json.simple.JSONValue.parse;
class TCPClient {
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 sentence;
String modifiedSentence;
......@@ -113,18 +29,5 @@ class TCPClient {
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
// TCPClient client = new TCPClient();
// if (client.signUp("Kevin", "123")) {
// System.out.println("signUp berhasil");
// } else {
// System.out.println("signUp gagal");
// }
// if (client.login("Jonathan Sudibya", "Sudib")) {
// System.out.println("Login berhasil");
// } else {
// System.out.println("Login gagal");
// }
// client.editAtt("JONATHAN SUDIBYA", "seksi", "kankei");
}
}
......@@ -11,9 +11,92 @@
import java.io.*;
import java.net.*;
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;
......
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