CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Customer Info

 



kskim02
New User

Aug 13, 2012, 6:13 AM

Post #1 of 6 (1185 views)
Customer Info Can't Post

I'm trying to create a program that allows a user to create customer information that is written to a separate text file. I want to assign a unique customer ID to each customer so that I can modify or delete a customer using their ID when necessary. Here is what I have so far, any help would be greatly appreciated:


#This program allows you to add, change, or delete customer information

open (MYFILE, '>>test.txt');
$Test = 'test.txt';
open(INDB, $Test) or die "the database could not be found \n";

$count = 0;
while (1){

print "\nAdd(a),Change(c),Delete(d), or Quit(q): ";
$input = <STDIN>;

if($input=~/a/){

print "\nInput First Name: ";
$FirstName = <STDIN>;
chomp($FirstName);

print "\nInput Last Name: ";
$LastName = <STDIN>;
chomp($LastName);

print "\nInput Social Security Number: ";
$SSNO = <STDIN>;
chomp($SSNO);

print "\nInput Address 1: ";
$Add1 = <STDIN>;
chomp($Add1);

print "\nInput Address 2: ";
$Add2 = <STDIN>;
chomp($Add2);

print "\nInput City: ";
$City = <STDIN>;
chomp($City);

print "\nInput Zipcode: ";
$Zip = <STDIN>;
chomp($Zip);

print "\nInput State: ";
$State = <STDIN>;
chomp($State);


@info = ($FirstName,$LastName,$SSNO,$Add1,$Add2,$City,$Zip,$State,$count);

print MYFILE "$LastName, $FirstName, $SSNO, $Add1, $Add2, $City, $Zip, $State, $count\n";

}

if($input=~/c/) {

print "\nWhat member # would you like to change?: ";
$Memberchange = <STDIN>;
chomp($Memberchange);

seek(MYFILE, 0, 0);
if ($Memberchange = $count){

print "ABCD";}
}

if($input=~/d/) {

print "\nWhat member # would you like to delete?: ";
$Memberdelete = <STDIN>;
chomp($Memberdelete);
}

if($input=~/q/){
exit 0;
}

$count++;
}
close (MYFILE);
exit 1;


FishMonger
Veteran / Moderator

Aug 13, 2012, 8:05 AM

Post #2 of 6 (1178 views)
Re: [kskim02] Customer Info [In reply to] Can't Post

Write a subroutine for each action (List, Add, Modify, Delete, Quit).

Write another subroutine that displays the menu of actions.

Use a dispatch table to organize and launch the chosen option/action.


Laurent_R
Veteran / Moderator

Aug 13, 2012, 10:52 AM

Post #3 of 6 (1167 views)
Re: [FishMonger] Customer Info [In reply to] Can't Post

It is probably a bad idea to delete a customer. Add a field with a termination date or an active Y/N flag.

For the customer ID, just pick up the latest (orlargest) existing ID and add 1. You'll have to do more, though, if there is a possibility of concurrent accesses to you database (i.e. two processes accessing to it at the same time).


itsbaskar
Novice

Aug 13, 2012, 1:29 PM

Post #4 of 6 (1161 views)
Re: [kskim02] Customer Info [In reply to] Can't Post

Use switch statement, so code will look better.

Example:


Code
use Switch; 

$input;
print "\n Enter the input:";
chomp($input=<STDIN>);

switch ($input){
case("a") { print "Adding user"; }
case("c") { print "Changing user"; }
case("d") { print "Deleting user";}
case("q") { exit 0;}
else { print "Enter correct input"; }
}



itsbaskar
Novice

Aug 13, 2012, 1:32 PM

Post #5 of 6 (1160 views)
Re: [itsbaskar] Customer Info [In reply to] Can't Post

Replace the print statement with the subroutines names by calling them for each case.


FishMonger
Veteran / Moderator

Aug 13, 2012, 2:45 PM

Post #6 of 6 (1155 views)
Re: [itsbaskar] Customer Info [In reply to] Can't Post

The Switch module is a source filter and is known to be buggy, which is why it was removed from the core as of v5.13.1 and is no longer being recommended by the majority of the perl community.

A dispatch table would be the best choice or if you're using a newer perl (v5.10 or above), the second choice would be to use given/when which is Perl's built-in switch feature.
http://perldoc.perl.org/perlsyn.html#Switch-Statements

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives