Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jonathan Sudibya
IF3230-Tugas-Besar-Sister-2015
Commits
0a8641d6
Commit
0a8641d6
authored
Apr 20, 2015
by
Kevin
Browse files
buat class sndri
parent
80d83063
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/server/TCPServer/src/TCPServer.java
View file @
0a8641d6
...
...
@@ -13,90 +13,7 @@ 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
;
}
class
TCPServer
{
public
static
void
main
(
String
argv
[])
throws
Exception
{
String
clientSentence
;
String
capitalizedSentence
;
...
...
src/server/TCPServer/src/crudDB/crudDB.java
0 → 100644
View file @
0a8641d6
/*
* 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
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment