import javax.swing.*;

public class newBank
{

	  static int first,last,midd,binary;

 	  //static String name[]=new String[100];
      //static String pass[]=new String[100];
      //static double balance[]=new double[100];


      public int search(String name[],String target)
	        { //search

	            first=1;	last=100;

	            do
	            { // do while
	                midd=(first+last)/2;
	                if ( name[midd].lessThan(target))

	                    last=midd-1;
	                else
	                    first=midd+1;
			    } // end do while
			    while ((first>last)||(name[midd].equals(target)));

			    if (name[midd].equals(target))
			       return  binary=midd;
			    else
			       return  binary=0;
			} // end search method




	public void bank()
		{  //bank method
			String s1 = JOptionPane.showInputDialog(null," 1- ATM \n 2- Bank User \n 3- exit ");
			int option1 = Integer.parseInt(s1);
			switch (option1)
			{   //switch
				case 1:
				       ATM();
				break;

				case 2:
				       bank_user();
                break;

                case 3:
				        System.exit(0);
     			break;


		    } //end switch
        } // end bank method



    public void bank_user()
		{ //bank user

                    String name[]=new String[100];
                    String pass[]=new String[100];
                    double balance[]=new double[100];

                    int addc=0;
                    //int editc=0;
                    //int delc=0;

                    String s2 = JOptionPane.showInputDialog(null," 1- Add Customer \n 2- manage Customer \n  3- back ");
								int bankUserList = Integer.parseInt(s2);
								switch (bankUserList)
								{   //switch 1
			                        	case 1: // add customer
			                        	       String name1=JOptionPane.showInputDialog(null,"Enter your name to creat account: ");
                                               search(name,name1);
                                               if (name[binary].equals(name1))
                                                   JOptionPane.showMessageDialog(null," sorry this name of customer is registry....");
                                               else
                                                   { // else
                                                     name[addc]=name1;
                                                     pass[addc]=JOptionPane.showInputDialog(null,"Enter your password to creat account: ");
                                                     String b=JOptionPane.showInputDialog(null,"Enter your balance to creat account: ");
                                                     balance[addc]=Double.parseDouble(b);
											   	     addc++;
												    } // end else
                                        break;

                                        case 2: //manage customer
                                                String s3 = JOptionPane.showInputDialog(null," 1- delete custmer \n 2- Edit customer \n  3- back ");
									            int managList = Integer.parseInt(s3);

												switch (managList)
								                {   //switch 2
								                        case 1: //delete custmer
								                                String delcustomer=JOptionPane.showInputDialog(null,"enter the name of account to delete: ");
								                                search(name,delcustomer);
								                                   if (name[binary].equals(delcustomer))
								                                    { // if
								                                        name[binary]=null;
								                                        pass[binary]=null;
								                                        balance[binary]=0;
																	} // end if

																	else
																	JOptionPane.showMessageDialog(null,"sorry this name is not found... ");
															    //} // end for loop
                                                        break;

                                                        case 2: //Edit customer
                                                                String edit=JOptionPane.showInputDialog(null,"Enter the name of account to edit: ");
                                                                search(name,edit);
															       if (name[binary].equals(edit))
																    { // if
																         name[binary]=JOptionPane.showInputDialog(null,"enter the new name of your account: ");
		   						                                         pass[binary]=JOptionPane.showInputDialog(null,"enter the new password of your account: ");
									                                     String new_b=JOptionPane.showInputDialog(null,"enter the new balance of your account: ");
									                                     balance[binary]=Double.parseDouble(new_b);
																	} // end if


																	else
																	JOptionPane.showMessageDialog(null,"sorry this name to edite is not found... ");
                                                               //  } // end for loop
                                                        break;

                                                        case 3: // back

     			                                       	break;
													}// end switch 2


                                        case 3: // back

     			                        break;
							    }// end switch 1

		}// end bank_user method




    public  void ATM()
		{  // ATM method
		          	String name1="ali";
					String pass1="ali";
					double bal1=1000;

					String name2="omer";
					String pass2="omer";
					double bal2=2000;

					String name3="ahmed";
					String pass3="ahmed";
					double bal3=3000;

					String username, userpass;
					username= JOptionPane.showInputDialog(null,"Welcome \n Enter the Account name");

					if(( username.equals(name1) || username.equals(name2) || username.equals(name3)) == false)
						JOptionPane.showMessageDialog(null," This Account not exist");
					else

					if(username.equals(name1))
					{ // if1
						userpass= JOptionPane.showInputDialog(null,"Welcome \n Enter the Account Password");
						if(userpass.equals(pass1))
						{
							JOptionPane.showMessageDialog(null," welcome  " + name1 + "  Your balance is " + bal1);
							//obj.
							manage_balance(name1, bal1);
						}
						else
							JOptionPane.showMessageDialog(null," Sorry ..  wrong password");
					} // end if1

					else
					if(username.equals(name2))
					{ // if2
								userpass= JOptionPane.showInputDialog(null,"Welcome \n Enter the Account Password");
								if(userpass.equals(pass2))
								{
									JOptionPane.showMessageDialog(null," welcome " + name2 + "Your balance is " + bal2);
									//obj.
									manage_balance(name2, bal2);
								}

								else
									JOptionPane.showMessageDialog(null," Sorry ..  wrong password");
					} // end if2
					else
					if(username.equals(name3))
							{ // if3
								userpass= JOptionPane.showInputDialog(null,"Enter the Account Password");
								if(userpass.equals(pass3))
								{
									JOptionPane.showMessageDialog(null," welcome " + name3 + "Your balance is " + bal3);
									//obj.
									manage_balance(name3, bal3);
								}

								else
									JOptionPane.showMessageDialog(null," Sorry ..  wrong password");

					} // end if3
	}//end ATM method


	public void manage_balance(String name, double bal)
	{ // manage_balance method
		String s = JOptionPane.showInputDialog(" 1- balance \n 2- deposite \n 3- withdraw  \n 4- exit");
		int option = Integer.parseInt(s);

		switch(option)
		{// switch
			case 1:
				   JOptionPane.showMessageDialog(null," your balance is " + bal);
				   manage_balance(name,bal);
				   break;

			case 2:
					double amount1 = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter the amount"));
					bal+=amount1;
					JOptionPane.showMessageDialog(null,"Successfull.. your new balance is " + bal);
					manage_balance(name,bal);
					break;

			case 3:
					double amount2 = Double.parseDouble(	JOptionPane.showInputDialog(null,"Enter the amount"));
					bal-=amount2;
					JOptionPane.showMessageDialog(null,"Successfull.. your new balance is " + bal);
					manage_balance(name,bal);
					break;

			case 4:
     				System.exit(0);
     				break;



		} // end switch
	}//end manage_balance method


	public static void main(String ar[])
	{// main

		newBank obj= new newBank();
        obj.bank();


	} // end main
} // end class





















