Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java_assessment_1;
- import java.util.regex.Pattern;
- import javax.swing.JOptionPane;
- import java.util.Scanner;
- /**
- *
- * @author 30054547
- */
- public class Java_Assessment_1 {
- //create a global string variable called logonID for later use
- String logonID;
- public static void main(String[] args) {
- //create a new instance of the program in the memory
- Java_Assessment_1 prog = new Java_Assessment_1();
- prog.logon();
- }
- public void logon(){
- //create a variable called logonID and take their attempt at the logon
- logonID = JOptionPane.showInputDialog("please enter your logon ID");
- //check if their attempt is correct and either let them on or tell them their attempt is wrong
- if(logonID == null){
- System.exit(0);
- }
- if((logonID.equals(" ") || logonID.isEmpty())){
- JOptionPane.showMessageDialog(null,"please enter a logonID");
- logon();
- }
- if(logonID.length()!= 4){
- JOptionPane.showMessageDialog(null,"please enter a 4 character logon ID");
- logon();
- }
- if(logonID.equals("1234")) {
- menu();
- }
- else JOptionPane.showMessageDialog(null,"incorrect logon ID");
- logon();
- }
- public void menu(){
- //use the int choice to ask which option they would like to use and run the correct method according
- //to their input
- String choice;
- choice = JOptionPane.showInputDialog(null,"hello user " +logonID+" \nEnter 1 for user name \nEnter 2 for factorial \nEnter 3 to exit");
- //use if statements to decide which method to run
- if(choice == null){
- System.exit(0);
- }
- if ("1".equals(choice)) {
- userName();
- }
- if ("2".equals(choice)) {
- factorialResult();
- }
- if ("3".equals(choice)) {
- System.exit(0);
- }
- //display an error if the input does not match any of the options
- if (!"3".equals(choice) && !"2".equals(choice) && !"1".equals(choice)){
- JOptionPane.showMessageDialog(null,"please enter a valid input");
- menu();
- }
- }
- public void userName(){
- //ask them their full name and store it for later use
- String fullName;
- fullName = JOptionPane.showInputDialog(null,"hello user "+logonID+" what is your full name?");
- if(fullName == null){
- System.exit(0);
- }
- //carry the variable to the next method
- createUser(fullName);
- }
- public void createUser(String pFullName) {
- //use a substring to find their first initial
- String firstInitial = pFullName.substring(0,1);
- //find the position of the space between their names
- //int spacePos = pFullName.indexOf(" ");
- Scanner spaceScan = new Scanner(System.in);
- String spacePos = spaceScan.next();
- //System.out.println(s.indexOf(' '));
- //use an if to make sure there is a space in the name
- if(pFullName.contains(" ") && Pattern.matches("[a-zA-Z]+", pFullName) == false ){
- JOptionPane.showMessageDialog(null,"please enter a valid name");
- userName();
- }
- //use the space position to find their second name then create their username
- String surName = pFullName.substring((spacePos+1));
- String userName = firstInitial + surName;
- JOptionPane.showMessageDialog(null,"your username is " + userName);
- menu();
- }
- public void factorialResult(){
- //intialise your variables
- String input;
- int factorial = 0;
- int count = 0;
- //take in the number to be used in the factorial
- input = JOptionPane.showInputDialog("hello user "+logonID+"\nplease enter a number to use in a factorial");
- if(input == null){
- System.exit(0);
- }
- //perform various checks to see if the user has entered a valid number
- if(Pattern.matches(".*[a-zA-Z]+.*", input) == true || input.contains(" ") || input.contains(".")){
- JOptionPane.showMessageDialog(null,"plese enter a valid number");
- menu();
- }
- //put the input into an integer to perform further checks then calculate the factorial
- int number = Integer.parseInt(input);
- if(number == 0 || number == 1){
- JOptionPane.showMessageDialog(null, "the facrtorial of your number is 1");
- menu();
- }
- if(number < 0){
- JOptionPane.showMessageDialog(null,"please enter a positive number");
- menu();
- }
- //use this code to calculate the factorial by multiplying with the number below until the count goes down all the way
- factorial = number;
- while(count != 1){
- count = (number - 1);
- factorial = count * factorial;
- number = number - 1;
- }
- //display the users factorial and return back to the menu
- JOptionPane.showMessageDialog(null,"the factorial of your number is " + factorial);
- menu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement