Example for use System class and base on array two demotion in java

In this post I will discount about how to use array two demotion in java programming language and I would like base on how to use class System for input your data from keyboard in java. In this case I have one example it very very easy to use array and class System.

My purpose in this article I will write one example about hotel management that have some features like 
1. Check In 
2. Check Out
3. Display
4. Process

         In java programming allow you to input your data from keyboard by use class System.in  that it store in your memory of machine. In my example I create use Eclipse tool for create project, you can download eclipse from website of eclipse or follow this link : http://www.eclipse.org/downloads/eclipse-packages/ 

In this example I will explain step by step about this small project.

    --> First step I open eclipse and then create new project called Hotel

          File-->New --> Java Project



        I created package name hotel and created class name hotel the same.

     - hotel.java it contain main for run this program I no need separate to other class.

     First declaration I need to declare scanner object from Class Scanner for input your data from              keyboard. 

        package hotel
        public class hotel {
           private Scanner scanner = new Scanner(System.in)
        } 

     global declaration for object scanner. Next time I declared String hotel as two demotion array and        assign to null

                       package hotel                           public class hotel {                                 private Scanner scanner = new Scanner(System.in);     
                                String [][] hotel = null;                                 } 

    Next step I created constructor of class hotel for first initialize values index of array when object        was created in program.

           package hotel
                public class hotel {
                       private Scanner scanner = new Scanner(System.in); 
                       String [][] hotel = null; 
                public hotel ( int room, int floor){ 
                       hotel = new String[room][floor]; 
               }
           }
              
   After create constructor already now I create one method called CheckIn in this method need to     input from your keyboard and set String array hotel with your data.

         public void checkIn(){
                int roomNo, floor;
System.out.println("Enter Room : ");
roomNo = scanner.nextInt();
System.out.println("Enter Floor : ");
floor = scanner.nextInt();
System.out.println("Enter Guest :");
hotel[roomNo][floor] = scanner.next()+scanner.nextLine();
          }
And next method call checkOut like same code 

public void display(){
for(String[] str : hotel){
for(String str1 : str){
System.out.printf("%-15s",str1);
}
System.out.println();
}
}

For display all data that you input I need to created one method called Display

public void display(){
for(String[] str : hotel){
for(String str1 : str){
System.out.printf("%-15s",str1);
}
System.out.println();
}
}

In this case I use foreach loop for read all data from array of String and print format as this code.

And next method called process for processing display our interface by using switching case 

public void process(){
while(true){
System.out.println("1.CheckIn     2.CheckOut    3.Display    4.Exit ");
switch(scanner.nextInt()){
case 1:this.checkIn();break;
case 2:this.checkOut();break;
case 3:this.display();break;
default: System.exit(0);
}
}
}
  Finally is main for run this program and I initialized size of array String as 5; 


public static void main(String[] args) {
new Hotel(5, 5).process();
}

After run this program it will show result like this




      Full source code

package hotel;

import java.util.Scanner;

public class Hotel {

Scanner scanner = new Scanner(System.in);

String[][] hotel = null;

public Hotel(int room, int floor) {

hotel = new String[room][floor];
}

public void checkIn() {

int roomNo, floor;

System.out.println("Enter Room : ");
roomNo = scanner.nextInt();
System.out.println("Enter Floor : ");
floor = scanner.nextInt();
System.out.println("Enter Guest :");
hotel[roomNo][floor] = scanner.next() + scanner.nextLine();
}

public void checkOut() {
int roomNo, floor;

System.out.println("Enter Room : ");
roomNo = scanner.nextInt();
System.out.println("Enter Floor : ");
floor = scanner.nextInt();
hotel[roomNo][floor] = null;

}

public void display() {

for (String[] str : hotel) {
for (String str1 : str) {
System.out.printf("%-15s", str1);
}
System.out.println();
}
}

public void process() {

while (true) {
System.out.println("\t\t\t Welcome to Hotel management");
System.out.println("\t\t1.CheckIn     2.CheckOut    3.Display    4.Exit ");
switch (scanner.nextInt()) {
case 1:
this.checkIn();
break;
case 2:
this.checkOut();
break;
case 3:
this.display();
break;
default:
System.exit(0);
}
}
}

public static void main(String[] args) {
new Hotel(5, 5).process();
}

}


  I will post next article -->

















Post a Comment

0 Comments