Question

Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....

Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion).

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

class Role {

String user, password, role;

public Role(String user, String password, String role) {

super();

this.user = user;

this.password = password;

this.role = role;

}

/**

* @return the user

*/

public String getUser() {

return user;

}

/**

* @param user the user to set

*/

public void setUser(String user) {

this.user = user;

}

/**

* @return the password

*/

public String getPassword() {

return password;

}

/**

* @param password the password to set

*/

public void setPassword(String password) {

this.password = password;

}

/**

* @return the role

*/

public String getRole() {

return role;

}

/**

* @param role the role to set

*/

public void setRole(String role) {

this.role = role;

}

}

public class Main{

public static void main(String[] args) {

Map<String, Role> userCredentials = new HashMap<>();

userCredentials.put("Rebecca", new Role("Rebecca", "1234", "user"));

userCredentials.put("Lola", new Role("Lola", "9876", "user"));

userCredentials.put("Michelle", new Role("Michelle", "7777", "super user"));

Scanner sc = new Scanner(System.in);

String name, password;

System.out.println("Login: ");

name = sc.nextLine();

System.out.println("Password: ");

password = sc.nextLine();

if (userCredentials.containsKey(name) && userCredentials.get(name).getPassword().equals(password)) {

System.out.println("Login granted to " + name);

} else {

System.out.println("Wrong credentials");

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

class WrongCredentialsException extends Exception {

public WrongCredentialsException(String message) {

super(message);

}

}

class Role {

String user, password, role;

public Role(String user, String password, String role) {

super();

this.user = user;

this.password = password;

this.role = role;

}

/**

* @return the user

*/

public String getUser() {

return user;

}

/**

* @param user the user to set

*/

public void setUser(String user) {

this.user = user;

}

/**

* @return the password

*/

public String getPassword() {

return password;

}

/**

* @param password the password to set

*/

public void setPassword(String password) {

this.password = password;

}

/**

* @return the role

*/

public String getRole() {

return role;

}

/**

* @param role the role to set

*/

public void setRole(String role) {

this.role = role;

}

}

public class Main {

public static void main(String[] args) throws WrongCredentialsException {

Map<String, Role> userCredentials = new HashMap<>();

userCredentials.put("Rebecca", new Role("Rebecca", "1234", "user"));

userCredentials.put("Lola", new Role("Lola", "9876", "user"));

userCredentials.put("Michelle", new Role("Michelle", "7777", "super user"));

Scanner sc = new Scanner(System.in);

String name, password;

System.out.println("Login: ");

name = sc.nextLine();

System.out.println("Password: ");

password = sc.nextLine();

if (userCredentials.containsKey(name) && userCredentials.get(name).getPassword().equals(password)) {

System.out.println("Login granted to " + name);

} else {

throw new WrongCredentialsException("Wrong credentials");

}

}

}

Add a comment
Know the answer?
Add Answer to:
Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone fix the program below so it does what the picture says it won't work...

    Can someone fix the program below so it does what the picture says it won't work for me. import java.util.Scanner; public class Userpass { static String arr[]; static int i = 0; public static void main(String[] args) { String username, password; int tries = 0, result; do { System.out.print("Enter the username: "); username = readUserInput(); System.out.print("Enter the password: "); password = readUserInput(); result = verifyCredentials(username, password); tries++; if (result == -1) System.out.println("The username is incorrect!\n"); else if (result == -2)...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Can anyone identify which OO Design Patterns are being used in the following code?: package mis;...

    Can anyone identify which OO Design Patterns are being used in the following code?: package mis; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; class helpDeskGuidline{    private void setGuideLine(){           }    public void rateService(){           } } public class HelpDeskService extends helpDeskGuidline{ //HelpDeskService class extends helpDeskGuidline this    //called inheritance    private static int ticketId=1;    Ticket ticket; // HelpDeskService class using ticket class object. this is has-A relationship (aggregation)          ArrayList<Ticket> allTicket=new ArrayList<>();    HashMap<Ticket,Person> ticketAllocation=new HashMap<>();    HashMap<Person,Integer> assignee=new HashMap<>();    HelpDeskService(){        Person...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • package week_3; import java.util.Scanner; import java.util.*; //import java.lang.*; //import java.io.*; /** Write a program that can...

    package week_3; import java.util.Scanner; import java.util.*; //import java.lang.*; //import java.io.*; /** Write a program that can help decide if a particular programming project is best solved using a Waterfall or Agile methodology. Your program should ask the user: • How many programmers will be on the team [ More than 30 programmers -> Waterfall ] • If there needs to be firm deadlines and a fixed schedule [ Yes - > Waterfall ] • If the programmers have experience in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT