0

Hi,
I've attached the code I have that deals with allowing a user to have access to the main menu. When adding a new user, the username and password is saved to a text file. However, I want to add a default username and password so when a user runs the program for the first time, he/she can have access to the main menu which allows him to add his username/password to the accounts text file. the other option is...if there are no usernames or passwords available(the accounts text file has not been made) an option of adding a new user will show up in the login menu only that one time.

any help will be greatly appreciated...thanks

Attachments
/*=====================================================================
Program: CAS Doctor.Account 
Author: Oluwatomisin Bin Chiek Odediran
Date: March 03, 2009
Teacher: Kamran Bayegan
School: United Nations International School, New York, United States of America
Computer Used: Pentium M 1.73 GHz running under Windows XP
IDE Used: Eclipse 3.4.1
=====================================================================*/

/**
*Purpose: Class that contains Attributes for each Account
*/


public class Account 
{
	String UserName;
	String Password;
	
	
	public Account(String u, String p)
	{
		setUserName(u);
		Password = p;
		
	}
	
	/**
	*Sets the Length of the Username to 40 characters
	*@param UserName 
	*/
	public void setUserName(String u)
	{
		UserName = u;
		if(UserName.length() > 40)
		{
			UserName = UserName.substring(0, 40);
		}
	}
	/**
	*Sets the Password
	*@param Password 
	*/	
	public void setPassword(String p)
	{
		Password = p;
	}
	

	/**
	*Gets UserName
	*@return UserName
	*/
	public String getUserName()
	{
		return UserName;
	}
	
	/**
	*Gets Password
	*@return Password
	*/
	public String getPassword()
	{
		return Password;
	}

}
import java.io.*;

/*=====================================================================
Program: CAS Doctor.Account 
Author: Oluwatomisin Bin Chiek Odediran
Date: March 03, 2009
Teacher: Kamran Bayegan
School: United Nations International School, New York, United States of America
Computer Used: Pentium M 1.73 GHz running under Windows XP
IDE Used: Eclipse 3.4.1
=====================================================================*/

/**
*Purpose: Saves New Account Details and Checks User Information
*/

public class AccountManager 
{
	static final String File_Name = "Accounts.txt";  //Text file where the username and password information is kept

	/**
	* 
	* Creates a New Account and saves Username and Password Details
	*/

	public static void saveNewAccount(Account a) 
	{
		//Saves the Information into file and catch IOException
		try 
		{
			
			FileWriter file = new FileWriter(File_Name, true);
			PrintWriter writer = new PrintWriter(file);
			
			writer.println(a.getUserName()); //Saves UserName on one Line in the Text File
			writer.println(a.getPassword());//Saves UserName on The Following Line in The Text File

			writer.close();
		} catch (IOException e) 
		{
			IBIO.output("There are no Accounts.");
		}
	}
	
	/**
	*Checks text file to see if A UserName Already exists if user is adding a new username
	*@return usernameExists boolean value that states whether or not username already exists 
	*/	
	public static boolean usernameExists(String username)
	{
		String fileUsername;
		
		try 
		{
			FileReader file = new FileReader(File_Name);
			BufferedReader reader = new BufferedReader(file);
			
			//Runs a loop to check whether the username entered already exists in the file
			while ((fileUsername = reader.readLine()) != null) 
			{
				
				if (fileUsername.equals(username))//username found in file
						return true;
			}
			reader.close();
		} 
		catch (IOException e) 
		{
			IBIO.output("There are no Accounts.");
		}
		return false; //returns false if username does not already exist
	}

	/**
	*Deals with check whether or not the username and password entered is on the text file
	*@return a the username and password that exist on file
	*/	
	public static Account login(String username, String password) 
	{
		String fileUsername;
		String filePassword;
		
		fileUsername = username;
		filePassword = password;

		if(username== "casdoctor" && password=="casdoctor")
		{
			fileUsername = "casdoctor";
			filePassword= "casdoctor";
			Account a = new Account(username, password);
			return a;
		}
		
		else{
		try 
		{
			FileReader file = new FileReader(File_Name);
			BufferedReader reader = new BufferedReader(file);

			//
			while ((fileUsername = reader.readLine()) != null) 
			{
				filePassword = reader.readLine();
				if (fileUsername.equals(username) && filePassword.equals(password)) 
				{
					Account a = new Account(username, password);
					return a;
				}
				
			}
			reader.close();
		} 
		catch (IOException e) 
		{
			IBIO.output("There are no Accounts.");
			
		}
		return null;
	}
	}
}
public class IBIO {

	// ===================================================
	// IBIO simple input and output methods
	// ===================================================
	static void output(String info) {
	    System.out.println(info);
	}

	static void output(char info) {
	    System.out.println(info);
	}

	static void output(byte info) {
	    System.out.println(info);
	}

	static void output(int info) {
	    System.out.println(info);
	}

	static void output(long info) {
	    System.out.println(info);
	}

	static void output(double info) {
	    System.out.println(info);
	}

	static void output(boolean info) {
	    System.out.println(info);
	}

	static String input(String prompt) {
	    String inputLine = "";
	    System.out.print(prompt);
	    try {
	        inputLine = (new java.io.BufferedReader(
	            new java.io.InputStreamReader(System.in))).readLine();
	    } catch (Exception e) {
	        String err = e.toString();
	        System.out.println(err);
	        inputLine = "";
	    }
	    return inputLine;
	}

	static String inputString(String prompt) {
	    return input(prompt);
	}

	static String input() {
	    return input("");
	}

	static int inputInt() {
	    return inputInt("");
	}

	static double inputDouble() {
	    return inputDouble("");
	}

	static char inputChar(String prompt) {
	    char result = (char) 0;
	    try {
	        result = input(prompt).charAt(0);
	    } catch (Exception e) {
	        result = (char) 0;
	    }
	    return result;
	}

	static byte inputByte(String prompt) {
	    byte result = 0;
	    try {
	        result = Byte.valueOf(input(prompt).trim()).byteValue();
	    } catch (Exception e) {
	        result = 0;
	    }
	    return result;
	}

	static int inputInt(String prompt) {
	    int result = 0;
	    try {
	        result = Integer.valueOf(input(prompt).trim()).intValue();
	    } catch (Exception e) {
	        result = 0;
	    }
	    return result;
	}

	static long inputLong(String prompt) {
	    long result = 0;
	    try {
	        result = Long.valueOf(input(prompt).trim()).longValue();
	    } catch (Exception e) {
	        result = 0;
	    }
	    return result;
	}

	static double inputDouble(String prompt) {
	    double result = 0;
	    try {
	        result = Double.valueOf(input(prompt).trim()).doubleValue();
	    } catch (Exception e) {
	        result = 0;
	    }
	    return result;
	}

	static boolean inputBoolean(String prompt) {
	    boolean result = false;
	    try {
	        result = Boolean.valueOf(input(prompt).trim()).booleanValue();
	    } catch (Exception e) {
	        result = false;
	    }
	    return result;
	}
	// =========== end IBIO =======================================


}
/*=====================================================================
Program: CAS Doctor.Main
Author: Oluwatomisin Bin Chiek Odediran
Date: March 03, 2009
Teacher: Kamran Bayegan
School: United Nations International School, New York, United States of America
Computer Used: Pentium M 1.73 GHz running under Windows XP
IDE Used: Eclipse 3.4.1
=====================================================================*/
/**
*Purpose: Displays Menus & Calls Numerous Methods to Carry Out the Programs Functions
*Dealing with both the Student Information and Student Activities
*/
public class Main {

	private Student_Activity_Manager SAManager;

	public static void main(String[] args) {
		Main main = new Main();
		main.initializeCollections(); // Opens Student and Activity Files and
		// Reads them from disk
		main.showLogInScreen(); // Calls The Main Menu Method that displays Log In
		// Screen
	}

	
	/**
	* Shows Log In Menu and Allows Valid Users to Have Access To The Program
	* 
	*/
	private void showLogInScreen() {
		while (true) {
			// Log In Screen That Will Be Shown To User When Program is First
			// Launched
			IBIO.output("*************************************************");
			IBIO.output("                   CAS DOCTOR					  ");
			IBIO.output("*************************************************");
			IBIO.output("                   (1) Log In					  ");
			IBIO.output("                   (2) Quit					  ");
			int operation = IBIO.inputInt("Enter Option: ");
			IBIO.output("");

			switch (operation) {
			case 1: // If User Enters 1 
				Account a = null;
				
				while (a == null ) { // until a is not null, the statements below
					// will continue keep on being displayed
					String UserName = IBIO
							.inputString("Enter your User Name: ");
					String Password = IBIO.inputString("Enter your Password: ");

					// checks to see if user name and password are valid (match
					// the user name and password in the accounts text file)
					a = AccountManager.login(UserName, Password);

					// If UserName and Password are Invalid/ Don't Match, The
					// Message Below IS Displayed
					
					 if (a == null) {
						IBIO.output("The username or password is invalid.");
						IBIO
								.output("Please Try Again or Refer To The User Manual For Help");
						IBIO.output("");

					}

				}
				MainMenu();// When Correct Username & Password Has Been
				// Entered, User Will be Directed to Next
				// Menu

				break;
			case 2: //If User Enters 2
				System.exit(0); //Program will exit
				break;

			default: //If User Enters Anything Other than 1 or 2
				IBIO.output("You Entered an Invalid Option: " + operation
						+ ". Type 1 or 2 or 3"); //Error Message Is Displayed
				break;
			}
		}

	}

	//reads all the student and activity files from disk
	private void initializeCollections() {
		SAManager = new Student_Activity_Manager();
		SAManager.readStudents(11); //reads Grade 11 Students' file
		SAManager.readStudents(12); // reads Grade 12 Students' file
		SAManager.readStudentsActivities(11); // reads Grade 11 Students' Activities file
		SAManager.readStudentsActivities(12);// reads Grade 12 Students' Activities file
	}

	/**
	*Shows Main Menu and Depending on the Option entered leads to different parts of the program
	*
	*/

	private void MainMenu() {
		char confirm;
		while (true) {
			//Main Menu Options Are Displayed
			IBIO.output("----------------------------------------------------");
			IBIO.output("                  Main Menu");
			IBIO.output("----------------------------------------------------");
			IBIO.output("1. Edit Grade 11 Students & Activities");
			IBIO.output("2. Edit Grade 12 Students & Activities");
			IBIO.output("3. Move Grade 11 Records to Grade 12");
			IBIO.output("4. Delete Grade 12 Records");
			IBIO.output("5. Add New User To CAS Doctor");
			IBIO.output("6. Exit ");

			int MainMenuOption = 0;
			MainMenuOption = IBIO
					.inputInt("Enter ONE of the Menu Options and Press the 'Enter' Key: ");

			switch (MainMenuOption) {
			case 1: //If User Enters 1
				editGrade(11); //calls method editGrade, editing the records and activities of Students in Grade 11
				break;
			case 2: //If User Enters 2
				editGrade(12); //calls method editGrade, editing the records and activities of Students in Grade 12
				break;
			case 3: //If USer Enters 3
				IBIO
				.output("Note That If You Do This, All Grade 12 Records Will Be Deleted Permanently And These Records Will Remain Grade 12 Records Until Deleted.");
				confirm = IBIO
						.inputChar("Are You Sure You Want To Move These Records (Y/N) "); //Verification
				if (confirm == 'Y' || confirm == 'y') { //If User Enters Y or y
					SAManager.moveGrade11to12(); // Grade 11 Records Will Become Grade 12 Records Permanently
				
					//If User Enters N or n, They will be returned to the main menu
				} else if (confirm == 'N' || confirm == 'n') {
					break; 
					
					//If User Enters anything else, the message below will be shown and they will view the main menu again.		
				} else {
					IBIO
							.output("You Entered An Invalid Option.  You Will Be Returned To The Main Menu");
					break;
				}
				
				
			case 4: //If User Enters 4
				IBIO
						.output("Note That If You Continue, There Is No Way To Retrieve The Records After Deletion");
				confirm = IBIO
						.inputChar("Are You Sure You Want To Delete These Records (Y/N): "); //Verification
				if (confirm == 'Y' || confirm == 'y') {
					SAManager.deleteGrade12(); //If Y or y is entered, Grade 12 Records will be Deleted Permanently
					
					//If User Enters N or n, They will be returned to the main menu
				} else if (confirm == 'N' || confirm == 'n') {
					break;
					
					//If User Enters anything else, the message below will be shown and they will view the main menu again.		
				} else {
					IBIO
							.output("You Entered An Invalid Option.  You Will Be Returned To The Main Menu");
					break;
				}
			case 5: // if user enters 5

				boolean usernameExists = true;
				Account a;
				String username = "";
				
				// Deals with adding a new user to CAS Doctor
				while (usernameExists) {
					username = IBIO
							.inputString("Enter your Desired Username: ");

					usernameExists = AccountManager.usernameExists(username);

					if (usernameExists) { //If the user name that the user wants to enter already exists
						//the message below is displayed
						IBIO.output("Sorry, the username already exists.");
						IBIO.output("Please try again.");
					}
				}
				// If user name does not already exist, 
				String password = IBIO.inputString("Enter a Password: ");
				a = new Account(username, password);
				AccountManager.saveNewAccount(a); //New User Name and password is saved 

			case 6: // If user enters 6
				System.exit(0); //Program Exits
				break;
			default: // if user does not enter either 1-6, the message below is shown and the Main Menu Options are Displayed Again
				
				IBIO.output("You entered an invalid option: " + MainMenuOption
						+ ". Type 1 or 2 or 3 or 4 or 5 or 6");
				break;
			}
		}
	}

	/**
	*Shows Grade Menu that deals with Managing Student Information
	*@param grade
	*	the grade that is being edited (11 or 12)
	*/
	private void editGrade(int grade) {
		while (true) {
			//Display Menu Options
			IBIO
					.output("-------------------------------------------------------------");
			IBIO.output("                         Grade Menu.");
			IBIO
					.output("-------------------------------------------------------------");
			IBIO.output("1. Show All Students Sorted By First Name");
			IBIO.output("2. Show All Students Sorted By Last Name");
			IBIO.output("3. Show All Students Sorted By ID Number");
			IBIO.output("4. Add New Student");
			IBIO
					.output("5. Edit Existing Student (Enter 1/2/3 To View ID Number)");
			IBIO
					.output("6. Delete Existing Student (Enter 1/2/3 To View ID Number)");
			IBIO.output("7. Manage CAS Hours ");
			IBIO.output("8. Back to Main Menu");
			IBIO.output("9. Exit ");

			int editGradeMenu = 0;
			editGradeMenu = IBIO
					.inputInt("Choose One of the Menu Options and Press the 'Enter' Key: ");
			switch (editGradeMenu) {
			//depending on the number added by the user,
			case 1: 
				SAManager.showAllStudentsSortedByFirstName(grade);
				break;
			case 2:
				SAManager.showAllStudentsSortedByLastName(grade);
				break;
			case 3:
				SAManager.showAllStudentsSortedByIdNumber(grade);
				break;
			case 4:
				addNewStudent(grade);
				break;
			case 5:
				editSelectedStudent(grade, selectStudent(grade));
				break;
			case 6:
				IBIO
						.output("Once You Enter A Student's ID Number and Press 'Enter', You Will No Longer Be Able to Retrieve His/Her Information.");
				deleteSelectedStudent(grade, selectStudent(grade));
				break;
			case 7:
				CASHours(grade);
				break;
			case 8:
				return;
			case 9:
				System.exit(0);
				break;
			default:
				IBIO.output("You Entered an Invalid Option: " + editGradeMenu
						+ ". Type 1 or 2 or 3 or 4 or 5 or 6 or 7");
				break;
			}
		}

	}
	/**
	*Selects a student in a grade and returns idNumber of that student
	*@param grade
	*		the grade of the student
	*@return idNumber
	*		student's ID number	
	*	
	*/
	private Long selectStudent(int grade) {
		while (true) {
			Long idNumber = IBIO.inputLong("Enter Student's ID Number: ");
			if (SAManager.getGrade(grade).get(idNumber) != null) {
				return idNumber; //If IdNumber entered  
			}
			System.out.println("Student with id: " + idNumber
					+ " does not exist. Try again.");
		}
	}

	/**
	*Shows Grade Menu that deals with Managing Student Information
	*@param grade
	*		the grade of the student
	*@param student
	*		id number of student
	*	
	*@return activityIdNumber
	*		activity ID number
	*/		
	private Long selectActivity(int grade, long student) {
		LinkedList list = SAManager.getGrade(grade).get(student)
				.getActivities();
		while (true)
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by BestJewSinceJC
0

No one is going to sort through all of that just to give you an explanation that is quite simple anyway. One thing you could do is check to see how many usernames your program currently has, and if it's 0, then display a screen allowing the user to create a username and password. However, a better thing to do (depending on what your project guidelines are) would be to have a menu that allows the user to choose between signing in on an existing account or creating a new account.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.