Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package readxml;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.helpers.DefaultHandler;
- import org.xml.sax.helpers.XMLReaderFactory;
- /**
- *
- * @author NOOB
- */
- public class Main extends DefaultHandler{
- static boolean checkUname = false;
- static boolean checkUpass = false;
- static String uname = "";
- static String upass = "";
- static String loginName="",loginPass="";
- static int choice = 0;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try {
- XMLReader parser = XMLReaderFactory.createXMLReader();
- Main objMain = new Main();
- parser.setContentHandler(objMain);
- parser.parse("UserInformation.xml");
- menuChoose();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- super.startElement(uri, localName, qName, attributes);
- if (qName.equals("UserName")) {
- checkUname = true;
- }
- if (qName.equals("Password")) {
- checkUpass = true;
- }
- }
- @Override
- public void characters(char[] ch, int start, int length) throws SAXException {
- super.characters(ch, start, length);
- if (checkUname) {
- uname = new String(ch,start,length);
- checkUname = false;
- }
- if (checkUpass) {
- upass = new String(ch,start,length);
- checkUpass = false;
- }
- }
- public static void loginUser()
- {
- do
- {
- Scanner input = new Scanner(System.in);
- System.out.println("Please enter username: ");
- loginName = input.nextLine();
- System.out.println("Please enter password");
- loginPass = input.nextLine();
- if (loginName.equals(uname)&& loginPass.equals(upass)) {
- System.out.println("Login success");
- }else
- {
- System.out.println("Login failed! Try again!");
- }
- System.out.println("Do you continue?:(1= Yes | 2=No)");
- choice = input.nextInt();
- }while(choice!=2);
- }
- public static void menuChoose()
- {
- int chose = 0;
- Scanner input = new Scanner(System.in);
- System.out.println("Please choose your method:\n1.Login\n2.Exit\n"
- + "****************************************");
- chose = input.nextInt();
- switch(chose)
- {
- case 1:
- loginUser();
- break;
- case 2:
- System.exit(0);
- break;
- default:
- System.out.println("Invalid choice!!!");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement