Hole with Random Generator

import java.util.Scanner;
public class randomHole{
String[][] holeStore;
static int Ncol=5;
static int Nrow=3;
int token;
randomHole(){
holeStore = new String[Nrow][Ncol];
for(int i=0; i<Nrow; i++){
for(int j=0; j<Ncol; j++){
holeStore[i][j] = “Free”;
}

}
}

void set_full(int row,int col){
findToken();
boolean valid_token = false;
for(int i=0; i<Nrow; i++){
for(int j=0; j<Ncol; j++){
if(holeStore[i][j] == Integer.toString(token)){
set_full(row,col);
}
else{
valid_token = true;
}
}
}
if(valid_token){
holeStore[row-1][col-1] = Integer.toString(token);
System.out.println(“Your Token is : “+token);
}
}

void set_Null(int row, int col){
holeStore[row-1][col-1] = “Free”;
}
void findHole(int token){
boolean is_found = false;
for(int i=0; i<Nrow; i++){
for(int j=0; j<Ncol; j++){
if( holeStore[i][j].equals(Integer.toString(token))){
int row = i+1;
int col = j+1;
System.out.println(“Your Row is : “+row);
System.out.println(“Your Colum is “+col);
is_found = true;
}
}
}
if(is_found == false){
System.out.println(“Sorry Your Token Not Found!\nTry Again.”);
}
}

int findToken(){
token = (int)Math.round(Math.random()*(Nrow*Ncol));
return token;
}
void search_token(int row, int col){
System.out.println(“Your Token is “+holeStore[row-1][col-1]);
}

void find_free(){
for(int i=0; i<Nrow; i++){
for(int j=0; j<Ncol; j++){
if(holeStore[i][j] == “Free”){
System.out.println(“Row : “+(i+1)+”\tColumn: “+(j+1)+”\t:is Empty”);

}
else{
System.out.println(“Row : “+(i+1)+”\tColumn: “+(j+1)+”\t:is Full”);
}
}
}
}

void current_Status(){
for(int i=0; i<3; i++){
for(int j=0; j<5; j++){
System.out.println((i+1)+” “+(j+1)+” :”+holeStore[i][j]);
}
}
}
void find_firt_free(){
boolean empty_found=false;
for(int i=0; i<Nrow; i++){
for(int j=0; j<Ncol; j++){
if(holeStore[i][j] == “Free”){
System.out.println(“You can store at Row : “+(i+1)+”\tColumn: “+(j+1)+”\t:Now!”);
empty_found =true;
break;
}

}
if(empty_found = true){
break;
}
else if(empty_found == false){
System.out.println(“All Holes a full”);
}
}
}

public static void main(String args[]){
int token;
randomHole H = new randomHole();
Scanner S = new Scanner(System.in);
int choice;
do{
System.out.println(“What you want to Do:\nEnter the Choice\n1:For Find Token No.\n2:For Find Row & Column\n3:For Find Free Holes”
+”\n4:For Store a thing\n5:For Retrive a thing”+”\n6:For Current Hole Status\n7:Find First Free Hole\n8: For Exit”);
choice = S.nextInt();
if( choice == 2)
{
System.out.println(“Enter your token No:”);
token = S.nextInt();
if(token>0 && token<=(Nrow*Ncol)){
H.findHole(token);
}
else{
System.out.println(“Token Must be between 0 and “+(Nrow*Ncol));
}
}
else if( choice ==1)
{
System.out.println(“Enter your Row No:”);
int row = S.nextInt();
System.out.println(“Enter your Column No:”);
int col = S.nextInt();
if(row>0 && col>0 && row<=Nrow && col<=Ncol)
H.search_token(row,col);
else
System.out.println(“Invalid Entry, Out of Max Row or Max Column”);
}
else if( choice == 3){
H.find_free();
}
else if( choice ==4){
System.out.print(“Enter Row No:”);
int row = S.nextInt();
System.out.print(“Enter Column No:”);
int col = S.nextInt();
if(row>0 && col>0 && row<=Nrow && col<=Ncol){
H.set_full(row,col);
System.out.println(“Item has been STORED”);
}
else
System.out.println(“Invalid Entry, Out of Max Row or Max Column”);


}
else if( choice ==5){
System.out.print(“Enter Row No:”);
int row = S.nextInt();
System.out.print(“Enter Column No:”);
int col = S.nextInt();
H.set_Null(row,col);
System.out.println(“Thank you Come Again”);
}
else if( choice == 8){
System.exit(0);
}
else if( choice == 6){
H.current_Status();
}
else if(choice == 7){
H.find_firt_free();
}
else{
System.out.println(“Invalid Entry”);

}
System.out.println(“***********************”);
}while(choice != 8);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s