Sunday, 11 January 2015

Clinic Reservation in Java

This is one of the final project in our java programming class. Lecturer gave us task to complete the program with certain function to record patient details using simple array. Maybe this program is not 100% perfect, because maybe my logarithm skills is not good enough. I couldn't find the solution to delete element and if the element is empty shouldn't show. Anyway here is my code.

Here the output will be in console.

So, I will upload different method to explain every function of the method code.


This code above is the class on this program. All method and main method will be inside this bracket. What is class? I can assume that class is like motorbike, in motorbike has different name/brand. The object that has created from class will inherit the behavior from the class.


//Declare and assign the final value so that wont get change by another method
    public static final String[] name = new String[3];
    public static final int[] ic = new int[3];
    public static final String[] phone = new String[3];
    public static int i = 0;
    public static int sum = 0;

this code above is declaration of variable that has base value that can be accessed from all method inside the class. That's why, I declare outside from main method but inside the class. If state "final", it means that the value of the variable can be changed from any method function. So, the result will be final !


public static void main(String[] args) {

        int x, index,id;

       Scanner in = new Scanner(System.in);
        do {
        System.out.print("\n\nWelcome to the Automated Reservation Machine!\n");
        System.out.println("Select from the following menu options below:\n");
        System.out.println("========================");
        System.out.println("| [1]  Add Patient   |");
        System.out.println("| [2]  Print         |");
        System.out.println("| [3]  Search        |"); 
        System.out.println("| [4]  Edit          |");
        System.out.println("| [5]  Sum / Average |");
        System.out.println("========================");
        System.out.print("Please select your option now: ");
        x = in.nextInt();
            
            if (x==1){
               addPatient();           
            }
            
            else if (x==2){
                printPatient();  
            }
            
            else if (x==3){   
                System.out.print("Search patient by Id: ");
                id = in.nextInt();
                System.out.println("-----------------");
                index = getIndex(id);
                searchPatient(index);
            } 
            
            else if(x==4){
                System.out.println("Edit refers to Id!");
                System.out.print("Please enter Id: ");
                id = in.nextInt();
                System.out.println("-----------------");
                index = getIndex(id);
                searchPatient(index);
                if (index!=-1){
                    editData(index);
                }
            }
            
            else if(x==5){
                int r=0;
                if(name[r]==null){
                    System.out.println("Today we have no patient ");
                }
                else {   
                System.out.println("Average per day is RM "+averagePatient(sum));}
                System.out.println("Sum = RM "+sum);
            }
            
            else
                System.out.println("Please choose the option correctly");
        }while(x!=0);
}//end main method

The code above is the main method. Main method is the brain of the program. All method will work from calling the main method. It is called call method. If we have main method but we cannot utilize the main method, the other method will not work. So, main method has the important role.


public static void addPatient(){
        
       Scanner input = new Scanner(System.in);
       
            if(name[name.length-1]!=null){
                System.out.println("SORRY DATA IS FULL !!!"); //it will appear when data is full
            }
            else {
                System.out.print("\nPlease enter the name: ");
                name[i]  = input.nextLine();
                System.out.print("Please enter the phone number: ");
                phone[i] = input.nextLine();
                System.out.print("Please enter the Id: ");
                ic[i] = input.nextInt();
                sum+=5;
                i++;
            }
    }//end addPatient method

public static void editData(int z){
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter new name: ");
        name[z]=in.nextLine();
        System.out.print("Please enter new phone: ");
        phone[z]=in.nextLine();
        System.out.print("Please enter new Ic: ");
        ic[z]=in.nextInt();
        
    }//end editData method

The method above is to edit data inside the array. We can change the element of the array if we know the index of the array that we would like to change. How to get index of the array? we can use method getIndex().

The method above is how to print all the patient for each array. If the index of array name is null/empty, it will return "Sorry No Data Available", else it will print all the element.


public static int getIndex(int a){
        
        for (int d = 0; d < ic.length; d++)
      {
           if (ic[d] == a ) 
                 return d;  //We found it!!!
      }
        return -1;
    }//end getIndex method

As I told you above that we need getIndex method to know which index that we need to print, search and edit. We call that method then if the data is found, getIndex method will return the index to the calling method, and if not found it will return -1.

public static void searchPatient(int a){ 
        
        if(a==-1){ //Condition when return value from index not found -1
            System.out.println("\nThe  customer information is not found!!!");
            System.out.println("__________________________________");
        }
        else
        System.out.println(a+1+"\t\t"+name[a]+"\t\t"+phone[a]+"\t\t"+ic[a]);
    }//end searchPatient method

Search method is needed if we want to find data based on the id of the person. It also takes getIndex method to find the data. If not found it will return -1, and say "Customer information is not found", else it will print the exact information.

Lastly, I will upload all the codes in one class.

package patientreservation;

import java.util.Scanner;

public class PatientReservation {
    
    //Declare and assign the final value so that wont get change by another method
    public static final String[] name = new String[3];
    public static final String[] ic = new String[3];
    public static final String[] phone = new String[3];
    public static int i = 0;
    public static int sum = 0;

    public static void main(String[] args) {

        int x, index,id;

        Scanner in = new Scanner(System.in);
        do {
        System.out.print("\n\nWelcome to the Automated Reservation Machine!\n");
        System.out.println("Select from the following menu options below:\n");
        System.out.println("========================");
        System.out.println("| [1]  Add Patient   |");
        System.out.println("| [2]  Print Patient |");
        System.out.println("| [3]  Search Patient|"); 
        System.out.println("| [4]  Edit Patient  |");
        System.out.println("| [5]  Sum / Average |");
        System.out.println("| [6]  Delete        |");
        System.out.println("========================");
        System.out.print("Please select your option now: ");
        x = in.nextInt();
            
            if (x==1){
                addPatient();           
            }
            
            else if (x==2){
                printPatient();  
            }
            
            else if (x==3){   
                System.out.print("Search patient by Id: ");
                id = in.nextInt();
                System.out.println("-----------------");
                index = getIndex(id);
                searchPatient(index);
            } 
            
            else if(x==4){
                System.out.println("Edit refers to Id!");
                System.out.print("Please enter Id: ");
                id = in.nextInt();
                System.out.println("-----------------");
                index = getIndex(id);
                searchPatient(index);
                if (index!=-1){
                    editData(index);
                }
            }
            
            else if(x==5){
                int r=0;
                if(name[r]==null){
                    System.out.println("Today we have no patient :)");
                }
                else {   
                System.out.println("Average per day is RM "+averagePatient(sum));}
                System.out.println("Sum = RM "+sum);
            }
           
            else if(x==6){
                if(name[0]==null)
                {
                    System.out.println("Sorry no data available");
                }
                else {
                    System.out.print("Search patient by Id: ");
                    id = in.nextInt();
                    System.out.println("-----------------");
                    index = getIndex(id);
                    searchPatient(index);
                    
                        if(index!=-1){
                       deletePatient(index);   
                    }
                    
                }
            }
            else
                System.out.println("Please choose the option correctly");
        }while(x!=0);
}//end main method      
    
    public static void deletePatient(int a){
          name[a]="-";
          phone[a]="-";
          ic[a]="-";
          i--;
    }
    
    public static void addPatient(){
        
       Scanner input = new Scanner(System.in);
       
            if(name[name.length-1]=="-"){
                System.out.print("\nPlease enter the name: ");
                name[i]  = input.nextLine();
                System.out.print("Please enter the phone number: ");
                phone[i] = input.nextLine();
                System.out.print("Please enter the Id: ");
                ic[i] = input.nextLine();
            }
            else if(name[name.length-1]!=null){
                  System.out.println("SORRY DATA IS FULL !!!"); //it will appear when data is full
            }
            
            else {
                System.out.print("\nPlease enter the name: ");
                name[i]  = input.nextLine();
                System.out.print("Please enter the phone number: ");
                phone[i] = input.nextLine();
                System.out.print("Please enter the Id: ");
                ic[i] = input.nextLine();
                 
            }
            sum+=5;
            i++; 
    }//end addPatient method
    
    public static void printPatient(){
        
        System.out.println("______________________________________________________________");
        System.out.println("No.\t\tName\t\tPhone Number\t\tIC");
        System.out.println("--------------------------------------------------------------");
        
        int a=0;
        if(name[a]==null){
            System.out.println("SORRY NO DATA AVAILABLE !!!"); //when data is not available
        }
        else
           
            for(int j=0;j<ic.length;j++){
                
                if((name[j]==null)&&(phone[j]==null)&&(ic[j]==null))
                {
                    name[j]="-";
                    phone[j]="-";
                    ic[j]="-";
                }
                System.out.println(j+1+"\t\t"+name[j]+"\t\t"+phone[j]+"\t\t\t"+ic[j]);
            }
    }//end PrintPatient method
    
    public static double averagePatient(int d){
        
        return d/i;
        
    }//end averagePatient method

    public static void editData(int z){
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter new name: ");
        name[z]=in.nextLine();
        System.out.print("Please enter new phone: ");
        phone[z]=in.nextLine();
        System.out.print("Please enter new Ic: ");
        ic[z]=in.nextLine();
        
    }//end editData method
    
    public static int getIndex(int a){
        
        String s;
        s = Integer.toString(a);
        
        for (int i = 0; i < ic.length; i++)
      {
           if (ic[i].equals(s) ) 
                 return i;  //We found it!!!
      }
        return -1;
    }//end getIndex method
    
    public static void searchPatient(int a){ 
        
        if(a==-1){ //Condition when return value from index not found -1
            System.out.println("\nThe  customer information is not found!!!");
            System.out.println("__________________________________");
        }
        else
        System.out.println(a+1+"\t\t"+name[a]+"\t\t"+phone[a]+"\t\t"+ic[a]);
    }//end searchPatient method
}//endclass

Share:

Sunday, 4 January 2015

Season Calculator in Java

For those of you where your place don't have four seasons like europe and any other countries have, don't be sad to determine which date of month is the season would be. I'd received this assignment from my lecturer to determine the season according to the date and month.

package calender;
import java.util.*;
public class Calender {

    public static void main(String[] args) {
        int date, month; //declare the variable
        
        //declare the Scanner so that user can store value to memory
        Scanner input = new Scanner(System.in);
        
        //Asking user to enter the date
        System.out.print("Please enter date: ");
        date = input.nextInt();
        
        //Asking user to enter month
        System.out.print("Please enter month: ");
        month = input.nextInt();
        
        //Condition to determine month not more than 13 and day not more than 32
        if (month >= 13 || month < 1 || date > 31 || date <= 0) {
            System.out.println("Please enter a valid date and month");
        } else {
            System.out.print("On "+getMonth(month)+", "+date+" the season ");
            System.out.println("is: "+season(date,month));
            
        }
    }//end main method
    
    public static String getMonth(int z) //Task3 Addition
    {
       //Switching number to string type
        switch(z)
        {
            case 1: 
               return "January";
               
            case 2:
                return "February";
                
            case 3: 
               return "March";
               
            case 4:
                return "April";
                
            case 5: 
               return "May";
               
            case 6:
                return "June";
                
            case 7: 
               return "July";
               
            case 8:
                return "August";
                
            case 9: 
               return "September";
               
            case 10:
                return "October";
                
            case 11: 
               return "November";
               
            case 12:
                return "December";
                
            default:
                return "No Month";
        }
    }//end getMonth method
    
    public static String season(int x, int y) //task2
    {
        String season = null; //Initializing season before statement
      
        //The condition when the season start
        if (((y == 12)&&(x>=16))||((y<=3)&&(x<=15))){
            season = "Winter";
        }          
        else if (((y >= 3)&&(x>=16))||((y<=6)&&(x<=15))){
            season = "Spring";
        }
        else if (((y >= 6)&&(x>=16))||((y<=9)&&(x<=15))){
            season = "Summer";
        } 
        else if (((y >= 9)&&(x>=16))||((y<=12)&&(x<=15))){
            season = "Fall";
        }
        return season;
    }   //end season method
}//end class

Share:

Sort First-digit of Integer Number

Maybe some of you know how to get first digit of number. For example this question: "how to get '2' from this number "2378940048"? Literally in logic, we can do this question by dividing this number with 10 and it will remain 237894004.8 (in decimal) and  we will get 237894004 (in integer), without 8 because integer does not recognize fraction /floating value. It's easy, isn't it?. Then, here it is!


  • Using Method First-digit

package firstletter;
import java.util.*;
public class FirstLetter {

    public static void main(String[] args) {
        int a; //declare the variable
        
        Scanner input = new Scanner(System.in);
        
        //Input from the user
        System.out.print("Please enter your number: ");
        a = input.nextInt();
        
        System.out.println("So the first digit of this '" +a+"' is: "+firstDigit(a));
    }//end main method
    
    public static int firstDigit(int n)
    {
        //Condition to determine the value 
       while (n < -9 || 9 < n) 
           n /= 10;
       return Math.abs(n); //Math absolute is used to round the value
    }//end First digit method
}


  • Using Single Method in main
package firstletter;
import java.util.*;
public class FirstLetter {

    public static void main(String[] args) {
        int a; //declare the variable
        
        Scanner input = new Scanner(System.in);
        
        //Input from the user
        System.out.print("Please enter your number: ");
        a = input.nextInt();
        
//Condition to determine the value 
       while (a < -9 || 9 < a ) {
           a /= 10;
}
        System.out.println("So the first digit of this '" +a +"' is: "+a);
   
}//end main method
Share:

Sort Odd and Even Number in Java

In this section, I post about java programming language. In previous campus I learnt C# language, then I moved to another campus, definitely, I have to change my language because different course syllabus. C'est rien pour moi! new campus = new life = new language = new destination. Voila, c'est la vie! must go on!

I've written the java code when lecturer gave us the assignment about how to sort odd and even number. Here, I'll show you how to solve direct sort and using array that can store certain number of data.



  • Use Array


package arraylist;

import java.util.*;
public class Arraylist{
    public static int[] array = new int[0]; //Creating and object array
    
    //Creating list integer array even and odd
    public static List even = new ArrayList<>();
    public static List odd = new ArrayList<>();

    public static void classify(){
        
        //Deciding method either the value is odd or even
        for(int i = 0 ; i < array.length ; i++){
            if(array[i] % 2 == 0)
                even.add(array[i]);
            else
                odd.add(array[i]);
        }
    }//end classify method

    public static void display(List list){
        
        //Display the value inside the list integer
        for(Integer i : list)
            System.out.print(i+" ");
    }

    public static void main(String[] args){
        //Declaring variable and creating scanner
        int num,count=1;
        Scanner in = new Scanner(System.in);
        
        //Asking user how many number to input
        System.out.print("how many input? ");
        count = in.nextInt();
        array = new int[count]; //Assigning the total of index in array
       
           for(int i = 0 ; i < array.length ; i++)
           {
            System.out.print("number would like to enter ");
            num = in.nextInt();
            array[i]=num; //Assigning to the array according to the index
            }
    
        classify(); //Call method to classify
        
        System.out.print("Even number is ");
        display(even); //Call method to display even
        System.out.print("\nOdd number is ");
        display(odd); //Call method to display odd
        System.out.print("\n");
    }
}//end Class



  • Use Direct method
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package oddevennumber;

import java.util.Scanner;

public class OddEvenNumber {
    public static void main(String[] args)throws Exception {
        
        int num = 0;

        Scanner input = new Scanner(System.in);

        while(num!=-1)
        {
            System.out.println("\n###############################");
            System.out.println("Press '-1' to exit this Even/Odd checker");
            System.out.println("###############################");
            System.out.print("number would like to enter :");
            num = input.nextInt();
            AllDigitsOod(num);
        }
        System.out.println("Thanks!");
    }
    
    public static int AllDigitsOod(int x)
    {
        boolean a=(x%2==0);
        if (a ==true)
            {
                 System.out.println(x+" is even\n");
            }          
        else if(x==-1)
        {
            System.out.println(" You exit from this game");
                }
            else 
            {
                System.out.println(x+" is odd\n");
            }
        
        return  x;
    }
}
Share: