diff --git a/src/com/phone/Contact.java b/src/com/phone/Contact.java index 3b81cde..047cc48 100644 --- a/src/com/phone/Contact.java +++ b/src/com/phone/Contact.java @@ -1,51 +1,51 @@ -package com.phone; - -import java.util.Objects; - -public class Contact { - private final String name, phoneNumber, address; - private final int age; - - - public Contact(String name, String phoneNumber, String address, int age) { - this.name = name; - this.phoneNumber = phoneNumber; - this.address = address; - this.age = age; - } - - public String getName() { - return name; - } - - public String getPhoneNumber() { - return phoneNumber; - } - - public String getAddress() { - return address; - } - - public int getAge(){ - return age; - } - - - @Override - public String toString(){ - return "name: " + getName() + " | address: " + getAddress() + " | phone: " + getPhoneNumber() + ""; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Contact contact = (Contact) o; - return Objects.equals(name, contact.name) && Objects.equals(phoneNumber, contact.phoneNumber) && Objects.equals(address, contact.address); - } - - @Override - public int hashCode() { - return Objects.hash(name, phoneNumber, address); - } -} +package com.phone; + +import java.util.Objects; + +public class Contact { + private final String name, phoneNumber, address; + private final int age; + + + public Contact(String name, String phoneNumber, String address, int age) { + this.name = name; + this.phoneNumber = phoneNumber; + this.address = address; + this.age = age; + } + + public String getName() { + return name; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public String getAddress() { + return address; + } + + public int getAge(){ + return age; + } + + + @Override + public String toString(){ + return "name: " + getName() + " | address: " + getAddress() + " | phone: " + getPhoneNumber() + ""; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Contact contact = (Contact) o; + return Objects.equals(name, contact.name) && Objects.equals(phoneNumber, contact.phoneNumber) && Objects.equals(address, contact.address); + } + + @Override + public int hashCode() { + return Objects.hash(name, phoneNumber, address); + } +} diff --git a/src/com/phone/Main.java b/src/com/phone/Main.java index d445bb8..d51c69c 100644 --- a/src/com/phone/Main.java +++ b/src/com/phone/Main.java @@ -1,39 +1,40 @@ -package com.phone; - -import java.util.Scanner; - -public class Main { - public static void main(String[] args) { - Phonebook phonebook = new Phonebook(); - boolean isAddedMahdi = phonebook.addContact("mahdi", "09011234567", "Tehran", 29); - boolean isAddedAmir = phonebook.addContact("amir", "09021234567", "Tabriz", 22); - boolean isAddedPouya = phonebook.addContact("pouya", "09031234567", "Mashhad", 38); - - - Scanner reader = new Scanner(System.in); - System.out.println("enter user name:"); - String custName = reader.nextLine().strip(); - System.out.println("enter user phone:"); - String custPhone = reader.nextLine().strip(); - System.out.println("enter user address:"); - String custAddr = reader.nextLine(); - System.out.println("enter user age:"); - int custAge = Integer.parseInt(reader.nextLine().strip()); - boolean isAddedCust = phonebook.addContact(custName, custPhone, custAddr, custAge); - - - - System.out.println("added all contacts"); - - - - Contact mahdiCont = phonebook.getContact("mahdi"); - Contact montezaCont = phonebook.getContact("morteza"); - - int avgAge = phonebook.getAvgAge(); - System.out.println("average age of contacts is: " + avgAge); - - - phonebook.printContacts(); - } -} +<<<<<<< HEAD +package com.phone; + +// this is bobs edit +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Phonebook phonebook = new Phonebook(); + boolean isAddedMahdi = phonebook.addContact("mahdi", "09011234567", "Tehran", 29); + boolean isAddedAmir = phonebook.addContact("amir", "09021234567", "Tabriz", 22); + boolean isAddedPouya = phonebook.addContact("pouya", "09031234567", "Mashhad", 38); + + + Scanner reader = new Scanner(System.in); + System.out.println("enter user name:"); + String custName = reader.nextLine().strip(); + System.out.println("enter user phone:"); + String custPhone = reader.nextLine().strip(); + System.out.println("enter user address:"); + String custAddr = reader.nextLine(); + System.out.println("enter user age:"); + int custAge = Integer.parseInt(reader.nextLine().strip()); + boolean isAddedCust = phonebook.addContact(custName, custPhone, custAddr, custAge); + + + System.out.println("added all contacts"); + + + + Contact mahdiCont = phonebook.getContact("mahdi"); + Contact montezaCont = phonebook.getContact("morteza"); + + int avgAge = phonebook.getAvgAge(); + System.out.println("average age of contacts is: " + avgAge); + + + phonebook.printContacts(); + } +} diff --git a/src/com/phone/Phonebook.java b/src/com/phone/Phonebook.java index f229034..8a909e1 100644 --- a/src/com/phone/Phonebook.java +++ b/src/com/phone/Phonebook.java @@ -1,51 +1,51 @@ -package com.phone; - -import java.sql.Array; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; - -public class Phonebook { - private HashMap contactHashMap; - private int numberOfContacts; - - Phonebook(){ - contactHashMap = new HashMap<>(); - numberOfContacts = 0; - } - - public boolean addContact(String name, String phoneNumber, String address,int age){ - Contact newContact = new Contact(name, phoneNumber, address, age); - ArrayList contacts = new ArrayList<>(contactHashMap.values()); - if (contacts.contains(newContact) ){ - return false; - } - contactHashMap.put(name, newContact); - numberOfContacts++; - return true; - } - - public void printContacts(){ - ArrayList contacts = new ArrayList<>(contactHashMap.values()); - System.out.println("\nprinting contacts:"); - for(Contact el : contacts){ - - System.out.println(el); - System.out.print("--------------------------------------------------------------\n"); - } - return; - } - public Contact getContact(String name){ - return contactHashMap.get(name); - } - - public int getAvgAge(){ - ArrayList contacts = new ArrayList<>(contactHashMap.values()); - double sum = 0.00001; - for(Contact el : contacts){ - System.out.println(el); - sum += el.getAge(); - } - return (int) Math.round(sum / contacts.size()); - } -} +package com.phone; + +import java.sql.Array; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class Phonebook { + private HashMap contactHashMap; + private int numberOfContacts; + + Phonebook(){ + contactHashMap = new HashMap<>(); + numberOfContacts = 0; + } + + public boolean addContact(String name, String phoneNumber, String address,int age){ + Contact newContact = new Contact(name, phoneNumber, address, age); + ArrayList contacts = new ArrayList<>(contactHashMap.values()); + if (contacts.contains(newContact) ){ + return false; + } + contactHashMap.put(name, newContact); + numberOfContacts++; + return true; + } + + public void printContacts(){ + ArrayList contacts = new ArrayList<>(contactHashMap.values()); + System.out.println("\nprinting contacts:"); + for(Contact el : contacts){ + + System.out.println(el); + System.out.print("--------------------------------------------------------------\n"); + } + return; + } + public Contact getContact(String name){ + return contactHashMap.get(name); + } + + public int getAvgAge(){ + ArrayList contacts = new ArrayList<>(contactHashMap.values()); + double sum = 0.00001; + for(Contact el : contacts){ + System.out.println(el); + sum += el.getAge(); + } + return (int) Math.round(sum / contacts.size()); + } +}