1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Plz help in writing java equivalent code for this c++ code. A real challege.

Discussion in 'Java' started by simsim00, Jun 12, 2009.

  1. simsim00

    simsim00 New Member

    This is the 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.

    Code for Login page in C++
    Code:
    #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[x][0].compare(user) == 0) 
    { 
    found = 1;
    if(user_pass[x][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 edited by a moderator: Jun 12, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    The question is based on the invalid premise that rewriting the code in some other language will make it "maintainable". Maintainable code is about how you write it, using clear formatting, sensible variable names, plenty of commenting and above all use of simple algorithms with complications ONLY where necessary (and extra-well commented when that happens) so it's clear what's going on.

    Using those rules you can make maintainable code in C, assembler, even Brainf*ck, and by ignoring those rules you can make unmaintainable code in Java, Cobol, ADA and so on. There is no such thing as a language that enforces maintainability.

    Now if they just want you to rewrite it in Java that's fine, but they shouldn't be spouting nonsense that this is somehow about maintainability.

    Anyway, this looks like coursework/project, so we won't do it for you but we will help. How far have you got and where are you stuck? If you've been on a Java course then you already know how to write Java code and it's then just a case of using the C code as a detailed spec for the Java code that you will write.
     
  3. simsim00

    simsim00 New Member

    Now plz tell me is this code correct according to the given problem. Plz help me in bringing high quality to this code. I.E Hungarian Notation and use of comments.

    HTML:
    <html>
    <head>
    <title>Khalid Mahmood   </title>
    </head>
    
    <body>
    <br>
     <p> <h3><center>Please enter your user name and
     password</center></h3></p>
    
     <br>
     <br>
     <form  action="process2.jsp "    method = "post" >
     <center>username</center>
     <center><input type = "text" name=
     "username"></center>
     <center>password</center>
     <center><input type = "password" name =
     "password"></center>
     <center><input type="submit"     name="Submit"
     value="Login"></center>
    
    
     </form>
    
     </body>
     </html>
    
    HTML:
    <%@ page import="java.util.*" %>
    <jsp:useBean id="idHandler" class="foo.Login" scope="request">
    <jsp:setProperty name="idHandler" property="*"/>
    </jsp:useBean>
    
    <% 
       if (idHandler.validate()) {
    %>
    <jsp:forward page="success.jsp"/>
    <%
       }  else {
    %>
    <jsp:forward page="retry.jsp"/>
    <%
       }
    %>
    
    Code:
    package foo;
     import java.sql.*;
    
     public class Login {
    
       private String username = "";
     private String password = "";
    
       public Login() {
      }
    
      public void setUsername(String username) {
         this.username = username;
      }
    
      public void setPassword(String password) {
         this.password = password;
       }
    
    
     public boolean  authenticate(String username2,
     String password2) {
      String query="select * from Registration;";
       String DbUserName="";
       String DbPassword="";
       String finalUser="";
      try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection
     con=DriverManager.getConnection("jdbc:odbc:register");
       Statement stat=con.createStatement();
       ResultSet rst=stat.executeQuery(query);
       while(rst.next())
    
      {
       DbUserName=rst.getString("UserName");
    
      DbPassword=rst.getString("password");
    
       if (username2.equals(DbUserName) &&
     password2.equals(DbPassword)) {
    
     break;
         }
    
    
       }
     return true;
     }catch(Exception e){
    
     e.printStackTrace();
     return false;
     }
    }}
     
    Last edited by a moderator: Jun 14, 2009
  4. xpi0t0s

    xpi0t0s Mentor

    Use code blocks when posting code please; this retains the formatting.

    So are you saying that you don't know what Hungarian notation is, and that you don't know where to put comments and how to write them?
     

Share This Page