Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 03, 2015, 08:34:27 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Plz help me in writing Java equivalent code for this problem!  (Read 1912 times)

simsim00

  • Participant
  • Joined in 2009
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
This is my C++ code for the problem given below. But since i'm newbie to java, dont know how to write equivalent Java code. If you people can help, plz do so. Helping hands are always praised worthy. Im stuck...Plz assist me..

Code for Login page in C++
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
string readString(ifstream&);
int main(int argc, char *argv[])
{
char user[80];
char pass[80];
string line_in;
string::size_type pos;
ifstream fp_in;
int count = 0;
string user_pass[100][2];
fp_in.open("users.txt");
if(fp_in == NULL)
{
cout << "Could not open user file, exiting." << endl;
return -1;
}
while(!fp_in.eof())
{
line_in=readString(fp_in);
if(line_in.length() > 0)
{
pos = line_in.find("\t",0);
if( pos != string::npos)
{
user_pass[count][0] = line_in.substr(0,pos);
user_pass[count][1] = line_in.substr(pos+1);
count++;
}
}
}
fp_in.close();
if(!count > 0)
{
cout << "Userlist is empty, exiting" << endl;
return -2;
}
int logged_in=0;
while(!logged_in)
{
cout << "Enter user name: ";
cin >> user;
cout << "Enter password: ";
cin >> pass;
int found = 0;
for(int x=0;x<count && !found;x++)
{
if(user_pass
  • [0].compare(user) == 0)

{
found = 1;
if(user_pass
  • [1].compare(pass) == 0)

{
found = 1;
logged_in = 1;
}
else
{
cout << "Invalid password!" << endl;
}
}
}
if(!found)
{
cout << "Invalid username!" << endl;
}
}
cout << "Logged in successfully!" << endl;
return 0;
}
string readString(ifstream& reader)
{
char buffer[256];
reader.getline(buffer,256);
string str1 = buffer;
return str1;
}

Problem:
Writing a maintainable code makes the product with High Quality. Here you are given a scenario where you have to write a maintainable code for the given problem.

Scenario:


Virtual Solutions is an IT solution provider company. It has hired you as a Software Engineer. Your job is to write code, but at the same it should be of High Quality. Your Team Lead has asked you to write a very small code of user login functionality. The code should be of High Quality.

You are required to

a. Write correct code for the user login in Java following the guidelines for writing code
b. Make use of Hungarian Notation for coding purpose

c. Make use of Comments in the Code where required necessary.
« Last Edit: June 13, 2009, 10:41:58 AM by simsim00 »

javafreak

  • Participant
  • Joined in 2009
  • *
  • Posts: 2
    • View Profile
    • Donate to Member
Re: Plz help me in writing Java equivalent code for this problem!
« Reply #1 on: September 21, 2009, 07:24:27 AM »
   import java.util.*;
   import java.io.*;
   public class login
   {
   public static void main(String ar[])
   {   
   try
   {
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
   String s;Integer pass;
   System.out.println("enter the Username");
   s=input.readLine();
   System.out.println("enter the password must be numeric");
   pass=Integer.parseInt(input.readLine());
   if(s.equals("sam") && pass.equals(123) )
   {
   System.out.println("you are logined sucess fully");
   }
   else
   {
   System.out.println("you have entered the wrong");
   }
   }
      catch (Exception e) {
        e.printStackTrace();
      }
   }
   }