Jul 31, 2018 01:40 AM
(This post was last modified: Jul 31, 2018 01:44 AM by Consulting Detective.)
It has been 2 weeks since I last posted in the Java section.
I have learned heaps since then. Constructors, encapsulation, polymorphism, composition, inheritance, pass by value vs pass by reference and so on. Right now I'm practicing ArrayLists. Just want to share something I made. It is a simple phonebook, it can search, add, delete and modify contacts. I know there is no input sanitation in my code, but I'm getting my hands dirty on that later.
I have learned heaps since then. Constructors, encapsulation, polymorphism, composition, inheritance, pass by value vs pass by reference and so on. Right now I'm practicing ArrayLists. Just want to share something I made. It is a simple phonebook, it can search, add, delete and modify contacts. I know there is no input sanitation in my code, but I'm getting my hands dirty on that later.
PHP Code
<?php
import java.util.Scanner;
public class MobilePhone{
public static void main(String[] args){
Contacts c = new Contacts();
Scanner scan = new Scanner(System.in);
int choice = -1;
System.out.println("---------Sherlock Phone---------");
System.out.println("[1]Search Contact");
System.out.println("[2]Add Contact");
System.out.println("[3]Delete Contact");
System.out.println("[4]Modify Contact");
System.out.println("[0]Exit");
while(choice != 0){
System.out.println("--------------------------------");
System.out.print("What would you like to do? ");
choice = scan.nextInt();
switch(choice){
case 1:
System.out.print("Name to search: ");
c.getContact(scan.next());
break;
case 2:
System.out.print("Name to add: ");
if(c.addContact(scan.next())){
System.out.print("Enter phone number: ");
c.addPhoneNumber(scan.nextInt());
}
break;
case 3:
System.out.print("Name to delete: ");
c.delContact(scan.next());
break;
case 4:
int tempIndex;
int tempPhoneNumber;
String tempName;
System.out.print("Enter contact to modify: ");
tempIndex = c.modContact(scan.next());
if(tempIndex >= 0){
System.out.print("Enter new name: ");
tempName = scan.next();
c.updateName(tempIndex, tempName);
System.out.print("Enter new phone number: ");
tempPhoneNumber = scan.nextInt();
c.updatePhoneNumber(tempIndex, tempPhoneNumber);
}else{
System.out.println("Contact does not exist.");
}
break;
case 0:
break;
default:
}
}
}
}
PHP Code
<?php
import java.util.ArrayList;
class Contacts{
private ArrayList<String> names = new ArrayList<String>();
private ArrayList<Integer> phoneNumbers = new ArrayList<Integer>();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public void getContact(String name){
if(names.contains(name)){
System.out.println("Name:" + name);
System.out.println("Phone Number: " + getPhoneNumber(names.indexOf(name)));
}else{
System.out.println("Contact not found.");
}
}
public int getPhoneNumber(int index){
return phoneNumbers.get(index);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public boolean addContact(String name){
if(names.contains(name)){
System.out.println("Name already exists.");
return false;
}else{
names.add(name);
}
return true;
}
public void addPhoneNumber(int phoneNumber){
phoneNumbers.add(phoneNumber);
System.out.println("Contact added.");
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public void delContact(String name){
if(names.contains(name)){
names.remove(name);
System.out.println("Contact deleted.");
}else{
System.out.println("Contact does not exist.");
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public int modContact(String name){
if(names.contains(name)){
return names.indexOf(name);
}
return -1;
}
public void updateName(int index, String name){
names.set(index, name);
}
public void updatePhoneNumber(int index, int phoneNumber){
phoneNumbers.set(index, phoneNumber);
}
}



