Advertisement
Guest User

assignmentPRA

a guest
Oct 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. // ------------http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.math.BigInteger;
  6. import java.security.MessageDigest;
  7. import java.util.Arrays;
  8. import java.util.Scanner;
  9.  
  10. import javax.xml.parsers.DocumentBuilder;
  11. import javax.xml.parsers.DocumentBuilderFactory;
  12.  
  13. import org.w3c.dom.Document;
  14. import org.w3c.dom.Element;
  15. import org.w3c.dom.Node;
  16. import org.w3c.dom.NodeList;
  17.  
  18. static String [] sales_ID = new String [doc2_List.getLength()];
  19.  
  20. public class Assignment_1 {
  21.  
  22. public static void main(String[] args) throws Exception, IOException{
  23. // TODO Auto-generated method stub
  24.  
  25. Scanner input = new Scanner (System.in);
  26.  
  27. }
  28.  
  29. // ==================================================ADMIN=============================================================
  30. public static void loadFileAdmin(){
  31. Scanner input=new Scanner(System.in);
  32.  
  33.  
  34.  
  35. System.out.print("Enter Path for admin : ");
  36. String adminPath = input.nextLine();
  37.  
  38. Document doc1 = null;
  39. DocumentBuilder dBuilder1 = null;
  40.  
  41. File admin_File = new File(adminPath);
  42.  
  43. try {
  44. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  45. dBuilder1 = dbFactory.newDocumentBuilder();
  46. doc1 = dBuilder1.parse(admin_File);
  47.  
  48.  
  49. doc1.getDocumentElement().normalize();
  50.  
  51. System.out.println("Root element :" + doc1.getDocumentElement().getNodeName());
  52.  
  53. NodeList doc1_List = doc1.getElementsByTagName("admin");
  54.  
  55. System.out.println("----------------------------");
  56.  
  57. String [] admin_ID = new String [doc1_List.getLength()];
  58. String [] admin_Name = new String [doc1_List.getLength()];
  59. String [] admin_Password = new String [doc1_List.getLength()];
  60.  
  61.  
  62. for (int k = 0; k < doc1_List.getLength(); k++) { //-------nList tukar doc1_List
  63.  
  64. Node nNode = doc1_List.item(k);
  65.  
  66. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  67.  
  68. Element eElement = (Element) nNode;
  69.  
  70. admin_ID [k] = eElement.getElementsByTagName("id").item(0).getTextContent();
  71. admin_Name [k] = eElement.getElementsByTagName("name").item(0).getTextContent();
  72. admin_Password [k] = eElement.getElementsByTagName("password").item(0).getTextContent();
  73.  
  74. } // ==============Close if
  75. }
  76. }// ================Close for
  77. catch (Exception e) {
  78. // TODO: handle exception
  79. System.out.println("File not found. Please Try Again.");
  80. System.out.println();
  81. loadFileAdmin();
  82. }
  83. }
  84.  
  85.  
  86.  
  87. /*
  88.  
  89. }
  90. System.out.println("Enter Path for admin : ");
  91. String adminPath = input.nextLine();
  92.  
  93. Document doc1 = null;
  94. DocumentBuilder dBuilder1 = null;
  95.  
  96. File admin_File = new File(adminPath);
  97.  
  98. try {
  99.  
  100. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  101. dBuilder1 = dbFactory.newDocumentBuilder();
  102. doc1 = dBuilder1.parse(admin_File);
  103. }
  104.  
  105. catch (Exception e) {}
  106.  
  107. //optional, but recommended
  108. //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
  109.  
  110. doc1.getDocumentElement().normalize();
  111.  
  112. System.out.println("Root element :" + doc1.getDocumentElement().getNodeName());
  113.  
  114. NodeList doc1_List = doc1.getElementsByTagName("admin");
  115.  
  116. System.out.println("----------------------------");
  117.  
  118. String [] admin_ID = new String [doc1_List.getLength()];
  119. String [] admin_Name = new String [doc1_List.getLength()];
  120. String [] admin_Password = new String [doc1_List.getLength()];
  121.  
  122.  
  123. for (int temp = 0; temp < doc1_List.getLength(); temp++) { //-------nList tukar doc1_List
  124.  
  125. Node nNode = doc1_List.item(temp);
  126.  
  127. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  128.  
  129. Element eElement = (Element) nNode;
  130.  
  131. admin_ID [temp] = eElement.getElementsByTagName("id").item(0).getTextContent();
  132. admin_Name [temp] = eElement.getElementsByTagName("name").item(0).getTextContent();
  133. admin_Password [temp] = eElement.getElementsByTagName("password").item(0).getTextContent();
  134.  
  135. } // ==============Close if
  136. }// ================Close for
  137.  
  138. */
  139.  
  140.  
  141. // ==================================================SALES=============================================================
  142. public static void loadFileSales(){
  143. Scanner input=new Scanner(System.in);
  144.  
  145.  
  146.  
  147. System.out.print("Enter Path for Sales : ");
  148. String salesPath = input.nextLine();
  149.  
  150. Document doc2 = null;
  151. DocumentBuilder dBuilder2 = null;
  152.  
  153. File sales_File = new File(salesPath);
  154.  
  155. try {
  156. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  157. dBuilder2 = dbFactory.newDocumentBuilder();
  158. doc2 = dBuilder2.parse(sales_File);
  159.  
  160.  
  161. doc2.getDocumentElement().normalize();
  162.  
  163. System.out.println("Root element :" + doc2.getDocumentElement().getNodeName());
  164.  
  165. NodeList doc2_List = doc2.getElementsByTagName("salesperson");
  166.  
  167. System.out.println("----------------------------");
  168.  
  169. String [] sales_ID = new String [doc2_List.getLength()];
  170. String [] sales_Name = new String [doc2_List.getLength()];
  171. String [] sales_Password = new String [doc2_List.getLength()];
  172.  
  173.  
  174. for (int k = 0; k < doc2_List.getLength(); k++) { //-------nList tukar doc1_List
  175.  
  176. Node nNode = doc2_List.item(k);
  177.  
  178. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  179.  
  180. Element eElement = (Element) nNode;
  181.  
  182. sales_ID [k] = eElement.getElementsByTagName("id").item(0).getTextContent();
  183. sales_Name [k] = eElement.getElementsByTagName("name").item(0).getTextContent();
  184. sales_Password [k] = eElement.getElementsByTagName("password").item(0).getTextContent();
  185.  
  186. } // ==============Close if
  187. }
  188. }// ================Close for
  189. catch (Exception e) {
  190. // TODO: handle exception
  191. System.out.println("File not found. Please Try Again.");
  192. System.out.println();
  193. loadFileSales();
  194. }
  195. }
  196.  
  197.  
  198.  
  199. // ==================================================SALES=============================================================
  200. /*
  201. System.out.println("Enter Path for Salesperson : ");
  202. String salesPath = input.nextLine();
  203.  
  204. Document doc2 = null;
  205. DocumentBuilder dBuilder2 = null;
  206.  
  207. File sales_File = new File(salesPath);
  208.  
  209. try {
  210.  
  211. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  212. dBuilder2 = dbFactory.newDocumentBuilder();
  213. doc1 = dBuilder2.parse(admin_File);
  214. }
  215.  
  216. catch (Exception e) {}
  217.  
  218. //optional, but recommended
  219. //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
  220.  
  221. doc1.getDocumentElement().normalize();
  222.  
  223. System.out.println("Root element :" + doc1.getDocumentElement().getNodeName());
  224.  
  225. NodeList doc2_List = doc1.getElementsByTagName("salesperson");
  226.  
  227. System.out.println("----------------------------");
  228.  
  229. String [] salesperson_ID = new String [doc2_List.getLength()];
  230. String [] salesperson_Name = new String [doc2_List.getLength()];
  231. String [] salesperson_Password = new String [doc2_List.getLength()];
  232.  
  233.  
  234. for (int temp = 0; temp < doc2_List.getLength(); temp++) { //-------nList tukar doc1_List
  235.  
  236. Node nNode = doc2_List.item(temp);
  237.  
  238. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  239.  
  240. Element eElement = (Element) nNode;
  241.  
  242. salesperson_ID [temp] = eElement.getElementsByTagName("id").item(0).getTextContent();
  243. salesperson_Name [temp] = eElement.getElementsByTagName("name").item(0).getTextContent();
  244. salesperson_Password [temp] = eElement.getElementsByTagName("password").item(0).getTextContent();
  245.  
  246. } // ==============Close if
  247. }// ================Close for
  248. */
  249.  
  250. // ==================================================LOGIN=============================================================
  251.  
  252. public static void Login(){
  253. Scanner input=new Scanner(System.in);
  254.  
  255. while (true){
  256.  
  257. for (int a = 0; a<3; a++) {
  258.  
  259. System.out.print("Enter ID: ");
  260. String IDSales = input.nextLine();
  261.  
  262. System.out.print("Password: ");
  263. String passwordSales = input.nextLine();
  264.  
  265. MessageDigest m = MessageDigest.getInstance("MD5");
  266. m.reset();
  267. m.update(passwordSales.getBytes());
  268.  
  269. byte[] digest = m.digest();
  270. BigInteger bigInt = new BigInteger(1,digest);
  271. String hashtext = bigInt.toString(16);
  272. // Now we need to zero pad it if you actually want the full 32 chars.
  273. while(hashtext.length() < 32 ){
  274. hashtext = "0"+hashtext;
  275. }
  276.  
  277.  
  278. //boolean IDBoolean = Arrays.asList (sales_ID).contains(IDSales);
  279. //boolean PasswordBoolean = Arrays.asList(sales_Password).contains(passwordSales);
  280.  
  281. if (Arrays.asList (sales_ID).contains(IDSales) == true && Arrays.asList(sales_Password).contains(passwordSales) == true){ // untuk sales
  282. System.out.println("Access Granted");
  283. break;
  284. }
  285. else if{ /////untuk admin
  286. System.out.println("");
  287. }else
  288. }
  289.  
  290.  
  291.  
  292. for (int a = 0; a<3; a++) {
  293. System.out.print("Username: ");
  294. String username = input.nextLine();
  295. System.out.print("Password: ");
  296. String password = input.nextLine();
  297.  
  298.  
  299. MessageDigest m = MessageDigest.getInstance("MD5");
  300. m.reset();
  301. m.update(password.getBytes());
  302.  
  303. byte[] digest = m.digest();
  304. BigInteger bigInt = new BigInteger(1,digest);
  305. String hashtext = bigInt.toString(16);
  306. // Now we need to zero pad it if you actually want the full 32 chars.
  307. while(hashtext.length() < 32 ){
  308. hashtext = "0"+hashtext;
  309. }
  310. }
  311.  
  312.  
  313. } // ----------close try
  314. catch (Exception e) {
  315. e.printStackTrace();
  316. } // ----------close catch
  317. }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement