Advertisement
anhit92

Đề 5

Jun 12th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.Scanner;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. public class Demo {
  14.  
  15.     public static void main(String[] args) {
  16.         int chon;
  17.         menu();
  18.         Scanner s = new Scanner(System.in);
  19.         do {
  20.             System.out.println("Nhap menu: ");
  21.             chon = s.nextInt();
  22.             switch (chon) {
  23.                 case 1:
  24.                     System.out.println("Thoat");
  25.                     break;
  26.                 case 2:
  27.                     CreateAccount();
  28.                     break;
  29.                 case 3:
  30.                     Display();
  31.                     break;
  32.                 case 4:
  33.                     Login();
  34.                     break;
  35.                 default:
  36.                     System.out.println("Chon sai");
  37.                     break;
  38.             }
  39.         } while (chon != 1);
  40.     }
  41.  
  42.     public static void menu() {
  43.         System.out.println("1. Thoat");
  44.         System.out.println("2. Đăng ký tài khoản");
  45.         System.out.println("3. Xem thông tin tài khoản");
  46.         System.out.println("4. Dang nhap vao tai khoan");
  47.     }
  48.  
  49.     public static void CreateAccount() {
  50.         String user, password;
  51.         System.out.println("Dang ky tai khoan");
  52.         System.out.print("Nhap user Account: ");
  53.         Scanner s = new Scanner(System.in);
  54.         user = s.nextLine();
  55.  
  56.         System.out.print("Nhap pass Account: ");
  57.         Scanner s1 = new Scanner(System.in);
  58.         password = s1.nextLine();
  59.         try {
  60.             FileOutputStream fos = new FileOutputStream("login.txt");
  61.             fos.write((user+"\n").getBytes());
  62.             fos.write((password+"\n").getBytes());
  63.             fos.close();
  64.         } catch (FileNotFoundException ex) {
  65.             Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
  66.         } catch (IOException ex) {
  67.             Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
  68.         }
  69.     }
  70.  
  71.     public static void Login() {
  72.         String user1, password1;
  73.         System.out.println("Nhap thong tin dang nhap");
  74.         System.out.print("Nhap User: ");
  75.         Scanner s = new Scanner(System.in);
  76.         user1 = s.nextLine();
  77.  
  78.         System.out.print("Nhap Password: ");
  79.         Scanner s2 = new Scanner(System.in);
  80.         password1 = s2.nextLine();
  81.         String u = null, p = null;
  82.         try {
  83.             BufferedReader br = new BufferedReader(new FileReader("login.txt"));
  84.             u = br.readLine();
  85.             p = br.readLine();
  86.         } catch (FileNotFoundException ex) {
  87.             Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
  88.         } catch (IOException ex) {
  89.             Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
  90.         }
  91.         if(user1.equals(u) && password1.equals(p)){
  92.             System.out.println("Dang nhap thanh cong");
  93.         }else{
  94.             System.out.println("Dang nhap that bai");
  95.         }
  96.     }
  97.  
  98.     public static void Display() {
  99.         String u = null, p = null;
  100.         try {
  101.             BufferedReader br = new BufferedReader(new FileReader("login.txt"));
  102.             u = br.readLine();
  103.             p = br.readLine();
  104.         } catch (FileNotFoundException ex) {
  105.             Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
  106.         } catch (IOException ex) {
  107.             Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
  108.         }
  109.         System.out.println("Thong tin dang nhap");
  110.         System.out.println("User: " + u);
  111.         System.out.println("Password: " + p);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement