can somone help with this quick java program please? Write a java program that i
ID: 3745383 • Letter: C
Question
can somone help with this quick java program please?
Write a java program that implements a home security policy.
The name of the simulator should be secure_home
The policy states that only users with an authorized key can enter the home. To enter the home, the user must first put their key in the lock, then turn the lock, then enter the home, only if the key is valid. A home can be rekeyed with new keys only by the owner, and only if the owner is inside the home.
Firefighters can enter with the secret key (literal string) : FIREFIGHTER_SECRET_KEY
Police can enter as long as their user_name starts with OFFICER_WITH_WARRANT and their key is POLICE_SECRET_KEY.
You must implement the following command-line interface for your server:
./secure_home <owner_name> <key_1> <key_2> ... <key_n>
where <owner_name> is the name of the owner, and <key_1> through <key_n> are all authorized keys for the house.
All inputs to the program (keys and names) will be [a-zA-Z0-9_-] (alphanumeric, underscore, and dash). All matching is case-sensitive.
The input to the program (on standard input)) will be a series of events separated by a newline. The program must track these events and respond appropriately, while enforcing the security policy.
Every input will end in a newline, and every response must end in a newline.
INSERT KEY <user_name> <key>
User_name inserts key key into the door. Response should be: KEY <key> INSERTED BY <user_name>
TURN KEY <user_name>
<user_name> turns the key in the door. Possible responses are: SUCCESS <user_name> TURNS KEY <key> or FAILURE <user_name> UNABLE TO TURN KEY <key>
ENTER HOUSE <user_name>
<user_name> enters the house. Possible responses are: ACCESS DENIED or ACCESS ALLOWED.
WHO'S INSIDE?
Who is currently inside the home? Response should be a comma-separated list of user names, ordered by access time (earlier access first): <user_name_1>, <user_name_2>, <user_name_3>... or NOBODY HOME if there are no users in the home.
CHANGE LOCKS <user_name> <key_1> <key_2> ... <key_n>
<user_name> wishes to rekey the home with new given keys <key_1>, <key_2>, ..., <key_n>. Possible responses are: ACCESS DENIED or OK
LEAVE HOUSE <user_name>
<user_name> leaves the house. Possible responses are: OK or <user_name> NOT HERE
If any events are received that are not according to this specification, the response must be: ERROR.
Example Running the program as follows:
./secure_home that lady
Given the input:
INSERT KEY sadaf key
TURN KEY sadaf
ENTER HOUSE sadaf
INSERT KEY that lady
TURN KEY that
ENTER HOUSE that
WHO'S INSIDE?
The program will produce the following output:
KEY key INSERTED BY sadaf
FAILURE sadaf UNABLE TO TURN KEY key
ACCESS DENIED
KEY lady INSERTED BY that
SUCCESS that TURNS KEY lady
ACCESS ALLOWED
that
Explanation / Answer
import java.util.Scanner;
public class HelloWorld{
public static void main(String[] args) {
String owner="";
String keys="";
String members="";
for(int i = 0; i < args.length; i++) {
if (i==0)
owner = args[i];
else keys=keys+' '+args[i];
}
Scanner dd = new Scanner(System.in);
String[] ins = new String[7]; //change according to your input
for(int i = 0; i < ins.length; i++) {
ins[i] = dd.nextLine();
}
for(int i=0;i < ins.length; i++) {
String[] word = ins[i].split(" ");
switch(word[0]) {
case "INSERT" :
System.out.println("KEY " + word[3] + " INSERTED BY " + word[2]);
String name=word[2];
String key=word[3];
i++;
if(i<ins.length)
{
word = ins[i].split(" ");
int intIndex = -1;
if(word[0].equals("TURN"))
{
intIndex = keys.indexOf(key);
if(intIndex ==-1)
System.out.println("FAILURE "+name+" UNABLE TO TURN KEY "+ key);
else System.out.println("SUCCESS "+ key + " TURNS KEY "+ name);
}
i++;
if(i<ins.length)
{
word = ins[i].split(" ");
if(word[0].equals("ENTER"))
{
if(intIndex ==-1)
System.out.println("ACCESS DENIED");
else {
System.out.println("ACCESS ALLOWED");
members =members + " " + word[2];
}
}
}
}
break;
case "WHO'S" :
if (members.length() == 0 )
{
System.out.println("NOBODY HOME");
}
else
System.out.println(members);
break;
case "CHANGE" :
if(word[3] == owner){
for(i = 2; i < word.length; i++) {
keys=keys+' '+word[i];
}
System.out.println("OK");
}
else System.out.println("ACCESS DENIED");
break;
case "LEAVE" :
int intIndex = members.indexOf(word[2]);
if(intIndex == -1) System.out.println(word[2] + " NOT HERE");
else{
members.replace(word[2], "");
}
default :
System.out.println("ERROR");
}
}
}
}
SAMPLE INPUT :
INSERT KEY sadaf key
TURN KEY sadaf
ENTER HOUSE sadaf
INSERT KEY that lady
TURN KEY that
ENTER HOUSE that
WHO'S INSIDE?
SAMPLE OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.