paulamitjava / corejava1.8

0 stars 0 forks source link

Core Java : Programs #8

Open paulamitjava opened 5 years ago

paulamitjava commented 5 years ago

// ATM Application

package atm_project;

import java.util.Scanner;

class DBS{

int balance;

void userChoice() {
    System.out.println("Enter Your Choice : ");
    System.out.println("1. Check Balanace");
    System.out.println("2. Deposit");
    System.out.println("3. Withdraw");
    System.out.println("4. Exit");

    Scanner sc = new Scanner(System.in);
    int choice = sc.nextInt();

    switch (choice) {
    case 1:
        checkBalnace();
        break;
    case 2:
        deposit();
        break;
    case 3:
        withdraw();
        break;
    case 4:
        exit();
        break;

    default:
        System.out.println("Invalid Option!!");
        break;
    }       
} //  end of userChoice

void myBusinessLogic() {
    String choice;
    do {
        userChoice();
        System.out.println("Wish to Continue (yes/no) : ");
        Scanner sc = new Scanner(System.in);
        choice = sc.next();
    } while(!choice.equals("no"));

} // end of myBusinessLogic

void withdraw() {
    System.out.println("Enter Amout to withdraw : ");
    Scanner sc = new Scanner(System.in);
    int amount = sc.nextInt();

    balance = balance - amount;
    System.out.println("Transaction Completed..");

} // end of withdraw

void deposit() {
    System.out.println("Enter Amout to deposit : ");
    Scanner sc = new Scanner(System.in);
    int amount = sc.nextInt();

    balance = balance +amount;
    System.out.println("Transaction Completed..");

} // end of deposit

void checkBalnace() {

    System.out.println("Available Balance : "+balance);

} // end of checkBalnace

void exit() {

    System.out.println("Thanking for Banking with Us!!");

} // end of exit

}

public class DBS_ATM {

public static void main(String[] args) {

    DBS ref = new DBS();
    ref.myBusinessLogic();

}

}

paulamitjava commented 5 years ago

// User Login Authentication

// View public class LoginAuthetication {

public static void main(String[] args) {

    BusinessLogic ref = new BusinessLogic();
    ref.myLogic();

}

}

// Controller public class BusinessLogic {

UserDAO userDAORef;

public void myLogic() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter ID : ");
    int id = sc.nextInt();  // 1000

    System.out.println("Enter Password : ");
    String pwd = sc.next();

    User refUser = new User();
    refUser.setUserID(id); // 1000
    refUser.setUserPWD(pwd);

    userDAORef = new UserDAO();

    boolean status = userDAORef.userAuthentication(refUser);

    if (status==true) {
        System.out.println("Login Success!!");
    } else {
        System.out.println("Login Fail!!");
    }

}

}

// DAO (Data Access Object) public class UserDAO {

boolean status;

User userRef;

public boolean userAuthentication(User ref) {

    if (ref.getUserID()==100 && ref.getUserPWD().equals("admin")) 
    {
        status =true;
    } 

    else {
        status = false;
    }
    return status;

}

}

// Model public class User {

private int userID; 
private String userPWD;

public int getUserID() {
    return userID;
}
public void setUserID(int userID) { 
    this.userID = userID;
}
public String getUserPWD() {
    return userPWD;
}
public void setUserPWD(String userPWD) {
    this.userPWD = userPWD;
}

}

paulamitjava commented 5 years ago

// User Defined Array Example

class Person{

String name; // global variable
int mobile;  // global variable

public Person(String name, int mobile) { // constructor
    this.name=name;
    this.mobile=mobile;
}

@Override
public String toString() {   // converts object to string
    return name + " "+mobile;
}

} // end of Person

public class ArrayExample {

void arrayDisplay(String myArray[]) {
    for (int i = 0; i < myArray.length; i++) {
        System.out.println(myArray[i]);
    }
}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("How many records you want to store : "); 
int number = sc.nextInt(); // 5

Person p[] = new Person[number];    // 5

for (int i = 0; i < p.length; i++) {
    System.out.println("Enter Name : ");
    String name = sc.next();

    System.out.println("Enter Mobile : ");
    int mobile = sc.nextInt();

    p[i] = new Person(name,mobile); // storing data to an array

}

for (Person person : p) {
    System.out.println(person); // retrieving data from an array
}

}

}