简体   繁体   English

使用Java和面向对象创建联系人列表

[英]Creating a contact list with java and object oriented

I am new to programming and object oriented design. 我是编程和面向对象设计的新手。 This is my last requirement to finish my bachelors degree (not in programming). 这是我完成本科学历的最后要求(不是编程专业)。 I am so 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 is stores two types of contacts: business and personal. 我需要一个存储两种联系人类型的联系人列表:公司联系人和个人联系人。 1. Prompt to select which contact to add or display. 1.提示选择要添加或显示的联系人。 2. Prompt to allow user to enter the contact info. 2.提示允许用户输入联系信息。 3. Prompt that will display the output of a chosen contact back. 3.提示,将显示所选联系人的输出。

I have the following class and subclasses. 我有以下类别和子类别。 I am stuck on how I am supposed to read in the inputs to the specific arraylists. 我被困在应该如何读入特定arraylists的输入中。 I don't even know if the classes are built right either. 我什至都不知道这些类是否构建正确。 Any help would be awesome, I just need to get through this, then I will gladly leave programing to those that know what they are doing. 任何帮助都是很棒的,我只需要解决这个问题,然后我将很乐意将编程留给那些知道他们在做什么的人。

This is what I have for my Parent Class: 这是我的家长班的内容:

package ooo1;

public abstract class Contact {

    private String contactId;
    private String firstName;
    private String lastName;
    private String address;
    private String phoneNumber;
    private String emailAddress;

    public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
    {
        this.contactId = contactId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.emailAddress = emailAddress;
    }
    public void setContactId(String input){
        this.contactId = input;
    }
    public String getContactId(){
        return contactId;
    }

    public void setFirstName(String input){
        this.firstName = input;
    }
    public String getFirstName(){
        return firstName;
    }

    public void setLastName(String input){
        this.lastName = input;
    }
    public String getLastName(){
        return lastName;
    }

    public void setAddress(String input){
        this.address = input;
    }
    public String getAddress(){
        return address;
    }

    public void setPhoneNumber(String input){
        this.phoneNumber = input;
    }
    public String getPhoneNumber(){
        return phoneNumber;
    }

    public void setEmailAddress(String input){
        this.emailAddress = input;
    }
    public String getEmailAddress(){
        return emailAddress;
    }

    void displayContact(){
        System.out.println("Contact ID:" + contactId + " First Name:" + firstName + " Last Name:" + lastName);
        System.out.println("Address:" + address);
        System.out.println("Phone Number:" + phoneNumber);
        System.out.println("Email Address:" + emailAddress);
    }

}

This is one of my subclasses: 这是我的子类之一:

package ooo1;

public class PersonalContact extends Contact {

    private String dateofBirth;

    public PersonalContact(String contactId, String firstName, String lastName, String address, String phoneNumber, String emailAddress, String dateofBirth){

        super(contactId, firstName, lastName, address, phoneNumber, emailAddress);

        this.dateofBirth = dateofBirth;
    }
    public void setDateofBirth(String input){
        this.dateofBirth=input;
    }
    public String getDateofBirth(){
        return this.dateofBirth;
    }

}

This is my other subclass: 这是我的另一个子类:

package ooo1;

public class BusinessContact extends Contact {

    private String jobTitle;
    private String organization;

    public BusinessContact(String contactId, String firstName, String lastName, String address, String phoneNumber, String emailAddress, String jobTitle, String organization){

        super(contactId, firstName, lastName, address, phoneNumber, emailAddress);

        this.jobTitle = jobTitle;
        this.organization = organization;
    }
    public void setJobTitle(String input){
        this.jobTitle = input;
    }
    public String getJobTitle(){
        return this.jobTitle;
    }

    public void setOrganization(String input){
        this.organization = input;
    }
    public String getOrganization(){
        return this.organization;
    }

}

This is what I have for Main which is so wrong at this point I believe: 这是我对Main所拥有的东西,目前我认为这是错误的:

package ooo1;

import java.util.ArrayList;
import java.util.Scanner;

public class ContactList {

    public static void main(String[] args) {

        ArrayList<PersonalContact> personalList = new ArrayList<PersonalContact>();

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter ContactId : ");
        String contactId = input.nextLine();
        System.out.println("Please enter First Name : ");
        String firstName = input.nextLine();
        System.out.println("Please enter Last Name : ");
        String lastName = input.nextLine();
        System.out.println("Please enter Address : ");
        String address = input.nextLine();
        System.out.println("Please enter Phone Number : ");
        String phoneNumber = input.nextLine();
        System.out.println("Please enter Email Address : ");
        String emailAddress = input.nextLine();
        System.out.println("Please enter Birthday: ");
        String dateofBirth = input.nextLine(); 

    }

}

You have the pieces in place you just need to put them together. 您已经将各个部分放在一起,只需要将它们放在一起即可。 First, think about the List being used. 首先,考虑正在使用的List It is supposed to hold both types of contacts, however its type argument is using the derived type PersonalContact . 它应该包含两种类型的联系人,但是其类型参数使用派生的类型PersonalContact Instead use the base class Contact 而是使用基类Contact

 List<Contact> contacts = new ArrayList<Contact>();

Next it appears you start to gather the information about the contact from the end user via the console. 接下来,您似乎开始通过控制台从最终用户那里收集有关联系人的信息。 Before doing this you may want to ask what type of contact is being entered, maybe give the option to enter 1 for a personal contact or 2 for a business contact. 在执行此操作之前,您可能要询问输入的是哪种类型的联系人,或者可以选择为个人联系人输入1或为业务联系人输入2。 Then collect the information specific to the type of contact they chose. 然后收集特定于他们选择的联系人类型的信息。

Scanner input = new Scanner(System.in);
System.out.println("Please enter Specify the contact type (1 Personal, 2 Business) : ");
int contactType = input.nextInt();

//collect data common to both types
if(contactType == 1){
   //collect information specific to personal
} else if(contactType ==2){
   //collect information specific to business
}

Next you need to turn the data you collected into the actual objects, using a the constructors. 接下来,您需要使用构造函数将收集到的数据转换为实际对象。 This will need to be done conditionally and could actually be a part of the last section if done properly. 这将需要有条件地完成,并且如果正确完成的话,它实际上可能是最后一部分的一部分。

   Contact contact;
   if(contactType == 1){
       contact = new PersonalContact(/*arguments here*/);
   } else{
       contact = new BusinessContact(/*arguments here*/);
   }

Then finish up by adding the contact to your list: 然后将联系人添加到您的列表中以完成此操作:

   contacts.add(contact);
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprac;

/**
 *
 * @author Arijit
 */
public class Contact {
    private String name;
    private int number;

    public Contact(int number,String name) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public int getNumber() {
        return number;
    }

    public static Contact createContact(int number, String name){
        return new Contact(number,name);
    }



}

This is the Contact class, which is governed by a contact name and a contact number. 这是Contact类,由联系人名称和联系人号码控制。 Next we will design the MobilePhone class which will have a number(optional) and an arraylist of contacts. 接下来,我们将设计MobilePhone类,该类将具有一个数字(可选)和一个联系人数组列表。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprac;

import java.util.ArrayList;

/**
 *
 * @author Arijit
 */
class MobilePhone1 {
 public int mynumber;
 public ArrayList < Contact > contacts;

 public MobilePhone1(int mynumber) {
  this.mynumber = mynumber;
  this.contacts = new ArrayList < Contact > ();
 }

 public Boolean addContact(Contact contact) {
  if (findPosition(contact) >= 0) {
   System.out.println("Contact already is phone");
   return false;
  } else {
   contacts.add(contact);
  }

  return true;
 }

 public ArrayList < Contact > getContacts() {
  return contacts;
 }

 public void updateContact(Contact oldContact, Contact newContact) {
  if (findPosition(oldContact) >= 0) {
   contacts.set(findPosition(oldContact), newContact);
  } else {
   System.out.println("Contact does not exist");
  }
 }

 public void removeContact(Contact contact) {
  if (findPosition(contact) >= 0) {
   contacts.remove(findPosition(contact));
  } else {
   System.out.println("No contact");
  }
 }

 public int searchContact(Contact contact) {
  int position = findPosition(contact);
  if (contacts.contains(contact)) {
   System.out.println("Item found at position");
   return position;
  }
  System.out.println("Not found");
  return -1;
 }



 private int findPosition(Contact contact) {
  return this.contacts.indexOf(contact);

 }

 private int findPosition(String name) {
  for (int i = 0; i < contacts.size(); i++) {
   Contact contact = this.contacts.get(i);
   if (contact.getName().equals(name)) {
    return i;
   }
  }
  return -1;
 }


}

public class MobilePhone {
 public static void main(String args[]) {
  Boolean quit = false;
  int choice = 0;
  java.util.Scanner sc = new java.util.Scanner(System.in);
  MobilePhone1 phone = new MobilePhone1(98312);

  //Contact newcontact = Contact.createContact(12345,"Arijt");

  while (!quit) {
   System.out.println("Enter your choice");
   choice = sc.nextInt();
   sc.nextLine();


   switch (choice) {
    case 0:
     System.out.println("Enter the number of Contacts");
     int count = sc.nextInt();
     for (int i = 0; i < count; i++) {
      System.out.println("Enter number");
      int phoneNumber = sc.nextInt();
      System.out.println("Enter Name");
      sc.nextLine();
      String name = sc.nextLine();
      Contact newcontact = Contact.createContact(phoneNumber, name);
      phone.addContact(newcontact);
     }
     break;

    case 1:
     int size = phone.getContacts().size();
     System.out.println(size);
     for (int i = size - 1; i >= 0; i--) {
      phone.removeContact(phone.getContacts().get(i));
     }
     System.out.println(phone.getContacts().isEmpty());
     break;

    case 2:

     for (int i = 0; i < phone.getContacts().size(); i++) {
      System.out.println(phone.searchContact(phone.getContacts().get(i)));
     }
     break;

    case 3:

     //Contact newcontact1 = Contact.createContact(12345,"Buzz");
     System.out.println("Enter the Contact name you want to update");
     String oldContactName = sc.nextLine();

     for (int j = 0; j < phone.getContacts().size(); j++) {
      if (phone.getContacts().get(j).getName().equals(oldContactName)) {
       System.out.println("Enter the new Contact name");
       String newName = sc.nextLine();
       System.out.println("Enter the new Contact number");
       int newNumber = sc.nextInt();
       phone.updateContact(phone.getContacts().get(j), Contact.createContact(newNumber, newName));
      } else {
       System.out.println("You are looking for the wrong contact");
      }

     }


     for (int i = 0; i < phone.getContacts().size(); i++) {
      System.out.println(phone.getContacts().get(i).getName() + "," + phone.getContacts().get(i).getNumber());
     }
     break;

    case 4:
     if(phone.getContacts().isEmpty()){
         System.out.println("Emtpty contact list");

     }
     else {
         System.out.println("Contact list");
     for (int i = 0; i < phone.getContacts().size(); i++) {
      System.out.println("Name: "+phone.getContacts().get(i).getName() + ",Phone Number: " + phone.getContacts().get(i).getNumber());
     }
     }
     break;

    case 6:
        System.out.println("Enter 0 for adding contact\n");
        System.out.println("Enter 1 for removing every contact\n");
        System.out.println("Enter 2 for searching contact\n");
        System.out.println("Enter 3 for updating contact\n");
        System.out.println("Enter 4 for viewing the contact list\n");
        System.out.println("Enter 6 for exiting\n");
        System.out.println("Enter 5 to see the instrusctions again\n");
        break;
    case 5:
     quit = true;
     break;



   }
  }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM