Write two programs named Account.java and Problem1.java that work as follows: Th
ID: 3753350 • Letter: W
Question
Write two programs named Account.java and Problem1.java that work as follows:
The Account class is a template class with operations for an online account. TestProblem1.java is provided as a demonstration of expected methods. The Account class has the following:
Two private instance variables: a password (String) and a login status (boolean). The password is used when attempting to log in. The status variable has a value of “true” when the user is logged in, and “false” otherwise.
The constructor receives an initial password.
A “login” method returns nothing (void) and receives nothing. It prompts the user for a password. If
the text entered is equal to the password, the login status is set to “true”, and a message is displayed
indicating a successful login.
A “logout” method returns nothing (void) and receives nothing. It sets the login status to “false” and
prints a message is indicating a successful logout.
A “changePassword” method returns nothing (void) and receives nothing. If the login status is true,
the user is prompted for a new password, and the password is changed. It also prints a message indicating a successful password change. If the login status is false, it prints a message indicating that the user is not logged in.
The Problem1 class is an application class that allows the user to repeatedly perform account operations. It does the following in the main() method:
• Creates an instance of the Account class with any initial password you wish.
• Uses a while loop to repeatedly prompt the user for one of 4 possible commands:
o “quit” terminates the program.
o “login” calls the “login” method from the Account class.
o “change” calls the “changePassword” method from the Account class.
o “logout” calls the “logout” method from the Account class.
o Any other text entered as a command leads to a message indicating the command was not
recognized.
• The commands above are compared using an efficient, nested if-else structure (do not use multiple, simple if statements).
Below is an example of how your program might work and comments that describe what happens. An infinite number of scenarios are possible. You are allowed to use different prompts, messages, and command names if you wish.
Console (user input in bold):
Enter command: delete
Unrecognized command.
Enter command: change
Sorry, you are not logged in.
Enter command: login
Enter password: Yippee
Incorrect password.
Enter command: login
Enter password: Woohoo!
You are logged in!
Enter command: change
Enter new password: YippeePassword changed.
Enter command: logout
You are logged out
. Enter command: login
Enter password: Yippee
You are logged in!
Enter command: quit
Comments about what happens when the initial password is “Woohoo!”:
User quits.
Console (user input in bold):
Enter command: delete
Unrecognized command.
Enter command: change
Sorry, you are not logged in.
Enter command: login
Enter password: Yippee
Incorrect password.
Enter command: login
Enter password: Woohoo!
You are logged in!
Enter command: change
Enter new password: YippeePassword changed.
Enter command: logout
You are logged out
. Enter command: login
Enter password: Yippee
You are logged in!
Enter command: quit
Comments about what happens when the initial password is “Woohoo!”:
User enters an invalid command.
User attempts to change password without having first logged in. User attempts to log in. User enters wrong password.
User attempts to log in again and is successful. User chooses to change password.
User logs out. User logs in using new password.
User quits.
Explanation / Answer
-------------------------------------------
Account.java
import java.util.*;
public class Account {
private String password;
private boolean loginStatus;
public Account(String pwd) {
this.password = pwd;
this.loginStatus = false;
}
public void login() {
System.out.println("Enter password: ");
Scanner sc = new Scanner(System.in);
if(sc.nextLine().equals(this.password)) {
this.loginStatus = true;
System.out.println("You are logged in!");
}
else
System.out.println("Incorrect password.");
}
public void logout() {
this.loginStatus = false;
System.out.println("You are logged out.");
}
public void changePassword() {
if(!this.loginStatus)
System.out.println("Sorry, you are not logged in.");
else {
System.out.println("Enter new password: ");
Scanner sc = new Scanner(System.in);
this.password = sc.nextLine();
System.out.println("Password changed.");
}
}
}
----------------------------------------
Problem1.java
import java.util.*;
import java.io.*;
public class Problem1 {
public static void main(String args[]) {
Account myacc = new Account("abcpqr");
Scanner sc = new Scanner(System.in);
String command;
while(true) {
System.out.println("Enter command: ");
command = sc.nextLine();
if(command.equals("login"))
myacc.login();
else if(command.equals("logout"))
myacc.logout();
else if(command.equals("change"))
myacc.changePassword();
else if(command.equals("quit"))
break;
else
System.out.println("Unrecognized command.");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.