Learn-dsa..in 30 days!



























CC-20 : Simulate an Ad carousel using CircularDoubleLinkedList.

Description:

Write and test code to simulate an Advertisement carousel displayed on top panel of a website home page.

Test cases and expected outputs:

Input Parameters Expected outputs
Advertisement carousel display menu :
1: Add a new Ad to carousel
2: Display Ad carousel
3: Exit Advertisement carousel display program

Choose an option:
1
Advertisement added to carousel
Choose an option:
1
Advertisement added to carousel
Choose an option:
1
Advertisement added to carousel
Choose an option:
1
Advertisement added to carousel
Choose an option:
2
Displaying Circular Ads Carousel...
Each Ad will be displayed for 4 seconds
(To stop Ad carousel, close the program manually !
***************************
Current Displayed Ad: Ad1
***************************
Current Displayed Ad: Ad2
***************************
Current Displayed Ad: Ad3
***************************
Current Displayed Ad: Ad4
***************************
Current Displayed Ad: Ad1
***************************
Current Displayed Ad: Ad2
***************************
Current Displayed Ad: Ad3
***************************
Current Displayed Ad: Ad4
***************************

Pseudocode:

Create a Circular Double Linked List Node class with String Data (instead of integer data that was used in above examples, Advertisement text will be stored as data vales in the nodes).
To allow user to add new Advertisement nodes into carousel use Circular Double Linked List “addNode()” method.
To display Ad carousel:
o Use Circular Double Linked List Node methods “getNextNode()” and “getPreviousNode()” to go to next or previous nodes.
o Display each Advertisement for 4 seconds, then go to next Advertisement in the carousel.

Code:

public static void circularSingleLinkedListDisplayAdCarousel() throws Exception{
	CircularDoubleLinkedListAdCarousel carousal=new CircularDoubleLinkedListAdCarousel();
	CircularDoubleLinkedList adCarousel=carousal.new CircularDoubleLinkedList();
 	DoubleLinkedListNode currentAdinCarousel=null;
 	int newAdCounter=0;
 	while (true) {
 	try { 				
 	System.out.println("\n\n Advertisement carousel display menu :");
 	System.out.println();
 	System.out.println("1: Add a new Ad to carousel");
 	System.out.println("2: Display Ad carousel");
 	System.out.println("3: Exit Advertisement carousel display program");
 	System.out.println();
 	System.out.println("Choose an option:\n");
 	int selectedMenuOption=readIntegerFromConsole();
 	switch(selectedMenuOption) {
 	case 1://Add a new Ad
 		newAdCounter++;
 		adCarousel.addLast(newAdCounter);
 		break;		
 	case 2: //Display
 		printAdCarousel(adCarousel);
 		   break;
 	case 3: //Exit the program
 		System.out.println("\nExiting program....\n");
 		return;
 	default: //user has input an option that is not available
 			System.out.print("\n Invalid option selected; please use one of the options given above: ");
 			break;
 	}
 	}catch (Exception exception) {
 		System.out.print("Exception: "+ exception);
 		continue;
 	}	
 	}
}

Click here to download and run code and test cases !