Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pesudo coding

 
Tee Hussain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i was just messing around stuff and want to learn on how to make a pesudo code from the following coding that has been given below can anyone help ?

(btw am new so if i posted in wrong section sorry)

import java.util.*;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;

public class coursework4
{
private static final int MAX_RECORDS = 20;
private static String lastName[] = new String[MAX_RECORDS];
private static String firstName[] = new String[MAX_RECORDS];
private static String telNumber[] = new String[MAX_RECORDS];
private static String emailAddress[] = new String[MAX_RECORDS];
//private static Scanner data_input = new Scanner(System.in).useDelimiter("\\s*");
private static Scanner data_input = new Scanner(System.in);

//---
private static java.util.HashMap contactsMap = new java.util.HashMap();
private static java.util.Vector contactsVector = new java.util.Vector();
private static String LAST_NAME = "lastName";
private static String FIRST_NAME = "firstName";
private static String TEL_NO = "telNumber";
private static String EMAIL = "emailAddress";
//---

private static void readFile(String file_name) {
Scanner read_in;
Scanner line;
int record_count = 0;
String record;

try {
read_in = new Scanner(new File(file_name));

// read in one line at a time
read_in.useDelimiter(System.getProperty("line.separator"));
while (read_in.hasNext()) {
// Test to see if there are too many records in the file
if (record_count == MAX_RECORDS) {
System.out.println("Only " + MAX_RECORDS +" records allowed in file");
System.exit(0);
}

// read in record
record = new String(read_in.next());

// Split the record up into its fields and store in
// appropriate arrays
line = new Scanner(record);
line.useDelimiter("\\s*,,\\s*");
String lastName = line.next();
String firstName = line.next();
String telNumber = line.next();
String emailAddress = line.next();

contactsMap = new java.util.HashMap();
contactsMap.put(LAST_NAME, lastName);
contactsMap.put(FIRST_NAME, firstName);
contactsMap.put(TEL_NO, telNumber);
contactsMap.put(EMAIL, emailAddress);
System.out.println("\n\nFirst Name :" + firstName);
System.out.println("\nLast Name :" + lastName);
System.out.println("\nTelephone :" + telNumber);
System.out.println("\nEmail :" + emailAddress);
contactsVector.add(contactsMap);

// Increment record count
record_count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

private static void getUserChoice() {
String userChoice = null;
try
{
System.out.print("\n------------WELCOME TO THE CONTACTS APPLICATION---------------");
System.out.print("\n| 1. Search By First Name or Surname |");
System.out.print("\n| 2. List All |");
System.out.print("\n| 3. Insert a record |");
System.out.print("\n| 4. Delete a record, from the list of contacts |");
System.out.print("\n| 5. Quit without saving |");
System.out.print("\n| 6. Save and Quit |");
System.out.print("\n--------------------------------------------------------------\n\n");

userChoice = data_input.next();
System.out.println("userChoice = " + userChoice);
if (userChoice != null && userChoice.equals("1")) {
searchByName();
System.out.print("Search Contact ");
} else if (userChoice.equals("2")) {
System.out.println("List All");
listAll();
} else if (userChoice.equals("3")) {
addContact();
} else if (userChoice.equals("4")) {
deleteRecord();
} else if (userChoice.equals("5")) {

}
else if(userChoice.equals("6"))
{
saveAneExit();
// Get new filename and write out the file
/*String filename;
System.out.print("Enter new New file name: ");
filename = data_input.next();*/
}
} catch (Exception e) {
System.out.println("ERROR OCCURED");
}
}

private static void searchByName()
{
try {
// Get new name for search
System.out.print("Enter First Name or Surname for search : ");
String contactName = data_input.next();
if (contactName != null && contactName.length() < 1) {
System.out.println("Invalid Name");
return;
}
for (int i=0;i<contactsVector.size();i++)
{
contactsMap = (java.util.HashMap) contactsVector.get(i);
if(contactsMap!=null)
{
String lastName = (contactsMap.get(LAST_NAME)!=null)?((String)contactsMap.get(LAST_NAME)):"";
String firstName =(contactsMap.get(FIRST_NAME)!=null)?((String)contactsMap.get(FIRST_NAME)):"";
String telNumber =(contactsMap.get(TEL_NO)!=null)?((String)contactsMap.get(TEL_NO)):"";
String emailAddress = (contactsMap.get(EMAIL)!=null)?((String)contactsMap.get(EMAIL)):"";

if(contactName.equalsIgnoreCase(firstName) || contactName.equalsIgnoreCase(lastName))
{
System.out.println("Contact details Found");
System.out.println("\nFirst Name :"+firstName);
System.out.println("\nLast Name :"+lastName);
System.out.println("\nTelephone :"+telNumber);
System.out.println("\nEmail :"+emailAddress);
break;
}
}
}
//Get User Choice
getUserChoice();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void listAll()
{
try {
System.out.print("List Of Contacts : ");
System.out.println("No\tFirst Name\tLast Name\tTelephone\tEmail");
for (int i=0;i<contactsVector.size();i++)
{
contactsMap = (java.util.HashMap) contactsVector.get(i);
if(contactsMap!=null)
{
String lastName = (contactsMap.get(LAST_NAME)!=null)?((String)contactsMap.get(LAST_NAME)):"";
String firstName =(contactsMap.get(FIRST_NAME)!=null)?((String)contactsMap.get(FIRST_NAME)):"";
String telNumber =(contactsMap.get(TEL_NO)!=null)?((String)contactsMap.get(TEL_NO)):"";
String emailAddress = (contactsMap.get(EMAIL)!=null)?((String)contactsMap.get(EMAIL)):"";
System.out.println(i+"\t"+ firstName+"\t" + lastName+"\t"+ telNumber+"\t"+ emailAddress);
}
}
//Get User Choice
getUserChoice();
}
catch (Exception e)
{
e.printStackTrace();
}
}

private static void addContact()
{
try {
// Get First Name
System.out.print("Enter First Name : ");
String firstName = data_input.next();
if (firstName != null && firstName.length() < 1) {
System.out.println("Invalid First Name");
return;
}
// Get Surname
System.out.print("Enter Surname : ");
String lastName = data_input.next();
if (lastName != null && lastName.length() < 1) {
System.out.println("Invalid Surname");
return;
}
// Get Telephone Number
System.out.print("Enter Telephone No : ");
String telNumber = data_input.next();
if (telNumber != null && telNumber.length() < 1) {
System.out.println("Invalid Telephone No");
return;
}
// GetEmail
System.out.print("Enter Email : ");
String emailAddress = data_input.next();
if (telNumber != null && telNumber.length() < 1) {
System.out.println("Invalid Email");
return;
}

contactsMap = new java.util.HashMap();
contactsMap.put(LAST_NAME, lastName);
contactsMap.put(FIRST_NAME, firstName);
contactsMap.put(TEL_NO, telNumber);
contactsMap.put(EMAIL, emailAddress);
System.out.println("\n\nFirst Name :" + firstName);
System.out.println("\nLast Name :" + lastName);
System.out.println("\nTelephone :" + telNumber);
System.out.println("\nEmail :" + emailAddress);
contactsVector.add(contactsMap);

//Get User Choice
getUserChoice();

} catch (Exception e) {
e.printStackTrace();
}
}
private static void deleteRecord()
{
try {
System.out.print("List Of Contacts : ");
System.out.println("No\tFirst Name\tLast Name\tTelephone\tEmail");
for (int i=0;i<contactsVector.size();i++)
{
contactsMap = (java.util.HashMap) contactsVector.get(i);
if(contactsMap!=null)
{
String lastName = (contactsMap.get(LAST_NAME)!=null)?((String)contactsMap.get(LAST_NAME)):"";
String firstName =(contactsMap.get(FIRST_NAME)!=null)?((String)contactsMap.get(FIRST_NAME)):"";
String telNumber =(contactsMap.get(TEL_NO)!=null)?((String)contactsMap.get(TEL_NO)):"";
String emailAddress = (contactsMap.get(EMAIL)!=null)?((String)contactsMap.get(EMAIL)):"";
System.out.println(i+"\t"+ firstName+"\t" + lastName+"\t"+ telNumber+"\t"+ emailAddress);
}
}
System.out.print("Please Enter the No to Delete : ");
String delNo = data_input.next();
if (delNo != null && delNo.length() < 1) {
System.out.println("Invalid Name");
return;
}
else
{
int no = Integer.parseInt(delNo);
contactsVector.remove(no);
System.out.print("List Of Contacts : ");
System.out.println("No\tFirst Name\tLast Name\tTelephone\tEmail");
for (int i=0;i<contactsVector.size();i++)
{
contactsMap = (java.util.HashMap) contactsVector.get(i);
if(contactsMap!=null)
{
String lastName = (contactsMap.get(LAST_NAME)!=null)?((String)contactsMap.get(LAST_NAME)):"";
String firstName =(contactsMap.get(FIRST_NAME)!=null)?((String)contactsMap.get(FIRST_NAME)):"";
String telNumber =(contactsMap.get(TEL_NO)!=null)?((String)contactsMap.get(TEL_NO)):"";
String emailAddress = (contactsMap.get(EMAIL)!=null)?((String)contactsMap.get(EMAIL)):"";
System.out.println(i+"\t"+ firstName+"\t" + lastName+"\t"+ telNumber+"\t"+ emailAddress);
}
}
}
//Get User Choice
getUserChoice();
}
catch (Exception e)
{
e.printStackTrace();
}
}

private static void saveAneExit()
{
String filename;
PrintStream write_out;
try {
// Get filename and read in file
System.out.print("Enter the masterfile file name: ");
filename = data_input.next();
if (filename != null && filename.length() < 1) {
System.out.println("Invalid File Name");
return;
}
// Create new file
write_out = new PrintStream(new File(filename));

for (int i=0;i<contactsVector.size();i++)
{
contactsMap = (java.util.HashMap) contactsVector.get(i);
if(contactsMap!=null)
{
String lastName = (contactsMap.get(LAST_NAME)!=null)?((String)contactsMap.get(LAST_NAME)):"";
String firstName =(contactsMap.get(FIRST_NAME)!=null)?((String)contactsMap.get(FIRST_NAME)):"";
String telNumber =(contactsMap.get(TEL_NO)!=null)?((String)contactsMap.get(TEL_NO)):"";
String emailAddress = (contactsMap.get(EMAIL)!=null)?((String)contactsMap.get(EMAIL)):"";
// Output a record
write_out.print(lastName);
write_out.print(",,");
write_out.print(firstName);
write_out.print(",,");
write_out.print(telNumber);
write_out.print(",,");
write_out.println(emailAddress);

}
}
// Close file
write_out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
try {
// Declare Variables
int number_of_records;
String filename;
int counter;
String userChoice;

// Get filename and read in file
System.out.print("Enter the masterfile file name: ");
filename = data_input.next();
System.out.println(filename);
if (filename != null && filename.length() > 0) {
readFile(filename);
} else {
System.out.println("Please Enter the File Name");
}
//Get User Choice
getUserChoice();
} catch (Exception e) {
System.out.println("ERROR OCCURED");
}
}
}



(end of coding)
 
Joe Ess
Bartender
Posts: 9300
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Tee.
We love to help people with their problems, but since this is obviously an academic assignment, I'm going to ask that you Do Your Own Homework.
PseudoCode is a way to document what code does without getting into the details of a particular language syntax. If you can, compile and run the program, and write a summary of what it does. Read through the code and simplify the blocks into consise explanations. Your complete Pseudoode listing should capture all the behavior in simple terms.
 
Tee Hussain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thanks for that i just wanted to know how i rite it up but its basically breakin the coding down in plain english thanks agen
 
Henry Wong
author
Marshal
Pie
Posts: 21209
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tee Hussain:
hi thanks for that i just wanted to know how i rite it up but its basically breakin the coding down in plain english thanks agen


I think this is less of "how to write Pseudo code", and more of "how to understand inherited code". Obviously, you inherited the code from somewhere -- probably taken over a project from a colleague perhaps?

Anyway, there is no magically answer. If there is documentation, you start from that. If there isn't, you simply have to break each subsystem down -- figuring out piece by piece.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic