Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1.  
  2. package java_assessment_1;
  3.  
  4. import java.util.regex.Pattern;
  5. import javax.swing.JOptionPane;
  6. import java.util.Scanner;
  7. /**
  8. *
  9. * @author 30054547
  10. */
  11. public class Java_Assessment_1 {
  12. //create a global string variable called logonID for later use
  13. String logonID;
  14. public static void main(String[] args) {
  15. //create a new instance of the program in the memory
  16. Java_Assessment_1 prog = new Java_Assessment_1();
  17. prog.logon();
  18. }
  19.  
  20.  
  21. public void logon(){
  22. //create a variable called logonID and take their attempt at the logon
  23. logonID = JOptionPane.showInputDialog("please enter your logon ID");
  24. //check if their attempt is correct and either let them on or tell them their attempt is wrong
  25. if(logonID == null){
  26. System.exit(0);
  27. }
  28. if((logonID.equals(" ") || logonID.isEmpty())){
  29. JOptionPane.showMessageDialog(null,"please enter a logonID");
  30. logon();
  31. }
  32. if(logonID.length()!= 4){
  33. JOptionPane.showMessageDialog(null,"please enter a 4 character logon ID");
  34. logon();
  35. }
  36. if(logonID.equals("1234")) {
  37. menu();
  38. }
  39. else JOptionPane.showMessageDialog(null,"incorrect logon ID");
  40. logon();
  41. }
  42.  
  43. public void menu(){
  44. //use the int choice to ask which option they would like to use and run the correct method according
  45. //to their input
  46. String choice;
  47.  
  48. choice = JOptionPane.showInputDialog(null,"hello user " +logonID+" \nEnter 1 for user name \nEnter 2 for factorial \nEnter 3 to exit");
  49. //use if statements to decide which method to run
  50. if(choice == null){
  51. System.exit(0);
  52. }
  53. if ("1".equals(choice)) {
  54. userName();
  55. }
  56. if ("2".equals(choice)) {
  57. factorialResult();
  58. }
  59. if ("3".equals(choice)) {
  60. System.exit(0);
  61. }
  62. //display an error if the input does not match any of the options
  63. if (!"3".equals(choice) && !"2".equals(choice) && !"1".equals(choice)){
  64. JOptionPane.showMessageDialog(null,"please enter a valid input");
  65. menu();
  66. }
  67. }
  68.  
  69. public void userName(){
  70. //ask them their full name and store it for later use
  71. String fullName;
  72. fullName = JOptionPane.showInputDialog(null,"hello user "+logonID+" what is your full name?");
  73. if(fullName == null){
  74. System.exit(0);
  75. }
  76.  
  77. //carry the variable to the next method
  78. createUser(fullName);
  79. }
  80.  
  81. public void createUser(String pFullName) {
  82. //use a substring to find their first initial
  83. String firstInitial = pFullName.substring(0,1);
  84. //find the position of the space between their names
  85. //int spacePos = pFullName.indexOf(" ");
  86. Scanner spaceScan = new Scanner(System.in);
  87. String spacePos = spaceScan.next();
  88. //System.out.println(s.indexOf(' '));
  89.  
  90. //use an if to make sure there is a space in the name
  91. if(pFullName.contains(" ") && Pattern.matches("[a-zA-Z]+", pFullName) == false ){
  92. JOptionPane.showMessageDialog(null,"please enter a valid name");
  93. userName();
  94. }
  95. //use the space position to find their second name then create their username
  96. String surName = pFullName.substring((spacePos+1));
  97. String userName = firstInitial + surName;
  98.  
  99. JOptionPane.showMessageDialog(null,"your username is " + userName);
  100. menu();
  101. }
  102.  
  103.  
  104. public void factorialResult(){
  105. //intialise your variables
  106. String input;
  107. int factorial = 0;
  108. int count = 0;
  109.  
  110. //take in the number to be used in the factorial
  111. input = JOptionPane.showInputDialog("hello user "+logonID+"\nplease enter a number to use in a factorial");
  112. if(input == null){
  113. System.exit(0);
  114. }
  115. //perform various checks to see if the user has entered a valid number
  116. if(Pattern.matches(".*[a-zA-Z]+.*", input) == true || input.contains(" ") || input.contains(".")){
  117. JOptionPane.showMessageDialog(null,"plese enter a valid number");
  118. menu();
  119. }
  120. //put the input into an integer to perform further checks then calculate the factorial
  121. int number = Integer.parseInt(input);
  122.  
  123. if(number == 0 || number == 1){
  124. JOptionPane.showMessageDialog(null, "the facrtorial of your number is 1");
  125. menu();
  126. }
  127. if(number < 0){
  128. JOptionPane.showMessageDialog(null,"please enter a positive number");
  129. menu();
  130. }
  131.  
  132. //use this code to calculate the factorial by multiplying with the number below until the count goes down all the way
  133. factorial = number;
  134. while(count != 1){
  135. count = (number - 1);
  136. factorial = count * factorial;
  137. number = number - 1;
  138. }
  139. //display the users factorial and return back to the menu
  140. JOptionPane.showMessageDialog(null,"the factorial of your number is " + factorial);
  141. menu();
  142. }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement