Question

In: Computer Science

Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...

Write a class called CheckUserName.

CheckUserName must have a main method.

Your program must ask for a user name.  If the name is on the list
below (Liam, for example) greet the user saying:

welcome back: Liam

If the user is an admin (like Benkamin, for example) print:

welcome back: Benjamin
you have admin privileges

Your program must accept upper case or lower case:

emacs% java CheckUserName
enter a user name: Liam
welcome back: Liam
emacs% java CheckUserName
enter a user name: liam
welcome back: liam


Here is the list of users:

Liam
Noah
William
James
Oliver
Benjamin admin
Elijah
Lucas
Mason
Logan
Alexander admin
Ethan
Jacob
Michael
Daniel admin
Henry
Jackson
Sebastian

Don't use switch

Examples:

emacs% java CheckUserName
enter a user name: Liam 
welcome back: Liam 
emacs% java CheckUserName 
enter a user name: liam 
welcome back: liam 
emacs% java CheckUserName 
enter a user name: Oliver 
welcome back: Oliver 
emacs% java CheckUserName 
enter a user name: Benjamin 
welcome back: Benjamin 
you have admin privileges 
emacs% java CheckUserName 
enter a user name: benjamin 
welcome back: benjamin 
you have admin privileges 
emacs% java CheckUserName 
enter a user name: foo 
You are not a recognized user emacs%

I have this for the java code

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class CheckUserName {

    public static void main(String[] args) {
        //users list
        HashSet<String> users = new HashSet<>();
        users.add("liam");
        users.add("noah");
        users.add("william");
        users.add("james");
        users.add("oliver");
        users.add("elijah");
        users.add("lucas");
        users.add("mason");
        users.add("jacob");
        users.add("logan");
        users.add("ethan");
        users.add("michael");
        users.add("henry");
        users.add("sebastian");
        users.add("jackson");
        //admins list
        HashSet<String> admins = new HashSet<>();
        admins.add("benjamin");
        admins.add("alexander");
        admins.add("daniel");
        Scanner sc = new Scanner(System.in);
        System.out.println("enter a user name: ");
        String name = sc.nextLine();
        //checking in users list
        if (users.contains(name.toLowerCase())) {
            System.out.println("Welcome back " + name);
        }
        //checking in admins list
        else if (admins.contains(name.toLowerCase())) {
            System.out.println("Welcome back " + name);
            System.out.println("you have admin privileges ");
        }

        else System.out.println("You are not a recognized user");
    }
}

but I run it, it says "enter a user name:" your output, "enter a user name: welcome back: sebastian" expected output (error with the different names )

Solutions

Expert Solution

import java.util.HashSet;
import java.util.Scanner;

public class CheckUserName {

        public static void main(String[] args) {
                // users list
                HashSet<String> users = new HashSet<>();
                users.add("liam");
                users.add("noah");
                users.add("william");
                users.add("james");
                users.add("oliver");
                users.add("elijah");
                users.add("lucas");
                users.add("mason");
                users.add("jacob");
                users.add("logan");
                users.add("ethan");
                users.add("michael");
                users.add("henry");
                users.add("sebastian");
                users.add("jackson");
                // admins list
                HashSet<String> admins = new HashSet<>();
                admins.add("benjamin");
                admins.add("alexander");
                admins.add("daniel");
                Scanner sc = new Scanner(System.in);
                // need to use print instead of println
                System.out.print("enter a user name: ");
                String name = sc.nextLine();
                // checking in users list
                if (users.contains(name.toLowerCase())) {
                        System.out.println("Welcome back " + name);
                }
                // checking in admins list
                else if (admins.contains(name.toLowerCase())) {
                        System.out.println("Welcome back " + name);
                        System.out.println("you have admin privileges ");
                }

                else
                        System.out.println("You are not a recognized user");
        }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
Part I: Have a program class named TestArrays This class should have a main() method that...
Part I: Have a program class named TestArrays This class should have a main() method that behaves as follows: Have an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
n-class: Write a program to store exam scores into a file. The program will ask the...
n-class: Write a program to store exam scores into a file. The program will ask the user how many exam scores to enter. Then it will ask the user for each exam score and store them in a file called scores.dat The file output should have the following format . Exam 1: 97 Exam 2: 85 C++ Coding please
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input two integers (one by one), add the two integers, and return the sum. By using java.util.Scanner to get user input; The method may not compile due to Scanner Class which need to add a "throws" statement onto the end of the method header because some lines may throw exceptions Refer to the Java API documentation on Scanner to figure out which specific Exception should...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
Write a program that utilizes a function called paymentCalc. In the main body of the program,...
Write a program that utilizes a function called paymentCalc. In the main body of the program, ask the user what is their principal and how many months they want the loan. If it is easier, ask the user for number of years, but don’t forget to change it to months for the calculation. Use a function to calculate their monthly mortgage payment. In the main body of the program, print out what their monthly mortgage payment will be. For simplicity,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT