In: Computer Science
Must be written in JAVA Code
Modify the program you created in previous project to accomplish the following:
Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-'
Store the data in an ArrayList object
Program to Modify
import java.util.ArrayList;
import java.util.Scanner;
public class Address
{
String firstname;
String lastname;
int zipcode;
Address(String firstname,String lastname,int zipcode)
{
this.firstname = firstname;
this.lastname = lastname;
this.zipcode = zipcode;
}
public String toString()
{
return this.firstname + " " + this.lastname + " " +
this.zipcode;
}
public static void main(String[] args)
{
Scanner aa = new Scanner(System.in);
int n;
System.out.print("Enter a number up to 25: ");
n = aa.nextInt();
String firstname,lastname;
int zipcode;
ArrayList<Address> addr = new
ArrayList<Address>();
aa.nextLine();
for(int i=1;i<=n;i++)
{
System.out.print("Enter First Name:");
firstname = aa.nextLine();
System.out.print("Enter Last Name:");
lastname = aa.nextLine();
System.out.print("Enter Zip Code:");
zipcode = aa.nextInt();
aa.nextLine();
Address ad = new Address(firstname,lastname,zipcode);
addr.add(ad);
}
System.out.println("\nFirstName/LastName/Zipcode");
System.out.println();
for(Address a:addr)
{
System.out.println(a);
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Address {
String firstname;
String lastname;
int zipcode;
// added variables
String address;
String city;
String state;
long phone;
// changed constructor according to new
variables
public Address(String aFirstname, String aLastname,
int aZipcode, String aAddress, String aCity, String aState,
long aPhone)
{
super();
firstname = aFirstname;
lastname = aLastname;
zipcode = aZipcode;
address = aAddress;
city = aCity;
state = aState;
phone = aPhone;
}
// added toString to return new fields
public String toString() {
return this.firstname + " " +
this.lastname + " " + this.zipcode + " " + this.address + " " +
this.state + " "
+ this.city + " " + this.phone;
}
public static void main(String[] args) {
Scanner aa = new
Scanner(System.in);
int n;
System.out.print("Enter a number up
to 25: ");
n = aa.nextInt();
String firstname, lastname, add, c,
s;
int zipcode;
long ph;
ArrayList<Address> addr = new
ArrayList<Address>();
aa.nextLine();
for (int i = 1; i <= n; i++)
{
System.out.print("Enter First Name:");
firstname =
aa.nextLine();
System.out.print("Enter Last Name:");
lastname =
aa.nextLine();
System.out.print("Enter Zip Code:");
zipcode =
aa.nextInt();
aa.nextLine();
System.out.println("Enter address: ");
add =
aa.nextLine();
System.out.println("Enter city: ");
c =
aa.nextLine();
System.out.println("Enter state: ");
s =
aa.nextLine();
System.out.println("Enter phone: ");
ph =
aa.nextLong();
aa.nextLine();
Address ad = new
Address(firstname, lastname, zipcode, add, c, s, ph);
addr.add(ad);
}
System.out.println("\nFirstName/LastName/Zipcode/Address/City/State/Phone");
System.out.println();
for (Address a : addr) {
System.out.println(a);
}
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me