Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet - java

So I had a question open the other day and I thought I was able to get my issue resolved and I understood what was going on but now I am getting a new problem. I am very new to programming and this is for a class I am taking. I am trying to create a contact list that uses inheritance, polymorphism,and collections. I need a contact list that stores two types of contacts: business and personal. I need prompt 1 to add a contact and then ask 1 for personal or 2 for business. Prompt 2 will allow user to display the output of a chosen contact and prompt 3 will quit.
I have the following class and subclasses built. I am pretty sure the classes are built right but after adding either type of contact, when I choose 2 in order to view I am getting Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet. Any help with this would be awesome.
I apologize for the long post but thought I should show everything.
Here is my main class:
package contactlist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ContactList {
/*ArrayList<List.Contact> contactlist;*/
List<Contact> contactlist = new ArrayList<>();
/* Contact contact; */
private int top = 0;
public static void main(String[] args) throws IOException {
/* Contact contact;
contact = new Contact();
List.Contact c;
c = contact; */
ContactList list = new ContactList();
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String choose = "";
while (true) {
System.out.println("\n[1] Add contact");
System.out.println("[2] View all contacts");
System.out.println("[3] Quit");
System.out.print("Choose : ");
try {
choose = keyIn.readLine();
} catch (IOException e) {
System.out.println("Error");
}
switch (choose) {
case "1":
list.addContact();
break;
case "2":
list.viewContacts();
break;
case "3":
System.exit(0);
break;
default:
System.out.println("Error");
break;
}
}
}
public ContactList() {
this.contactlist = new ArrayList<>();
}
public void addContact() throws IOException {
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String firstName;
String lastName;
String address;
String email;
String phone;
String jobTitle;
String organization;
String dateOfBirth;
Scanner input = new Scanner(System.in);
System.out.println("Please enter Specify the contact type (1) Personal
or (2) Business: ");
int contactType = input.nextInt();
if (contactType == 1) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Date of Birth (MM/DD/YYYY): ");
dateOfBirth = keyIn.readLine();
PersonalContact entry;
entry = new PersonalContact(firstName, lastName, address, email,
phone, dateOfBirth);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
} else if (contactType == 2) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Job Title: ");
jobTitle = keyIn.readLine();
System.out.print("Organization: ");
organization = keyIn.readLine();
BusinessContact entry;
entry = new BusinessContact(firstName, lastName, address, email,
phone, jobTitle, organization);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
}
}
public void view() {
for (int index = 0; index < top; index++) {
contactlist.get(index).viewContacts();
}
}
private void viewContacts() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
}
Contact Class:
package contactlist;
public class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;
public Contact(String firstName, String lastName, String address, String
email, String phone){
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.email = email;
this.phone = phone;
}
public String getfirstName() {
return this.firstName;
}
public String setfirstName(){
return (this.firstName = firstName);
}
public String getlastName() {
return this.lastName;
}
public String setlastName(){
return (this.lastName = lastName);
}
public String getAddress() {
return this.address;
}
public String setAddress(){
return (this.address = address);
}
public String getEmail() {
return this.email;
}
public String setEmail(){
return (this.email = email);
}
public String getPhone() {
return this.phone;
}
public String setPhone(){
return (this.phone = phone);
}
String getFirstName() {
return firstName;
}
String getLastName() {
return lastName;
}
public void viewContacts() {
System.out.println(this.getfirstName());
System.out.println(this.getlastName());
System.out.println(this.getAddress());
System.out.println(this.getEmail());
System.out.println(this.getPhone());
}
}
Business Contact subclass:
package contactlist;
public class BusinessContact extends Contact {
private String jobTitle;
private String organization;
public BusinessContact(String firstName, String lastName, String
address, String email, String phone, String jobTitle, String
organization) {
super(firstName, lastName, address, email, phone);
this.jobTitle = jobTitle;
this.organization = organization;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
void write() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
public void viewContacts() {
super.viewContacts();
System.out.println(this.getOrganization());
System.out.println(this.getJobTitle());
}
}
Personal Contact subclass:
package contactlist;
public class PersonalContact extends Contact {
private String dateOfBirth;
public PersonalContact(String firstName, String lastName, String
address, String email, String phone, String dateOfBirth) {
super(firstName, lastName, address, email, phone);
this.dateOfBirth = dateOfBirth;
}
public String getdateOfBirth() {
return dateOfBirth;
}
public void setdateOfBirth(String dateOfBirth){
this.dateOfBirth = dateOfBirth;
}
void write() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
public void viewContacts() {
super.viewContacts();
System.out.println(this.dateOfBirth);
}
}

Your implementation for the viewContacts() method does nothing but throw this exception. If you want a different behavior, you'd have to implement it. E.g., a trivial implementation:
private void viewContacts() {
System.out.println (contactlist);
}

Related

change String in an Arraylist java

I am trying to change a String in an Arraylist but having some trouble being able to access the correct position to change.
So I am tried using students.get(i).getFirstName() = newFirstName; but I was just really just trying different things at this point.
Below is the code I am trying to make changes for firstName and lastName. This is my editStudentId method
public void editStudentID(int findStudentId) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() != findStudentId) {
continue;
}
System.out.println("Found a profile containing information for " + findStudentId + ":");
System.out.println("What would you like to change in your profile?");
System.out.println("1.First Name");
System.out.println("2.Last Name");
int decision = scanner.nextInt();
switch (decision) {
case 1:
System.out.println("Enter a new first name to continue");
String newFirstName = scanner.next();
//so this is the part when I am currently working on but am having some trouble doing so
students.get(i).getFirstName() = newFirstName;
break;
case 2:
System.out.println("Enter a new last name to continue");
break;
}
return;
}
System.out.println(" Id not found ");
}
Here is my class for Students
public class Student {
private final int id;
private final String firstName;
private final String lastName;
private final String dob;
public Student(int id,String firstName, String lastName, String dob) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getDob() {
return dob;
}
public static Student createStudentID(int id,String firstName, String lastName, String dob)
{
return new Student(id,firstName, lastName, dob);
}
}
You should add a set function:
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
Same for the other variables. Setters and Getters are your friends.
Then call these functions. Use get* to use the variable, not change. Use set to change the variable.
students.get(i).setFirstName(newFirstName);

Java calling function with class in argument

We have simple homework. I need to create a user with address in java. But I can't figure out where to write address or how to connect the address to user.
My User
package xxx;
import java.util.List;
import java.util.UUID;
public class User {
private String ID;
private String firstName;
private String lastName;
private int idNumber;
private String email;
private Address officialAddress;
private Address postAddress;
private List contracts;
public User(String firstName, String lastName, int idNumber, String email,Address officialAddress, Address postAddress) {
ID = UUID.randomUUID().toString();
setFirstName(firstName);
setLastName(lastName);
setIdNumber(idNumber);
setEmail(email);
}
public void setFirstName(String firstName) {
if(firstName == null){
throw new IllegalArgumentException("Please fill firstName");
} this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lastName) {
if(lastName == null){
throw new IllegalArgumentException("Please fill lastName");
} this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setIdNumber(int idNumber) {
if(idNumber > 0){
throw new IllegalArgumentException("Please fill idNumber");
} this.idNumber = idNumber;
}
public int getIdNumber(){
return idNumber;
}
public void setEmail(String email) {
if(email == null){
throw new IllegalArgumentException("Please fill email");
} this.email = email;
}
public String getEmail(){
return email;
}
}
My address
package xxx;
public class Address {
private int zipcode;
private String region;
private String streetName;
private int streetNumber;
public Address(int zipcode, String region, String streetName, int streetNumber) {
setZipcode(zipcode);
setRegion(region);
setStreetName(streetName);
setStreetNumber(streetNumber);
}
private void setZipcode(int zipcode) {
if(zipcode > 0){
throw new IllegalArgumentException("Please fill zipcode");
} this.zipcode = zipcode;
}
private int getZipcode(){
return zipcode;
}
private void setRegion(String region) {
if(region == null){
throw new IllegalArgumentException("Please fill region");
} this.region = region;
}
private String getRegion(){
return region;
}
private void setStreetName(String streetName) {
if(streetName == null){
throw new IllegalArgumentException("Please fill region");
} this.streetName = streetName;
}
private String getStreetName(){
return streetName;
}
private void setStreetNumber(int streetNumber) {
if(streetNumber > 0){
throw new IllegalArgumentException("Please fill zipcode");
} this.streetNumber = streetNumber;
}
private int getStreetNumber(){
return streetNumber;
}
}
But how i can create user if there is no address in arguments. But i dont know how to create it with that address argument.(Maybe i need it to put out of arguments and connect somehow different).
And this is my try but i get error that i need to fill zipcode.
private Scanner scannerBuff = new Scanner(System.in);
private String firstName;
private User customer;
private Address officialAddress;
private Address postAddress;
public Engine() {
officialAddress = new Address(484984,"gdfgfd","gdfgdfgdf",4);
postAddress = new Address(484984,"gdfgdf","gdfgdfg",4);
customer = new User("gdfgdf","gdfgdf",123456,"fdsfd",officialAddress,postAddress );
Thanks for any help.
To handle addresses, or no addresses, make multiple constructors
public User(String firstName, String lastName, int idNumber, String email) {
ID = UUID.randomUUID().toString();
setFirstName(firstName);
setLastName(lastName);
setIdNumber(idNumber);
setEmail(email);
}
public User(String firstName, String lastName, int idNumber, String email, Address officialAddress, Address postAddress) {
this(firstName, lastName, idNumber, email); // call the first constructor to avoid duplicate code
setOfficialAdress(officialAddress);
setPostAdress(postAddress);
}
And the setters like the others
public void setOfficialAdress(Address officialAddress) {
if(officialAddress == null){
throw new IllegalArgumentException("Please fill officialAddress");
}
this.officialAddress = officialAddress;
}

is there any way of not using array list in my contact book java

is there any other way instead of using array list? in my assignment we are not allowed to use an array list, array is fine but not an array list at all ....basically it is a contact list book that saves 20 contacts. every contact has to be shown in alphabetical way.
main
public static void main(String[] args) {
List<Contact> lsCont = new ArrayList<Contact>();
Contact[] contacts = new Contact[20];
Scanner scanner = new Scanner(System.in);
String firstName;
String lastName;
String phone;
String email;
int maxContacts = 20;
for (int i = 0; i < contacts.length; i++) {
System.out.println("insert first name : ");
firstName = scanner.nextLine();
System.out.println("insert last Name : ");
lastName = scanner.nextLine();
System.out.println("insert phone : ");
phone = scanner.nextLine();
System.out.println("insert email : ");
email = scanner.nextLine();
Contact cont = new Contact(firstName, lastName, phone, email);
lsCont.add(cont);
Collections.sort(lsCont);
for (Contact contact : lsCont) {
System.out.println(contact.toString());
}
if (i == maxContacts) {
System.out.println("maxiumum number of adding contact has reached");
break;
}
}
}
}
Contact
public class Contact implements Comparable<Contact> {
private String firstName;
private String lastName;
private String phone;
private String email;
public Contact(String firstName, String lastName, String phone, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getPhone() {
return phone;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setPhone(String phone) {
this.phone = phone;
}
#Override
public int compareTo(Contact obj) {
return this.lastName.compareTo(obj.lastName);
}
#Override
public String toString() {
return "Contacts: firstName = " + firstName + ", lastName = " + lastName + ", phone = " + phone + ", email = " + email;
}
}
If you know the size of Contact array then you can just go with array
public static void main(String[] args) {
int maxContacts = 20;
Contact[] contacts = new Contact[maxContacts]; // initialize array of size 20
Scanner scanner = new Scanner(System.in);
String firstName;
String lastName;
String phone;
String email;
//collect 20 contacts into array
for (int i = 0; i < maxContacts; i++) {
System.out.println("insert first name : ");
firstName = scanner.nextLine();
System.out.println("insert last Name : ");
lastName = scanner.nextLine();
System.out.println("insert phone : ");
phone = scanner.nextLine();
System.out.println("insert email : ");
email = scanner.nextLine();
contacts[i] = new Contact(firstName, lastName, phone, email);
}
// Just print contacts array is full
if (contacts.length == maxContacts) {
System.out.println("maxiumum number of adding contact has reached");
}
//Now just sort the array based on name
Arrays.sort(contacts);
//Finally print contacts in order
for (Contact contact : contacts) {
System.out.println(contact.toString());
}
}

Not getting expected output

When I run my program and choose option 2 to get contact details I am expecting it to display as follows (First Name, Last Name):
Contacts how have been entered:
0) John Doe
1) George Smith
2) Nancy Davis
Please enter the number corresponding to the contact you would like to view:
Instead for a personal contact it is displaying as follows:
Contacts who have been entered:
0) Doe 1 F St. (last name, address)
Please enter the number corresponding to the contact you would like to view:
Instead for a business contact it is displaying as follows:
Contacts who have been entered:
0) 1 F St. jd#gmail.com (address, email)
Please enter the number corresponding to the contact you would like to view:
Then when I enter the number to display the contact for personal it is returning me only first name and business is only returning me first and last name. It should be returning the full contact info that was put in during the add contact step. I thought I programmed everything properly but it isn't displaying what I want to see. Any help would be greatly appreciated. My code is listed below.
Main:
package contactlist;
import java.util.ArrayList;
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
int swValue;
Scanner keyIn = new Scanner(System.in);
ArrayList<Contact> ContactRecords = new ArrayList<Contact>();
while (true) {
// Display menu graphics
System.out.println("========================================");
System.out.println("| Address List |");
System.out.println("========================================");
System.out.println("| Options: |");
System.out.println("| 1. Add Contact |");
System.out.println("| 2. Get Contact Details |");
System.out.println("| 3. Exit |");
System.out.println("========================================");
System.out.println(" Select option: ");
swValue = keyIn.nextInt();
switch (swValue) {
case 1:
addContact(ContactRecords);
break;
case 2:
getRecords(ContactRecords);
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid selection");
break;
}
}
}
public static void addContact(ArrayList<Contact> ContactRecords) {
Scanner textIn = new Scanner(System.in);
Scanner keyIn = new Scanner(System.in);
System.out.println("First Name: ");
String firstName = textIn.nextLine();
System.out.println("Last Name: ");
String lastName = textIn.nextLine();
System.out.println("Address: ");
String address = textIn.nextLine();
System.out.println("Email Address: ");
String email = textIn.nextLine();
System.out.println("Phone: ");
String phone = textIn.nextLine();
System.out.println("Is this a 1) Personal or 2) Business?");
int choice = keyIn.nextInt();
if (choice == 1) {
System.out.println("Date of Birth: ");
String dateOfBirth = textIn.nextLine();
Personal aPersonal = new Personal(firstName, lastName, address,
email, phone, dateOfBirth);
ContactRecords.add(aPersonal);
}
if (choice == 2) {
System.out.println("Job Title: ");
String jobTitle = textIn.nextLine();
System.out.println("Organization: ");
String organization = textIn.nextLine();
Business aBusiness = new Business(firstName, lastName, address,
email, phone, jobTitle, organization);
ContactRecords.add(aBusiness);
}
}
public static void getRecords(ArrayList<Contact> ContactRecords)
{
Scanner keyIn = new Scanner(System.in);
System.out.println("Contacts who have been entered:");
for (int i = 0; i < ContactRecords.size(); i++) {
System.out.println(i + ") "+ ContactRecords.get(i).getFirstName() +
" " + ContactRecords.get(i).getLastName();
}
System.out.println("Please enter the number corresponding to the contact
you would like to view: ");
int choice = keyIn.nextInt();
System.out.println(ContactRecords.get(choice).toString());
}
}
Contact
package contactlist;
public abstract class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;
public Contact(String firstName, String lastName, String address, String
email, String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.email = email;
this.phone = phone;
}
public Contact() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#Override
public String toString() {
return toString() ;
}
}
Personal
package contactlist;
public class Personal extends Contact {
private String dateOfBirth;
public Personal(String dateOfBirth, String firstName, String lastName,
String address, String email, String phone) {
super(firstName, lastName, address, email, phone);
this.dateOfBirth = dateOfBirth;
}
public Personal() {
super();
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
#Override
public String toString() {
return dateOfBirth;
}
}
Business
package contactlist;
public class Business extends Contact {
private String jobTitle;
private String organization;
public Business(String jobTitle, String organization, String firstName,
String lastName, String address, String email, String phone) {
super(firstName, lastName, address, email, phone);
this.jobTitle = jobTitle;
this.organization = organization;
}
public Business() {
super();
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
#Override
public String toString() {
return jobTitle + " " + organization;
}
}
This is the line you are using to print the contact:
System.out.println(ContactRecords.get(choice).toString());
This means you are using the toString() method to print the contact. Now, what do you have in a personal contact?
#Override
public String toString() {
return dateOfBirth;
}
This means it is only returning the date of birth, not any of the other fields in the contact.
For the business contact you have:
#Override
public String toString() {
return jobTitle + " " + organization;
}
This means it will only display the content of the fields jobTitle and organization, not any of the other fields.
This, in combination to the answer #femtoRgon gave you, which means that you are also not assigning the fields correctly, gives you the result you have.
You have to:
Change the toString() in Contact to return the common fields. Right now it is a dangerous, infinitely recursive method:
#Override
public String toString() {
return toString() ;
}
Rewrite the toString methods in the personal and business card so that they return a combination of super.toString() - the method you changed in step 1 - along with the additional fields that are specific to Personal or Business.
Change the new calls you make so that you pass the parameters correctly to the constructors.
Here are the Personal constructor call and the Personal constructor definition.
Personal aPersonal = new Personal(firstName, lastName, address,
email, phone, dateOfBirth);
public Personal(String dateOfBirth, String firstName, String lastName,
String address, String email, String phone) {
They order of your arguments doesn't match. So you are passing in a variable called "firstName" as the argument specified as the "dateOfBirth", and "lastName" as the "firstName", "address" as the "lastName", etc. So your call to the ctor should be:
Personal aPersonal = new Personal(dateOfBirth, firstName, lastName, address,
email, phone);
The problem is very much the same with your call to the Business constructor, you pass in "jobTitle" and "organization" as the last arguments, while the class expects them to be the first two arguments.

Displaying contacts from contact list program in java

I am new to programming and object oriented design. This is my last requirement to finish my bachelors degree (not in programming). I am a bit confused with how to make object oriented work, and nothing I look at seems to help. The assignment is to create a contact list that uses inheritance, polymorphism,and collections. I need a contact list that stores two types of contacts: business and personal. I need prompt 1 to add a contact and then ask 1 for personal or 2 for business. Prompt 2 will allow user to display the output of a chosen contact and prompt 3 will quit.
I have the following class and subclasses built. I am pretty sure the classes are built right but after adding either type of contact, when I choose 2 in order to view I can only see the info from the super class and not the subclass (ie: Job Title & Organization or Date of Birth). Any help would be awesome, I just need to get through this, then I will gladly leave programming to those that know what they are doing.
I apologize for the long post but thought I should show everything.
Here is my main class:
package contactlist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ContactList {
/*ArrayList<List.Contact> contactlist;*/
List<Contact> contactlist = new ArrayList<>();
/* Contact contact; */
private int top = 0;
public static void main(String[] args) throws IOException {
/* Contact contact;
contact = new Contact();
List.Contact c;
c = contact; */
ContactList list = new ContactList();
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String choose = "";
while (true) {
System.out.println("\n[1] Add contact");
System.out.println("[2] View all contacts");
System.out.println("[3] Quit");
System.out.print("Choose : ");
try {
choose = keyIn.readLine();
} catch (IOException e) {
System.out.println("Error");
}
switch (choose) {
case "1":
list.addContact();
break;
case "2":
list.viewContacts();
break;
case "3":
System.exit(0);
break;
default:
System.out.println("Error");
break;
}
}
}
public ContactList() {
this.contactlist = new ArrayList<>();
}
public void addContact() throws IOException {
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String firstName;
String lastName;
String address;
String email;
String phone;
String jobTitle;
String organization;
String dateOfBirth;
Scanner input = new Scanner(System.in);
System.out.println("Please enter Specify the contact type (1) Personal
or (2) Business: ");
int contactType = input.nextInt();
if (contactType == 1) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Date of Birth (MM/DD/YYYY): ");
dateOfBirth = keyIn.readLine();
PersonalContact entry;
entry = new PersonalContact(firstName, lastName, address, email,
phone, dateOfBirth);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
} else if (contactType == 2) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Job Title: ");
jobTitle = keyIn.readLine();
System.out.print("Organization: ");
organization = keyIn.readLine();
BusinessContact entry;
entry = new BusinessContact(firstName, lastName, address, email,
phone, jobTitle, organization);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
}
}
public void viewContacts() {
for (int index = 0; index < top; index++) {
System.out.println((index + 1) + " First Name " +
contactlist.get(index).getFirstName());
System.out.println("Last Name " + contactlist.get(index).getLastName());
System.out.println("Address: " + contactlist.get(index).getAddress());
System.out.println("E-mail: " + contactlist.get(index).getEmail());
System.out.println("Phone: " + contactlist.get(index).getPhone());
System.out.println("Job Title " + contactlist.get(index).getJobTitle());
System.out.println("Organization " + contactlist.get(index).
getOrganization());
System.out.println("Date of Birth " + contactlist.get(index).
getDateOfBirth());
}
}
}
Contact Class:
package contactlist;
public class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;
public Contact(String firstName, String lastName, String address, String email, String phone){
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.email = email;
this.phone = phone;
}
public String getfirstName() {
return this.firstName;
}
public String setfirstName(){
return (this.firstName = firstName);
}
public String getlastName() {
return this.lastName;
}
public String setlastName(){
return (this.lastName = lastName);
}
public String getAddress() {
return this.address;
}
public String setAddress(){
return (this.address = address);
}
public String getEmail() {
return this.email;
}
public String setEmail(){
return (this.email = email);
}
public String getPhone() {
return this.phone;
}
public String setPhone(){
return (this.phone = phone);
}
String getFirstName() {
return firstName;
}
String getLastName() {
return lastName;
}
String getJobTitle() {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
String getOrganization() {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
String getDateOfBirth() {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
}
Business Contact Subclass:
package contactlist;
public class BusinessContact extends Contact {
private String jobTitle;
private String organization;
public BusinessContact(String firstName, String lastName, String address,
String email, String phone, String jobTitle, String organization) {
super(firstName, lastName, address, email, phone);
this.jobTitle = jobTitle;
this.organization = organization;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
void write() {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
}
Personal Contact Subclass:
package contactlist;
public class PersonalContact extends Contact {
private String dateOfBirth;
public PersonalContact(String firstName, String lastName, String address,
String email, String phone, String dateOfBirth) {
super(firstName, lastName, address, email, phone);
this.dateOfBirth = dateOfBirth;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
void write() {
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
}
First of all you should not have getters for Organization,job title and dob in the base class since these are the properties of the individual derived classes. What you should do is override the viewContacts methods and first call the super.override and then display the derived class members
package contactlist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ContactList {
/*ArrayList<List.Contact> contactlist;*/
List<Contact> contactlist = new ArrayList<>();
/* Contact contact; */
private int top = 0;
public static void main(String[] args) throws IOException {
/* Contact contact;
contact = new Contact();
List.Contact c;
c = contact; */
ContactList list = new ContactList();
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String choose = "";
while (true) {
System.out.println("\n[1] Add contact");
System.out.println("[2] View all contacts");
System.out.println("[3] Quit");
System.out.print("Choose : ");
try {
choose = keyIn.readLine();
} catch (IOException e) {
System.out.println("Error");
}
switch (choose) {
case "1":
list.addContact();
break;
case "2":
list.viewContacts();
break;
case "3":
System.exit(0);
break;
default:
System.out.println("Error");
break;
}
}
}
public ContactList() {
this.contactlist = new ArrayList<>();
}
public void addContact() throws IOException {
BufferedReader keyIn;
keyIn = new BufferedReader(new InputStreamReader(System.in));
String firstName;
String lastName;
String address;
String email;
String phone;
String jobTitle;
String organization;
String dateOfBirth;
Scanner input = new Scanner(System.in);
System.out.println("Please enter Specify the contact type (1) Personal
or (2) Business: ");
int contactType = input.nextInt();
if (contactType == 1) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Date of Birth (MM/DD/YYYY): ");
dateOfBirth = keyIn.readLine();
PersonalContact entry;
entry = new PersonalContact(firstName, lastName, address, email,
phone, dateOfBirth);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
} else if (contactType == 2) {
System.out.print("First Name: ");
firstName = keyIn.readLine();
System.out.print("Last Name: ");
lastName = keyIn.readLine();
System.out.print("Address: ");
address = keyIn.readLine();
System.out.print("E-mail address: ");
email = keyIn.readLine();
System.out.print("Phone number: ");
phone = keyIn.readLine();
System.out.print("Job Title: ");
jobTitle = keyIn.readLine();
System.out.print("Organization: ");
organization = keyIn.readLine();
BusinessContact entry;
entry = new BusinessContact(firstName, lastName, address, email,
phone, jobTitle, organization);
contactlist.add(entry);
top++;
try {
entry.write();
} catch (Exception e) {
}
}
}
public void view() {
for (int index = 0; index < top; index++) {
contactlist.get(index).viewContact();
}
}
}
CONTACT CLASS:
package contactlist;
public class Contact {
private String firstName;
private String lastName;
private String address;
private String email;
private String phone;
public Contact(String firstName, String lastName, String address, String
email, String phone){
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.email = email;
this.phone = phone;
}
public String getfirstName() {
return this.firstName;
}
public String setfirstName(){
return (this.firstName = firstName);
}
public String getlastName() {
return this.lastName;
}
public String setlastName(){
return (this.lastName = lastName);
}
public String getAddress() {
return this.address;
}
public String setAddress(){
return (this.address = address);
}
public String getEmail() {
return this.email;
}
public String setEmail(){
return (this.email = email);
}
public String getPhone() {
return this.phone;
}
public String setPhone(){
return (this.phone = phone);
}
String getFirstName() {
return firstName;
}
String getLastName() {
return lastName;
}
public void viewContacts() {
System.out.println(this.getfirstName());
System.out.println(this.getlastName());
.....
}
Business Contact Subclass:
package contactlist;
public class BusinessContact extends Contact {
private String jobTitle;
private String organization;
public BusinessContact(String firstName, String lastName, String address,
String email, String phone, String jobTitle, String organization) {
super(firstName, lastName, address, email, phone);
this.jobTitle = jobTitle;
this.organization = organization;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
void write() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
public void viewContacts() {
super.viewContacts();
System.out.println(this.getOrganization());
System.out.println(this.getJobTitle());
}
}
Similarly you can do for the other class do. I was just giving an idea.
List<Contact> contactlist = new ArrayList<>();
It looks like your contactList is in fact the superclass object of type Contact, and with that said you will not be able to access the methods/attributes of the subclass because it doesn't know about them, and I imagine you have tried but it will tell you symbol not found I believe.
Maybe you could make another class that actually contains two collections, one for PersonalContact and BusinessContact, and add a method that will add this to the appropriate list. It might be overkill, but it is the only thing I can think of for this, and you would be satisfying your Collection requirement still. Hope it helps, and gives you a place to start.

Categories