Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.util.Arrays;
- import java.util.Scanner;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- static String [] sales_ID = new String [doc2_List.getLength()];
- public class Assignment_1 {
- public static void main(String[] args) throws Exception, IOException{
- // TODO Auto-generated method stub
- Scanner input = new Scanner (System.in);
- }
- // ==================================================ADMIN=============================================================
- public static void loadFileAdmin(){
- Scanner input=new Scanner(System.in);
- System.out.print("Enter Path for admin : ");
- String adminPath = input.nextLine();
- Document doc1 = null;
- DocumentBuilder dBuilder1 = null;
- File admin_File = new File(adminPath);
- try {
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- dBuilder1 = dbFactory.newDocumentBuilder();
- doc1 = dBuilder1.parse(admin_File);
- doc1.getDocumentElement().normalize();
- System.out.println("Root element :" + doc1.getDocumentElement().getNodeName());
- NodeList doc1_List = doc1.getElementsByTagName("admin");
- System.out.println("----------------------------");
- String [] admin_ID = new String [doc1_List.getLength()];
- String [] admin_Name = new String [doc1_List.getLength()];
- String [] admin_Password = new String [doc1_List.getLength()];
- for (int k = 0; k < doc1_List.getLength(); k++) { //-------nList tukar doc1_List
- Node nNode = doc1_List.item(k);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- admin_ID [k] = eElement.getElementsByTagName("id").item(0).getTextContent();
- admin_Name [k] = eElement.getElementsByTagName("name").item(0).getTextContent();
- admin_Password [k] = eElement.getElementsByTagName("password").item(0).getTextContent();
- } // ==============Close if
- }
- }// ================Close for
- catch (Exception e) {
- // TODO: handle exception
- System.out.println("File not found. Please Try Again.");
- System.out.println();
- loadFileAdmin();
- }
- }
- /*
- }
- System.out.println("Enter Path for admin : ");
- String adminPath = input.nextLine();
- Document doc1 = null;
- DocumentBuilder dBuilder1 = null;
- File admin_File = new File(adminPath);
- try {
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- dBuilder1 = dbFactory.newDocumentBuilder();
- doc1 = dBuilder1.parse(admin_File);
- }
- catch (Exception e) {}
- //optional, but recommended
- //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
- doc1.getDocumentElement().normalize();
- System.out.println("Root element :" + doc1.getDocumentElement().getNodeName());
- NodeList doc1_List = doc1.getElementsByTagName("admin");
- System.out.println("----------------------------");
- String [] admin_ID = new String [doc1_List.getLength()];
- String [] admin_Name = new String [doc1_List.getLength()];
- String [] admin_Password = new String [doc1_List.getLength()];
- for (int temp = 0; temp < doc1_List.getLength(); temp++) { //-------nList tukar doc1_List
- Node nNode = doc1_List.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- admin_ID [temp] = eElement.getElementsByTagName("id").item(0).getTextContent();
- admin_Name [temp] = eElement.getElementsByTagName("name").item(0).getTextContent();
- admin_Password [temp] = eElement.getElementsByTagName("password").item(0).getTextContent();
- } // ==============Close if
- }// ================Close for
- */
- // ==================================================SALES=============================================================
- public static void loadFileSales(){
- Scanner input=new Scanner(System.in);
- System.out.print("Enter Path for Sales : ");
- String salesPath = input.nextLine();
- Document doc2 = null;
- DocumentBuilder dBuilder2 = null;
- File sales_File = new File(salesPath);
- try {
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- dBuilder2 = dbFactory.newDocumentBuilder();
- doc2 = dBuilder2.parse(sales_File);
- doc2.getDocumentElement().normalize();
- System.out.println("Root element :" + doc2.getDocumentElement().getNodeName());
- NodeList doc2_List = doc2.getElementsByTagName("salesperson");
- System.out.println("----------------------------");
- String [] sales_ID = new String [doc2_List.getLength()];
- String [] sales_Name = new String [doc2_List.getLength()];
- String [] sales_Password = new String [doc2_List.getLength()];
- for (int k = 0; k < doc2_List.getLength(); k++) { //-------nList tukar doc1_List
- Node nNode = doc2_List.item(k);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- sales_ID [k] = eElement.getElementsByTagName("id").item(0).getTextContent();
- sales_Name [k] = eElement.getElementsByTagName("name").item(0).getTextContent();
- sales_Password [k] = eElement.getElementsByTagName("password").item(0).getTextContent();
- } // ==============Close if
- }
- }// ================Close for
- catch (Exception e) {
- // TODO: handle exception
- System.out.println("File not found. Please Try Again.");
- System.out.println();
- loadFileSales();
- }
- }
- // ==================================================SALES=============================================================
- /*
- System.out.println("Enter Path for Salesperson : ");
- String salesPath = input.nextLine();
- Document doc2 = null;
- DocumentBuilder dBuilder2 = null;
- File sales_File = new File(salesPath);
- try {
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- dBuilder2 = dbFactory.newDocumentBuilder();
- doc1 = dBuilder2.parse(admin_File);
- }
- catch (Exception e) {}
- //optional, but recommended
- //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
- doc1.getDocumentElement().normalize();
- System.out.println("Root element :" + doc1.getDocumentElement().getNodeName());
- NodeList doc2_List = doc1.getElementsByTagName("salesperson");
- System.out.println("----------------------------");
- String [] salesperson_ID = new String [doc2_List.getLength()];
- String [] salesperson_Name = new String [doc2_List.getLength()];
- String [] salesperson_Password = new String [doc2_List.getLength()];
- for (int temp = 0; temp < doc2_List.getLength(); temp++) { //-------nList tukar doc1_List
- Node nNode = doc2_List.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- salesperson_ID [temp] = eElement.getElementsByTagName("id").item(0).getTextContent();
- salesperson_Name [temp] = eElement.getElementsByTagName("name").item(0).getTextContent();
- salesperson_Password [temp] = eElement.getElementsByTagName("password").item(0).getTextContent();
- } // ==============Close if
- }// ================Close for
- */
- // ==================================================LOGIN=============================================================
- public static void Login(){
- Scanner input=new Scanner(System.in);
- while (true){
- for (int a = 0; a<3; a++) {
- System.out.print("Enter ID: ");
- String IDSales = input.nextLine();
- System.out.print("Password: ");
- String passwordSales = input.nextLine();
- MessageDigest m = MessageDigest.getInstance("MD5");
- m.reset();
- m.update(passwordSales.getBytes());
- byte[] digest = m.digest();
- BigInteger bigInt = new BigInteger(1,digest);
- String hashtext = bigInt.toString(16);
- // Now we need to zero pad it if you actually want the full 32 chars.
- while(hashtext.length() < 32 ){
- hashtext = "0"+hashtext;
- }
- //boolean IDBoolean = Arrays.asList (sales_ID).contains(IDSales);
- //boolean PasswordBoolean = Arrays.asList(sales_Password).contains(passwordSales);
- if (Arrays.asList (sales_ID).contains(IDSales) == true && Arrays.asList(sales_Password).contains(passwordSales) == true){ // untuk sales
- System.out.println("Access Granted");
- break;
- }
- else if{ /////untuk admin
- System.out.println("");
- }else
- }
- for (int a = 0; a<3; a++) {
- System.out.print("Username: ");
- String username = input.nextLine();
- System.out.print("Password: ");
- String password = input.nextLine();
- MessageDigest m = MessageDigest.getInstance("MD5");
- m.reset();
- m.update(password.getBytes());
- byte[] digest = m.digest();
- BigInteger bigInt = new BigInteger(1,digest);
- String hashtext = bigInt.toString(16);
- // Now we need to zero pad it if you actually want the full 32 chars.
- while(hashtext.length() < 32 ){
- hashtext = "0"+hashtext;
- }
- }
- } // ----------close try
- catch (Exception e) {
- e.printStackTrace();
- } // ----------close catch
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement