Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package readxml;
  7.  
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import org.xml.sax.Attributes;
  12. import org.xml.sax.SAXException;
  13. import org.xml.sax.XMLReader;
  14. import org.xml.sax.helpers.DefaultHandler;
  15. import org.xml.sax.helpers.XMLReaderFactory;
  16.  
  17. /**
  18.  *
  19.  * @author NOOB
  20.  */
  21. public class Main extends DefaultHandler{
  22. static boolean checkUname = false;
  23. static boolean checkUpass = false;
  24. static String uname = "";
  25. static String upass = "";
  26. static String loginName="",loginPass="";
  27. static int choice = 0;
  28.     /**
  29.      * @param args the command line arguments
  30.      */
  31.     public static void main(String[] args) {
  32.         try {
  33.             XMLReader parser = XMLReaderFactory.createXMLReader();
  34.             Main objMain = new Main();
  35.             parser.setContentHandler(objMain);
  36.             parser.parse("UserInformation.xml");
  37.             menuChoose();
  38.         } catch (Exception ex) {
  39.             ex.printStackTrace();
  40.         }
  41.     }
  42.  
  43.     @Override
  44.     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  45.         super.startElement(uri, localName, qName, attributes);
  46.         if (qName.equals("UserName")) {
  47.             checkUname = true;
  48.         }
  49.         if (qName.equals("Password")) {
  50.             checkUpass = true;
  51.         }
  52.     }
  53.  
  54.     @Override
  55.     public void characters(char[] ch, int start, int length) throws SAXException {
  56.         super.characters(ch, start, length);
  57.         if (checkUname) {
  58.             uname = new String(ch,start,length);
  59.             checkUname = false;
  60.         }
  61.         if (checkUpass) {
  62.             upass = new String(ch,start,length);
  63.             checkUpass = false;
  64.         }
  65.     }
  66. public static void loginUser()
  67. {
  68.     do
  69.     {
  70.         Scanner input = new Scanner(System.in);
  71.         System.out.println("Please enter username: ");
  72.         loginName = input.nextLine();
  73.         System.out.println("Please enter password");
  74.          loginPass = input.nextLine();
  75.         if (loginName.equals(uname)&& loginPass.equals(upass)) {
  76.         System.out.println("Login success");
  77.         }else
  78.         {
  79.         System.out.println("Login failed! Try again!");
  80.         }
  81.          System.out.println("Do you continue?:(1= Yes | 2=No)");
  82.          choice = input.nextInt();
  83.     }while(choice!=2);
  84.    
  85. }
  86. public static void menuChoose()
  87. {
  88.     int chose = 0;
  89.     Scanner input = new Scanner(System.in);
  90.     System.out.println("Please choose your method:\n1.Login\n2.Exit\n"
  91.             + "****************************************");
  92.     chose = input.nextInt();
  93.         switch(chose)
  94.         {
  95.             case 1:
  96.                 loginUser();
  97.                 break;
  98.             case 2:
  99.                 System.exit(0);
  100.                 break;
  101.             default:
  102.                System.out.println("Invalid choice!!!");
  103.                break;
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement