Revise one of the game that you have created so far, so that the user gets a men
ID: 3915219 • Letter: R
Question
Revise one of the game that you have created so far, so that the user gets a menu like the following: Press A or B to start the game: A. Sign in B. Create a new account Based on the user’s choice you must either provide him/her a way of getting his/her username and password or letting him/her to create a new one. Use a text editor to create a comm-delimited file of a username and a password in each line. Use this file to check the credential or append new ones. Make sure that usernames are unique, and not case sensitive. However, passwords are case sensitive and must be more than 8 characters long. Use Exceptions and handle them elegantly to program with style. Revise one of the game that you have created so far, so that the user gets a menu like the following: Press A or B to start the game: A. Sign in B. Create a new account Based on the user’s choice you must either provide him/her a way of getting his/her username and password or letting him/her to create a new one. Use a text editor to create a comm-delimited file of a username and a password in each line. Use this file to check the credential or append new ones. Make sure that usernames are unique, and not case sensitive. However, passwords are case sensitive and must be more than 8 characters long. Use Exceptions and handle them elegantly to program with style. Revise one of the game that you have created so far, so that the user gets a menu like the following: Press A or B to start the game: A. Sign in B. Create a new account Based on the user’s choice you must either provide him/her a way of getting his/her username and password or letting him/her to create a new one. Use a text editor to create a comm-delimited file of a username and a password in each line. Use this file to check the credential or append new ones. Make sure that usernames are unique, and not case sensitive. However, passwords are case sensitive and must be more than 8 characters long. Use Exceptions and handle them elegantly to program with style.Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class PopUpWindow extends Frame{
File file = new File("D:/Execution.txt");
Label l1,l2;
TextField tf1,tf2,tf3;
Button b1,b2,b3;
PopUpWindow(){
setTitle("Press A or B to start the game:");
setSize(400,400);
FlowLayout fl = new FlowLayout();
setLayout(fl);
l1=new Label("Enter User Name:");
l2=new Label("Enter Password:");
tf1=new TextField(20);
tf2=new TextField(20);
b1=new Button("Sign in");
b2=new Button("Create a new account");
b3=new Button("Exit:");
add(l1);add(l2);add(tf1);add(tf2);add(b1);add(b2);add(b3);
b1.addActionListener(new ActionInner());
b2.addActionListener(new ActionInner());
b3.addActionListener(new ActionInner());
setVisible(true);
}
class ActionInner implements ActionListener{
public void actionPerformed(ActionEvent ae){
boolean isNewUser = false;
String cocatOfUserAndPwd1 = new String();
if(ae.getSource() == b1){
try{
String userName = tf1.getText();
String passWord = tf2.getText();
if(passWord.length() >= 8){
String cocatOfUserAndPwd = userName+passWord;
cocatOfUserAndPwd1=cocatOfUserAndPwd;
if(file.exists() && file.length() == 0){
isNewUser = true;
}else{
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
while(line != null){
if(line.equalsIgnoreCase(cocatOfUserAndPwd)){
isNewUser = false;
System.out.println("You are logged in:");
System.exit(1);
break;
}else{
isNewUser = true;
}
line = reader.readLine();
}
}
if(isNewUser){
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(cocatOfUserAndPwd);
}
}else{
System.out.println("Password must be 8 characters long");
}
}catch (FileNotFoundException ex) {
System.out.println("File Not exists:");
}
catch(IOException ioe){
System.out.println("Unable to create file in write mode:");
}
}else if(ae.getSource() == b2){
try{
if(!isNewUser){
String userName = tf1.getText();
String passWord = tf2.getText();
cocatOfUserAndPwd1=userName+passWord;
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(cocatOfUserAndPwd1);
}else{
System.out.println("User already exists:");
}
}catch (FileNotFoundException ex) {
System.out.println("File Not exists:");
}
catch(IOException ioe){
System.out.println("Unable to create file in write mode:");
}
}else if(ae.getSource() == b3){
System.exit(1);
}
}
}
}
//Execuction logic class
public class Execution{
public static void main(String args[]){
PopUpWindow w1 = new PopUpWindow();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.