You are very good!
However, I am not so good, so I am walking through your code and then
integrating the previous information about creating an array with object
(Identification, or whatever) as it's type. What you did there (in the first
response) was to use a class to create the multiple elements of different
types, then create the array with the class as its reference. So I am now
seeing that you have some data test elements inside Main and class Data.
I am getting these entries by line by line requests of the user (non-GUI)
like:
"enter first name: "
"enter last name: "
"enter job: "
"enter amount: "
"etc..."
Then I am outputting the results, OK let me include my code- from what you
see below I am now wanting to use an array to store the input from the user
and then still print our the results, but from the array
[code below]
import java.util.*; // program uses class Scanner
// Class creation to setup place holders for the required data elements for
retrieval in my Public class later
class PersonData { // This class to gather all person information for my app
(outside of Main).
PersonData(String newFname, String newLname, float newDonationNum,
float newDonationAmount,
String newAddress, String newCity, String newState, String
newZip) {
fname = newFname; // constructor invokations
lname = newLname; // ..
address = newAddress; // ..
city = newCity; // ..
state = newState; // ..
zip = newZip; // ..
donationNum = newDonationNum; // ..
donationAmount = newDonationAmount; // ..
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZip() { // String rather than float, I guess, since US
zips can contain '-'?
return zip;
}
public float getYearlyDonations() {
return donationNum * donationAmount;
}
private String fname;
private String lname;
private String address;
private String city;
private String state;
private String zip; // wasn't sure whether to use string or float for
Zip??
private float donationNum, donationAmount;
} // end of PersonData class
public class AddBook // Modified to include requirement of positive num
entry for donations (amount of and number of)
{
// main method begins execution of java application
public static void main( String args[] )
{
ArrayList persons = new ArrayList(); // Array to handle multiple
entries of persons
while ( persons.size() <5 ) // limit to 5 iterations
{
// create scanner to obtain input from command window
Scanner input = new Scanner ( System.in );
System.out.println(); // a blank line
System.out.print( "Please enter a person's First Name: " ); // First
prompt. Get the First Name of the person
String Fname = input.nextLine(); // read persons First name -
initialize Fname variable
System.out.print( "Please enter a person's Last Name: " ); // Get
Last Name
String Lname = input.nextLine(); // read persons Last Name -
initialize Lname variable
// Initialize our variables not already initialized
String Address; // Address string
String City; // City String
String State; // State string
String Zip; // Zip (String seemed the best fit for this type)
float donationNum; // first number, number of donations for the year
float donationAmount; // second number, amount per donation
//end initialization of variables
// Section requesting user input-------------ends @ line 157
System.out.print ( "Please enter the person's Address: " );
Address = input.nextLine(); // Address validation and exception catch
needed
System.out.print ( "Please enter the person's City: " );
City = input.nextLine(); // City validation needed
System.out.print ( "Please enter the person's State: " );
State = input.nextLine(); // Validation for State needed
System.out.print ( "Zip Code in five number format please: " );
Zip = input.nextLine(); // Get zip - and validate
// Input request for number of donations. Includes validation for a
positive integer.
System.out.print( "Please enter number of donations: " ); // prompt for
donation number
donationNum = input.nextFloat();
while (donationNum <= 0) // validate for positive number
{
System.out.print( "The number of donations must be a positive
value. " +
"Please enter the number of donations again: " ); // prompt
for positive value for donations
donationNum = input.nextFloat(); // prompt for another try for
valid input
}
// Input reuest that includes validation for positive integer.
Validation for a numeral needed later
System.out.print( "Please enter amount per donation: $" ); // prompt for
donation amount
donationAmount = input.nextFloat();
while (donationAmount <= 0) // validate for positive amount
{
System.out.print( "The donation amount must be a positive value.
" +
"Please enter the donation amount again: " ); // Prompt
again after invalid entry
donationAmount = input.nextFloat(); // prompt for another try
for valid input
}
// End User Data Request section
// display User Input data
PersonData person; // Person data from our array
// Output of our data gathered from above. Blank lines and comma
included for readability of output.
person = new PersonData(Fname, Lname, donationNum, donationAmount,
Address, City, State, Zip); // fields included in our person entries
//yearlyDonations = donationNum * donationAmount; // multiply
System.out.printf("\n"); // Print a blank line between user entry
and results for readability
System.out.print( person.getFname() ); // display person's First
Name
System.out.printf(" "); // Get the First and Last names on same line
with a space
System.out.print( person.getLname() ); //display person's Last Name
System.out.println(); // Print Lname
System.out.print( person.getAddress() ); // get the address input
System.out.println(); // blank line for readability
System.out.print( person.getCity() ); // get the city input
System.out.printf(", "); // Comma, Space on same line as City, State
and Zip for readability
System.out.print( person.getState() ); // get the state input
System.out.printf(" "); // Same line for City, State and Zip
System.out.print( person.getZip() ); // get the zip code input
System.out.println(); // blank line for readability
System.out.print( person.getFname() ); // get the name again for the
statement of donations total
System.out.printf( "'s yearly donations is: $%,.2f\n",
person.getYearlyDonations() ); // display yearly donations total
persons.add(person); // Add another until 5 iterations
} // end While and termination of loop @ 5 iterations only
} // end Main
} // end class AddBook
As you see, the code prints out each block of information, formatted for
readability, and iterates x 5, then terminates. I am wanting to consolidate
the storage by using an array rather than the method above and still print
the results. What I have works well (except for validations, of course, but
I can do that later). I want to get the array method down so I can alter
this to use it instead.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en