Skip to content

Commit 47b9913

Browse files
committed
Refresh
1 parent 6acffb3 commit 47b9913

11 files changed

Lines changed: 712 additions & 380 deletions

File tree

ArrayList/.idea/workspace.xml

Lines changed: 452 additions & 183 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
269 Bytes
Binary file not shown.
-1.59 KB
Binary file not shown.
-2.63 KB
Binary file not shown.
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
package com.mausv;
22

33
/**
4-
* Created by Mauricio on 10/16/2015.
4+
* Created by mausv on 7/27/2016.
55
*/
66
public class Contact {
77
private String name;
8-
private String phone;
8+
private String phoneNumber;
99

10-
public Contact(String name, String phone) {
10+
public Contact(String name, String phoneNumber) {
1111
this.name = name;
12-
this.phone = phone;
12+
this.phoneNumber = phoneNumber;
1313
}
1414

1515
public String getName() {
16-
return this.name;
16+
return name;
1717
}
1818

19-
public String getPhone() {
20-
return phone;
19+
public String getPhoneNumber() {
20+
return phoneNumber;
2121
}
2222

23-
public void setName(String name) {
24-
this.name = name;
25-
}
26-
27-
public void setPhone(String phone) {
28-
this.phone = phone;
23+
public static Contact createContact(String name, String phoneNumber) {
24+
return new Contact(name, phoneNumber);
2925
}
3026
}

ArrayList/src/com/mausv/Main.java

Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,132 @@
22

33
import java.util.Scanner;
44

5+
/**
6+
* Created by mausv on 7/27/2016.
7+
*/
58
public class Main {
6-
7-
public static Scanner scanner = new Scanner(System.in);
8-
9-
public static MobilePhone myPhone = new MobilePhone();
9+
private static Scanner scanner = new Scanner(System.in);
10+
private static MobilePhone mobilePhone = new MobilePhone("0039 330 4404");
1011

1112
public static void main(String[] args) {
12-
13-
initializePhone();
14-
15-
}
16-
17-
public static void initializePhone() {
18-
printMenu();
19-
System.out.println("Choose an option: ");
20-
21-
boolean flag = true;
22-
23-
while(flag) {
24-
int choice = scanner.nextInt();
13+
boolean quit = false;
14+
startPhone();
15+
printActions();
16+
while(!quit) {
17+
System.out.println("\nEnter action: (6 to show available actions)");
18+
int action = scanner.nextInt();
2519
scanner.nextLine();
26-
switch (choice) {
20+
21+
switch (action) {
2722
case 0:
28-
printMenu();
23+
System.out.println("\nShutting down...");
24+
quit = true;
2925
break;
3026

3127
case 1:
32-
System.out.println("Enter name, press enter and then the phone number: ");
33-
myPhone.addContact(scanner.nextLine(), scanner.nextLine());
28+
mobilePhone.printContacts();
3429
break;
3530

3631
case 2:
37-
System.out.println("Contacts: ");
38-
myPhone.getContactList();
32+
addNewContact();
3933
break;
4034

4135
case 3:
42-
System.out.println("Enter contact name: ");
43-
myPhone.updateContact(scanner.nextLine());
36+
updateContact();
4437
break;
4538

4639
case 4:
47-
System.out.println("Remove contact: ");
48-
myPhone.removeContact(scanner.nextLine());
40+
removeContact();
4941
break;
5042

5143
case 5:
52-
System.out.println("Contact name: ");
53-
myPhone.findContact(scanner.nextLine());
44+
queryContact();
5445
break;
5546

56-
case 9:
57-
System.out.println("Exiting phone..");
58-
flag = false;
47+
case 6:
48+
printActions();
5949
break;
50+
}
6051

61-
default:
62-
System.out.println("Wrong option");
52+
}
6353

64-
}
54+
}
55+
56+
private static void addNewContact() {
57+
System.out.println("Enter new contact name: ");
58+
String name = scanner.nextLine();
59+
System.out.println("Enter phone number: ");
60+
String phone = scanner.nextLine();
61+
Contact newContact = Contact.createContact(name, phone);
62+
if(mobilePhone.addNewContact(newContact)) {
63+
System.out.println("New contact added: name = " + name + ", phone = "+ phone);
64+
} else {
65+
System.out.println("Cannot add, " + name + " already on file");
6566
}
67+
}
68+
69+
private static void updateContact() {
70+
System.out.println("Enter existing contact name: ");
71+
String name = scanner.nextLine();
72+
Contact existingContactRecord = mobilePhone.queryContact(name);
73+
if(existingContactRecord == null) {
74+
System.out.println("Contact not found.");
75+
return;
76+
}
77+
78+
System.out.print("Enter new contact name: ");
79+
String newName = scanner.nextLine();
80+
System.out.print("Enter new contact phone number: ");
81+
String newNumber = scanner.nextLine();
82+
Contact newContact = Contact.createContact(newName, newNumber);
83+
if(mobilePhone.updateContact(existingContactRecord, newContact)) {
84+
System.out.println("Successfully updated record");
85+
} else {
86+
System.out.println("Error updating record.");
87+
}
88+
}
89+
90+
private static void removeContact() {
91+
System.out.println("Enter existing contact name: ");
92+
String name = scanner.nextLine();
93+
Contact existingContactRecord = mobilePhone.queryContact(name);
94+
if (existingContactRecord == null) {
95+
System.out.println("Contact not found.");
96+
return;
97+
}
98+
99+
if(mobilePhone.removeContact(existingContactRecord)) {
100+
System.out.println("Successfully deleted");
101+
} else {
102+
System.out.println("Error deleting contact");
103+
}
104+
}
105+
106+
private static void queryContact() {
107+
System.out.println("Enter existing contact name: ");
108+
String name = scanner.nextLine();
109+
Contact existingContactRecord = mobilePhone.queryContact(name);
110+
if (existingContactRecord == null) {
111+
System.out.println("Contact not found.");
112+
return;
113+
}
114+
115+
System.out.println("Name: " + existingContactRecord.getName() + " phone number is " + existingContactRecord.getPhoneNumber());
116+
}
66117

118+
private static void startPhone() {
119+
System.out.println("Starting phone...");
67120
}
68121

69-
public static void printMenu() {
70-
System.out.println("Options: \n" +
71-
"\t 0 - Menu \n" +
72-
"\t 1 - Add contact \n" +
73-
"\t 2 - Print contact list \n" +
74-
"\t 3 - Update contact \n" +
75-
"\t 4 - Remove contact \n" +
76-
"\t 5 - Search contact \n" +
77-
"\t 9 - Exit");
122+
private static void printActions() {
123+
System.out.println("\nAvailable actions:\npress");
124+
System.out.println("0 - to shutdown\n" +
125+
"1 - to print contacts\n" +
126+
"2 - to add a new contact\n" +
127+
"3 - to update existing an existing contact\n" +
128+
"4 - to remove an existing contact\n" +
129+
"5 - query if an existing contact exists\n" +
130+
"6 - to print a list of available actions.");
131+
System.out.println("Choose your action: ");
78132
}
79133
}

ArrayList/src/com/mausv/MobilePhone.java

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,87 @@
33
import java.util.ArrayList;
44

55
/**
6-
* Created by Mauricio on 10/16/2015.
6+
* Created by mausv on 7/27/2016.
77
*/
88
public class MobilePhone {
9-
private ArrayList<Contact> directory = new ArrayList<>();
9+
private String myNumber;
10+
private ArrayList<Contact> myContacts;
1011

11-
public void addContact (String name, String phone){
12-
Contact newContact = new Contact(name, phone);
13-
directory.add(newContact);
12+
public MobilePhone(String myNumber) {
13+
this.myNumber = myNumber;
14+
this.myContacts = new ArrayList<Contact>();
1415
}
1516

16-
public void getContactList() {
17-
for(int i = 0; i < directory.size(); i++) {
18-
System.out.println("Id: " + (i + 1) + " Name: " + directory.get(i).getName() + " Phone: " + directory.get(i).getPhone());
17+
public boolean addNewContact(Contact contact) {
18+
if (findContact(contact.getName()) >= 0) {
19+
System.out.println("Contact is already on file");
20+
return false;
1921
}
20-
}
2122

22-
public void findContact(String name) {
23-
String phoneNumber = "Contact not found!";
23+
myContacts.add(contact);
24+
return true;
2425

25-
int pos = findContactPosition(name);
26+
}
2627

27-
if(pos != -1) {
28-
phoneNumber = "Name: " + directory.get(pos).getName() + " Phone: " + directory.get(pos).getPhone();
28+
public boolean updateContact(Contact oldContact, Contact newContact) {
29+
int foundPosition = findContact(oldContact);
30+
if (foundPosition < 0) {
31+
System.out.println(oldContact.getName() + ", was not found.");
32+
return false;
2933
}
3034

31-
System.out.println(phoneNumber);
35+
this.myContacts.set(foundPosition, newContact);
36+
System.out.println(oldContact.getName() + ", was replaced with " + newContact.getName());
37+
return true;
3238
}
3339

34-
public void removeContact(String name) {
35-
int pos = findContactPosition(name);
36-
37-
if(pos != -1) {
38-
System.out.println("Removed " + directory.get(pos).getName());
39-
directory.remove(pos);
40+
public boolean removeContact(Contact contact) {
41+
int foundPosition = findContact(contact);
42+
if (foundPosition < 0) {
43+
System.out.println(contact.getName() + ", was not found.");
44+
return false;
4045
}
46+
this.myContacts.remove(foundPosition);
47+
System.out.println(contact.getName() + ", was deleted.");
48+
return true;
4149
}
4250

43-
public void updateContact(String name) {
44-
int pos = findContactPosition(name);
51+
private int findContact(Contact contact) {
52+
return this.myContacts.indexOf(contact);
53+
}
4554

46-
if(pos != -1) {
47-
System.out.println("Do you want to edit..? " + " Name: " + directory.get(pos).getName() + " Phone: " + directory.get(pos).getPhone());
48-
String choice = Main.scanner.nextLine();
49-
if(choice.equals("yes")) {
50-
System.out.println("Enter new name, press enter and then new phone and enter again: ");
51-
directory.get(pos).setName(Main.scanner.nextLine());
52-
directory.get(pos).setPhone(Main.scanner.nextLine());
53-
System.out.println("Updated contact! New info: " + " Name: " + directory.get(pos).getName() + " Phone: " + directory.get(pos).getPhone());
54-
} else {
55-
System.out.println("Then it's a no. Going to main menu.");
55+
private int findContact(String contactName) {
56+
for (int i = 0; i < this.myContacts.size(); i++) {
57+
Contact contact = this.myContacts.get(i);
58+
if (contact.getName().equals(contactName)) {
59+
return i;
5660
}
57-
} else {
58-
System.out.println("Contact doesn't exists.");
5961
}
62+
return -1;
6063
}
6164

62-
private int findContactPosition(String name) {
63-
int position = -1;
65+
public String queryContact(Contact contact) {
66+
if (findContact(contact) >= 0) {
67+
return contact.getName();
68+
}
69+
return null;
70+
}
6471

65-
for(int i = 0; i < directory.size(); i++) {
66-
if(name.equals(directory.get(i).getName())) {
67-
position = i;
68-
}
72+
public Contact queryContact(String name) {
73+
int position = findContact(name);
74+
if (position >= 0) {
75+
return this.myContacts.get(position);
6976
}
7077

71-
return position;
78+
return null;
79+
}
80+
81+
public void printContacts() {
82+
System.out.println("Contact List");
83+
for (int i = 0; i < this.myContacts.size(); i++) {
84+
System.out.println((i + 1) + "." +
85+
this.myContacts.get(i).getName() + " -> " +
86+
this.myContacts.get(i).getPhoneNumber());
87+
}
7288
}
7389
}

Arrays/.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)