Advertisement
irishstorm

Programming - Assignment 1.java

Oct 30th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.84 KB | None | 0 0
  1. //  Import the Scanner.
  2. import java.util.Scanner;
  3.  
  4. /*
  5.     Assignment 1: Menu System
  6.     Date:23/10/2015
  7.     Time: 6:27pm
  8.     Version: 1.0 - Commented.
  9.  
  10.     ToDo:
  11.         Get user input and output strings - Done.
  12.         Use Do/While loop to repeat the question - Done.
  13.         Use Switch for options - Done.
  14.         Use a for loop to iterate a flag - Done.
  15.         Comment - Done.
  16. */
  17. public class assignment1
  18. {
  19.     public static void main(String[] args)
  20.     {
  21.         //  Initialize Varibles
  22.         Scanner input = new Scanner(System.in);
  23.         String name = "";
  24.         int option = 0,
  25.             day = 0,
  26.             month = 0,
  27.             year = 0,
  28.             loop = 0;
  29.  
  30.  
  31.         // Prints the menu to the user and gets the options they inputed.
  32.         System.out.print("\nTrying to make Programming Fun\n--------------------------------\n1) Enter Personal Details\n2) When you're 64\n3) I love USA\n4) Special Message for Someone:\n5) Some Statistics:\n0) Exit System\n\nSelect an option [0 - 5] >> ");
  33.         option = input.nextInt();
  34.  
  35.         /*  Do/While - Statement/Condition.
  36.             Run the block of code While the condition is true, else if false skip to the next section.  */
  37.         do
  38.         {
  39.             // This controls witch option is selected depening on which number is entered.
  40.             switch (option)
  41.             {
  42.                 case 0 :
  43.                     System.out.print("Thank you for using the Program");
  44.                     //  Not sure if its good practice? kills all processes but doesn't close console?
  45.                     System.exit(0);
  46.                 break;
  47.  
  48.                
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.                 /*  Case 1
  60.                     gets user input by asking the user to input a name and date of birth it then checks if each one is valid,
  61.                     if it is not valid it will loop until they enter a value that is within the parameter specified ( 1 - 31, 1 - 12 etc)
  62.                     then it will print a string to the console with the users data, and it will increment the loop variable by 1 each time
  63.                     option 1 is selected.*/
  64.                 case 1:
  65.                     System.out.print("\nEntering Personal Details\n--------------------------------\n");
  66.  
  67.                     //  Print a string to ask for users name then store the users name in variable name, used a nextLine incase they entered fullname.
  68.                     System.out.println("What is your name:");
  69.                     name = input.nextLine();
  70.  
  71.                     //  Make sure the name is entered before continuing
  72.                     while(name.isEmpty())
  73.                     {
  74.                         name = input.nextLine();
  75.                     }
  76.  
  77.                     //  Print a string to ask the user to enter there date of birth and store the values in Variable day, month and year.
  78.                     System.out.println("Date of Birth:\nYour Day e.g, 1 to 31: ");
  79.                     day = input.nextInt();
  80.  
  81.                     //  check to see if the inputed number is valid.
  82.                     while(day < 0 || day > 31)
  83.                     {
  84.                         System.out.println("Please enter a valid day ( 1- 31 ):");
  85.                         day = input.nextInt();
  86.                     }
  87.  
  88.                     System.out.println("Your month e.g, 1 - 12: ");
  89.                     month = input.nextInt();
  90.                    
  91.                     //  check to see if the inputed number is valid.
  92.                     while(month < 0 || month > 12)
  93.                     {
  94.                         System.out.println("Please enter a valid month ( 1- 12 ):");
  95.                         month = input.nextInt();
  96.                     }
  97.  
  98.                     System.out.println("Your Year e.g, 1989: ");
  99.                     year = input.nextInt();
  100.  
  101.                     // Print a string to the user with there details, name - day/month/year.
  102.                     System.out.println("Hi " + name + " ain't programming such fun!!\nYou where born on " + day + "/" + month + "/" + year);
  103.                    
  104.                     //  Increment the counter.
  105.                     loop++;
  106.                 break;
  107.  
  108.                
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.                 /*  Case 2
  120.                     Checks to see if the user has entered there details, if they have and there current age is not greater than 2015
  121.                     it will print lyrics of a song called 64 by the beatles, it will then calculate in what year the user will be 64. */
  122.                 case 2:
  123.                     if(name.equals("") || day == 0 || month == 0 || year == 0 )
  124.                         System.out.println("No one has entered any details yet; Please enter your details via option 1");
  125.                     else if(year > 2015)
  126.                         System.out.println("Doesn't apply to you yet....");
  127.                     else
  128.                         System.out.println("**** The Beatles - When I'm 54 ****\n\nWhen i get older, losing my hair, many years from now\nwill you still be sending me valentine, birthday greetings, bottle of wine?\nif I'd been out 'till quater to three, would you lock the door\nwill you still need me, will you still feedme, when i'm sixty-four?\n\n\n" + name + " you will be 64 in " + ((64 + year)));
  129.                 break;
  130.  
  131.                
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.                 /*  Case 3
  143.                     Prints the american flag using a for loop, there will be 6 iterations for the first 3 it will print a specific patter then switch to a different one.
  144.                     * * * * * * ========================
  145.                      * * * * * *========================
  146.                     * * * * * * ========================
  147.                      * * * * * *========================
  148.                     * * * * * * ========================
  149.                     ====================================
  150.                     ====================================
  151.                     ====================================
  152.                     ====================================
  153.  
  154.                     for(int i = 0; i < 9; i++)
  155.                     {
  156.                         //  Print the string every line until i is greater than 5.
  157.                         if( i < 5)
  158.                             System.out.println("* * * * * * ========================");
  159.                         //  Using the modulus operator to print a different string every second line until i is greater than 5.
  160.                         else if((i % 2)< 1 && i < 5)
  161.                             System.out.println(" * * * * * *========================");
  162.                         else
  163.                             System.out.println("====================================");
  164.                     }
  165.                 */
  166.                 case 3:
  167.                     for (int i = 0; i < 6; i++)
  168.                     {
  169.                         if(i < 3)
  170.                             System.out.println("*****=====");
  171.                         else
  172.                             System.out.println("==========");
  173.                     }
  174.                 break;
  175.  
  176.                
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.                 /*  Case 4
  188.                     This checks if the user has entered there detaisl and if not it will return to main menu.
  189.                     if the user has entered there details it will ask them what todays date is and compare it to the birth date they entered, if they are the same print
  190.                     happy birthday and it they are not equal then print no birthday*/
  191.                 case 4:
  192.                     if(name.equals("") || day == 0 || month == 0 || year == 0 )
  193.                         System.out.println("No one has entered any details yet; Please enter your details via option 1");
  194.                     else
  195.                     {
  196.                         System.out.print("What is todays date?\n\tThe Day e.g 1 - 31: ");
  197.                         int a = input.nextInt();
  198.  
  199.                         System.out.print("\tThe Month e.g 1 - 12: ");
  200.                         int b = input.nextInt();
  201.  
  202.                         System.out.print("\tThe Year e.g 1990: ");
  203.                         int c = input.nextInt();
  204.  
  205.                         if(a == day && b == month && c == year)
  206.                             System.out.println("\n\n Happy birthday to you!!!!!!");
  207.                         else
  208.                             System.out.println("\n\n Sorry - No birthday celebrations for you today!");
  209.                     }
  210.                 break;
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.                 /*  Case 5
  223.                     Check if the user has entered there details and if they have prints how many users have entered there details, if they haven't added there
  224.                     details it brings them back to main menu. */
  225.                 case 5:
  226.                     if(name.equals("") || day == 0 || month == 0 || year == 0 )
  227.                         System.out.println("\nNo one has entered any details yet; Please enter your details via option 1");
  228.                     else
  229.                         System.out.print("\n" + loop + " person/people took part in this fun Program\n");
  230.                 break;
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.                
  243.                 /*  Default
  244.                     Prints a default string to the user if they enter a number that is not an option.*/
  245.                 default:
  246.                         System.out.print("Invalid, Please select an option.");
  247.             }
  248.  
  249.             // Prints the menu to the user and gets the options they inputed.
  250.             System.out.print("\nTrying to make Programming Fun\n--------------------------------\n1) Enter Personal Details\n2) When you're 64\n3) I love USA\n4) Special Message for Someone\n5) Some Statistics\n0) Exit System\n\nSelect an option [0 - 5] >> ");
  251.             option = input.nextInt();
  252.         }
  253.         while( option >= 0 || option <= 5);
  254.     }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement