PDA

View Full Version : am I doing this right?


cranford07
03-03-2006, 02:05 PM
import apcslib.*;
import chn.util.*;
import java.util.* ;
import java.io.*;


public class Address
{
String peoplename,streetAddress, city, state;

private int zipCode;

FileOutput outFile = new FileOutput("database.txt");
ConsoleIO k = new ConsoleIO();


//-----------------------------------------------------------------
// Sets up this Address object with the specified data.
//-----------------------------------------------------------------
public Address()
{


System.out.print("Profile: ");
String filename = k.readLine();
System.out.print("Name: ");
String name = k.readLine();
System.out.print("Street: ");
String street = k.readLine();
System.out.print("City: ");
String town = k.readLine();
System.out.print("State: ");
String st = k.readLine();
System.out.print("Zip: ");
int zip = k.readInt();
peoplename = name;
streetAddress = street;
city = town;
state = st;
zipCode = zip;

FileOutput outFile = new FileOutput(filename+".txt");

outFile.println(peoplename);
outFile.println(streetAddress);
outFile.println(city);
outFile.println(state);
outFile.println(zipCode);

outFile.close();


}


//-----------------------------------------------------------------
// Returns this Address object as a string.
//-----------------------------------------------------------------


public static void main(String[] args)
{
ConsoleIO k = new ConsoleIO();
String ans2= "";
ArrayList address = new ArrayList();

do
{
System.out.print("1. New Account\n2. Existing Account\n3. View account\n4. Exit\n");
String ans = k.readToken();

if (ans.equals("1"))
{
Address P = new Address();
address.add(P);

}
else if (ans.equals("2"))
{
Address Q = new Address();
Q.update();

}
else if (ans.equals("3"))
{

System.out.print("\nProfile name: ");
String file = k.readLine();

FileInput inFile = new FileInput(file+".txt");

System.out.println("Name: " + inFile.readLine());
System.out.println("Address: " + inFile.readLine());
System.out.println("City: " + inFile.readLine());
System.out.println("State: " + inFile.readLine());
System.out.println("Zip: " + inFile.readLine());



}

System.out.print("Do you want to exit? ");
System.out.println();
ans2 = k.readToken();



}while (ans2.equalsIgnoreCase("n"));




}
}


did I create this in the right way or is there a more effective way. would I just let the user search for their txt profile and print it out so that they can update their information.

Spookster
03-03-2006, 02:41 PM
In your address object you should set up methods for performing the specific tasks that you need it to do. Right now you are running the same code for new and existing addresses and your main class is also performing tasks that your address object should be doing. Your address class should have methods such as updateAddress, viewAddress, createAddress, etc. Your main class should not be doing that type of work. You should think of your main class as being the boss. A boss delegates tasks down to his workers. In this case the worker would be the address object. So all your main class should do is create the address object and then command it to perform the different tasks such as update address or create a new address etc.

cranford07
03-03-2006, 02:45 PM
how would I create it as an objected with multilple people inside it..right now I just have it making txt files to create a new person.