Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/com/phone/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@

import java.util.Scanner;

// amirhossein aghighi | 40231038

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);
phonebook.addContact("mahdi", "09011234567", "Tehran", 29);
phonebook.addContact("amir", "09021234567", "Tabriz", 22);
phonebook.addContact("pouya", "09031234567", "Mashhad", 38);


Scanner reader = new Scanner(System.in);
System.out.println("enter user name:");
String custName = reader.nextLine().strip();
String newContactName = reader.nextLine().strip();
System.out.println("enter user phone:");
String custPhone = reader.nextLine().strip();
String newContactPhone = reader.nextLine().strip();
System.out.println("enter user address:");
String custAddr = reader.nextLine();
String newContactAddr = reader.nextLine();
System.out.println("enter user age:");
int custAge = Integer.parseInt(reader.nextLine().strip());
boolean isAddedCust = phonebook.addContact(custName, custPhone, custAddr, custAge);
int newContactAge = Integer.parseInt(reader.nextLine().strip());
phonebook.addContact(newContactName, newContactPhone, newContactAddr, newContactAge);



System.out.println("added all contacts");



Contact mahdiCont = phonebook.getContact("mahdi");
Contact montezaCont = phonebook.getContact("morteza");
phonebook.getContact("mahdi");
phonebook.getContact("morteza");

int avgAge = phonebook.getAvgAge();
System.out.println("average age of contacts is: " + avgAge);
Expand Down
7 changes: 3 additions & 4 deletions src/com/phone/Phonebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashSet;

public class Phonebook {
private HashMap<String, Contact> contactHashMap;
final private HashMap<String, Contact> contactHashMap;
private int numberOfContacts;

Phonebook(){
Expand All @@ -16,8 +16,7 @@ public class Phonebook {

public boolean addContact(String name, String phoneNumber, String address,int age){
Contact newContact = new Contact(name, phoneNumber, address, age);
ArrayList<Contact> contacts = new ArrayList<>(contactHashMap.values());
if (contacts.contains(newContact) ){
if (contactHashMap.containsValue(newContact)){
return false;
}
contactHashMap.put(name, newContact);
Expand All @@ -41,7 +40,7 @@ public Contact getContact(String name){

public int getAvgAge(){
ArrayList<Contact> contacts = new ArrayList<>(contactHashMap.values());
double sum = 0.00001;
double sum = 0f;
for(Contact el : contacts){
System.out.println(el);
sum += el.getAge();
Expand Down