In: Computer Science
Write a C++ program that will use good object-oriented
principles.
You have been tasked to write an application that will allow a user
to change their system password. The XYZ Corporation has the
following rules for passwords:
Once the user has created a new password that passes the above requirements, the user must then retype the password for the program to verify that the same password was entered by the user.
If the user creates a password that does not meet the minimum
requirements, be sure to let the user know the entered password
does not meet the minimum requirements and allow the user to retype
the password.
If the user creates a password that does not match the second entry
of the password (both entries much match), then be sure to let the
user know the password update is unsuccessful.
If the password entered does not meet the minimum requirements, an
error message should be displayed and allow the user to try
again.
If the password entered contains the minimum requirements listed,
then ask the user to retype the password for verification.
If the two entered passwords do not match, an error message should
be displayed and the user should be allowed to retype the
password.
An Example:
Update Your Password Password Requirements: - minimum of 8 characters - minimum of 2 uppercase characters (A - Z) - minimum of 2 lowercase characters (a - z) - minimum of 2 digits (0 - 9) - minimum of 2 special characters: !, @, $, %, &, _ Enter a new password: Pa$$word123 Error: password does not meet minimum requirements, try again: Enter a new password: Pa$$Word123 Password meets minimum requirements. Reenter the new password: Pa$$Word223 Error: update unsuccessful. Entries do not match, try again: Reenter the new password: Pa$$Word123 Password Update successful. Remember to change your password every 90 days. |
The program should have as a minimum:
a class named Password
private member variables:
private member functions (all called from the driver() method):
public member functions:
!!!NOTE: any program submission that does
not use a class and object and/or does not use appropriate methods
will result in a grade submission of 0.
Use of global variables will also result in a grade submission of
0.
#include <iostream>// needed for cin and cout
#include <string.h>
using namespace std;
class password {
private:
char passwrd[20],repass[20];
int min_upper,min_lower,min_digit,min_special;
int valid,valid2;
void setPassword1();
void validateRequirements();
void setPassword2();
void validateMatch();
void display();
public:
void driver();
password();
};
void password::driver()
{
setPassword1();
validateRequirements();
setPassword2();
validateMatch();
display();
}
password::password()
{
valid=0;
valid2=0;
min_upper=min_lower=min_digit=min_special=0;
}
void password::setPassword1()
{
cout<<"Update Your Password"<<endl;
cout<<"Password Requirements:"<<endl;
cout<<"- minimum of 8 characters"<<endl;
cout<<"- minimum of 2 uppercase characters (A - Z)"<<endl;
cout<<"- minimum of 2 lowercase characters (a - z)"<<endl;
cout<<"- minimum of 2 digits (0 - 9)"<<endl;
cout<<"- minimum of 2 special characters: !, @, $, %, &, _"<<endl;
cout<<"Enter a new password:";
cin>>passwrd;
}
void password::validateRequirements()
{
int invalid=0;
while(1)
{
int l=strlen(passwrd);
//cout<<endl<<"lenght"<<l<<passwrd<<endl;
for(int i=0;i<l;i++)
{
char c=passwrd[i];
if(c>='a' && c<='z') //count lowercase characters (a - z)
min_lower++;
else if(c>='A' && c<='Z') //count uppercase characters (A - Z)
min_upper++;
else if(c>='0' && c<='9') //count digits
min_digit++;
else if(c=='!' || c=='@'||c=='$'||c=='%'||c=='&'||c=='_') //count special chars
min_special++;
else
{
invalid++; //counts invalid special characers +,=,(,)etc
cout<<"invalid special character";
}
}
//if all requirement true, set valid=1
if(l>=8 && min_special>=2&&min_digit>=2&&min_upper>=2&&min_lower>=2&&invalid==0)
valid=1;
else
valid=0;
//cout<<valid<<"????";
if(valid==0)
{
cout<<"Error: password does not meet minimum requirements, try again:";
cin>>passwrd;
min_upper=min_lower=min_digit=min_special=0;
}
else
{
// cout<<"length"<<min_lower<<min_upper<<min_digit<<min_special<<endl;
// cout<<"ok"<<endl;
break;
}
}
}
void password::setPassword2()
{
if(valid) //if first password valid, reenter the Password
{
cout<<"Password meets minimum requirements."<<endl;
cout<<"Reenter the new password:";
cin>>repass;
}
}
void password::validateMatch()
{
while(1)
{
if(strcmp(passwrd,repass)!=0) //if both password are different,unsuccess
{
cout<<" Error: update unsuccessful. Entries do not match, try again:"<<endl;
cout<<"Reenter the new password:";
cin>>repass;
valid2=0;
}
else
{
valid2=1;
break;
}
}
}
void password::display()
{
if(valid2==1 && valid==1) //if both passwords match, success
{
cout<<"Password Update successful."<<endl;
cout<<"Remember to change your password every 90 days.";
}
}
int main()
{
password pw;
pw.driver();
return 0;
}