C# Cognizant Handson Solutions

Extract Book Code – Hands -On C# Solution Cognizant

Universal Library wants to extract the book code for the arrangements and easy retrieval .

Write a program to implement this scenario to help them to extract.

Business Rules:

The book code should be length of 18 . else print “ Invalid Book Code “

1 – The first 3 position is for Department Code .

2 – The next 4 position is for Publication Years

3 – The next 5 position is for Number of Pages.

4 – The next 6 position is for Book Id

The valid Department code is  101, 102, 103 else print “ Invalid Department Code”

The valid Year of Publication is 1900 to 2020 else print “ Invalid Year “

The valid Page Number is from 00001 to 99999 else print “ Invalid Page Numbers”

The valid Book ID is character followed by numbers else print “ Invalid Book ID”

Create a class Program and get the book code as input and implement the above functionalities

Sample Input 1 :

Enter the book code of length 18

101202012345J12345

Sample Output 1:

Department Code        : 101

Year of Publication     : 2020

Number of Pages        : 12345

Book ID                      : J12345

Sample Input 2 :

Enter the book code of length 18

102201945678G54321

Sample Output 2:

Department Code        : 102

Year of Publication     : 2019

Number of Pages        : 45678

Book ID                      : G54321

Sample Input 3 :

Enter the book code of length 18

102201945678G

Sample Output 3:

Invalid Book Code

Sample Input 4:

Enter the book code of length 18

104201945678G54321

Sample Output 4:

Invalid Department Code

Year of Publication     : 2019

Number of Pages        : 45678

Book ID                      : G54321

ExtractBookCode.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtractBookCode      //Do not change the namespace name
{
    public class Program      //Do not change the class name
    {
        public static void Main(String[] arg)         //Do not change the method signature
        {
            //Implement code here
            string bookCode;
            
            Console.WriteLine("Enter the book code of length 18");
            bookCode = Console.ReadLine();
            
            if (bookCode.Length == 18)
            {
                if(bookCode.Substring(0,3) == "101" || bookCode.Substring(0,3) == "102" || bookCode.Substring(0,3) == "103")
                    Console.WriteLine("Department Code : {0}", bookCode.Substring(0,3));
                else
                    Console.WriteLine(" Invalid Department Code");
                    
                int year = Convert.ToInt32(bookCode.Substring(3,4));
                if(year>=1900 && year<=2020)
                    Console.WriteLine("Year of Publication : {0}", year);
                else
                    Console.WriteLine("Invalid Year");
                    
                long pages = Int64.Parse(bookCode.Substring(7,5));
                if(pages >= 00001 && pages<=99999)
                    Console.WriteLine("Number of Pages : {0}", pages);
                else
                    Console.WriteLine("Invalid Page Numbers");
                    
                char ch = Convert.ToChar(bookCode.Substring(12,1));
                if(Char.IsLetter(ch))
                {
                    if(Char.IsNumber(Convert.ToChar(bookCode.Substring(13,1))) && Char.IsNumber(Convert.ToChar(bookCode.Substring(14,1))) && Char.IsNumber(Convert.ToChar(bookCode.Substring(15,1))) && Char.IsNumber(Convert.ToChar(bookCode.Substring(16,1))) && Char.IsNumber(Convert.ToChar(bookCode.Substring(17,1))))
                    {
                        Console.WriteLine("Book ID : " + bookCode.Substring(12,6));
                    }
                } else
                {
                    Console.WriteLine("Invalid Book ID");
                }
            }
            else
                Console.WriteLine("Invalid Book Code");
        }
    }
}

Similar Posts