Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- public class Contact {
- private String name;
- private String phoneNumber;
- public Contact(String name, String phoneNumber) {
- this.name = name;
- this.phoneNumber = phoneNumber;
- }
- public String getName() {
- return name;
- }
- public String getPhoneNumber() {
- return phoneNumber;
- }
- public static Contact createContact(String name, String phoneNumber){
- return new Contact(name,phoneNumber);
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- }
- package com.company;
- import java.util.ArrayList;
- public class MobilePhone {
- private String myNumber;
- private ArrayList<Contact> myContacts;
- public MobilePhone(String phoneNumber){
- this.myNumber = phoneNumber;
- this.myContacts = new ArrayList<>();
- }
- public boolean addNewContact(Contact contact) {
- if (findContact(contact.getName()) >=0){
- System.out.println("Contact is already on name");
- System.out.println("The contact that is already on name has index " + findContact(contact.getName()));
- return false;
- } else {
- myContacts.add(contact);
- System.out.println("A new contact was added to the list");
- return true;
- }
- }
- public boolean removeContact(Contact contact) {
- if(findContact(contact) >=0){
- System.out.println(contact.getName() + "was removed from the contact list");
- this.myContacts.remove(contact);
- return true;
- }else{
- System.out.println("Contact to be removed was not found in the contact list");
- return false;
- }
- }
- private int findContact(Contact contact) {
- return this.myContacts.indexOf(contact);
- }
- public int findContact(String contactName){
- boolean foundMyContact = false;
- for(int i = 0; i < this.myContacts.size(); i++){
- foundMyContact = true;
- Contact contact = this.myContacts.get(i);
- if (contact.getName().equals(contactName)){
- return i;
- }
- }
- return -1;
- }
- public int getMobilePhoneLength() {
- return this.myContacts.size();
- }
- public String queryContact(Contact contact){
- if(findContact(contact) >= 0){
- return contact.getName();
- } else return null;
- }
- public boolean updateContact(Contact oldContact, Contact newContact){
- int foundPosition = findContact(oldContact);
- if(foundPosition < 0){
- System.out.println("Old contact.getName was not found");
- return false;
- }
- myContacts.set(foundPosition,newContact);
- return true;
- }
- public String getMyNumber() {
- return myNumber;
- }
- public ArrayList<Contact> getMyContacts() {
- return myContacts;
- }
- public String[] myStringArray(){
- Contact[] myContactString = new Contact[myContacts.size()];
- for(int i = 0; i < myContactString.length; i++){
- myContactString[i] = myContacts.get(i);
- }
- String[] myStringArray2;
- myStringArray2 = new String[myContactString.length];
- for(int i = 0; i < myContactString.length; i++){
- myStringArray2[i] = myContacts.get(i).getName();
- }
- return myStringArray2;
- }
- public void printContacts(){
- for (int i = 0; i < myContacts.size(); i++){
- System.out.println("Contact name is " + myContacts.get(i).getName());
- }
- }
- }
- package com.company;
- import java.util.Scanner;
- public class Main {
- private static Scanner scanner = new Scanner(System.in);
- public static MobilePhone mobilePhone = new MobilePhone("07885599015");
- public static void main(String[] args) {
- boolean quit = false;
- startPhone();
- printActions();
- while(!quit){
- System.out.println("\n Enter action: (6 to show available actions)");
- int action = scanner.nextInt();
- if (action >= 0 && action < 7){
- switch(action){
- case 0:
- System.out.println("\nShutting down");
- break;
- case 1:
- printContacts();
- break;
- case 2:
- addContacts();
- break;
- case 3:
- updateContact();
- break;
- case 4:
- removeContact();
- break;
- case 5:
- queryContact();
- break;
- case 6:
- printActions();
- }
- }
- else{
- System.out.println("Invalid input");
- }
- }
- }
- private static void startPhone(){
- System.out.println("Starting phone.......");
- }
- private static void printContacts(){
- mobilePhone.printContacts();
- }
- private static void printActions(){
- System.out.println("\nAvailable actions:\npress");
- System.out.println("0 - to shut down\n"
- + "1 - to print contacts\n"
- + "2 - to add a new contact\n"
- + "3 - to update an existing contact\n"
- + "4 - to remove an existing contact\n"
- + "5 - to query whether an an input exists\n"
- + "6 - to print a list of existing actions\n"
- );
- }
- private static void addContacts(){
- System.out.println("Enter the name of the contact you would like to add...");
- String contactName = scanner.nextLine();
- scanner.nextLine();
- System.out.println("Enter the phone number of the contact you would like to add...");
- String phoneNumber = scanner.nextLine();
- Contact contact = new Contact(contactName,phoneNumber);
- mobilePhone.addNewContact(contact);
- }
- private static void removeContact(){
- System.out.println("Enter the name of the contact you would like to remove");
- String removedContactName = scanner.nextLine();
- int indexOfRemovedContact = mobilePhone.findContact(removedContactName);
- if(indexOfRemovedContact >=0){
- Contact contactToBeRemoved = mobilePhone.getMyContacts().get(indexOfRemovedContact);
- mobilePhone.removeContact(contactToBeRemoved);
- System.out.println("The mobile phone contact with name " + removedContactName + " has been removed");
- }
- else{
- System.out.println("The name of the contact you have inputted is not in the contact list");
- }
- }
- private static void updateContact(){
- System.out.println("Enter the name of the contact you would like to update...");
- String modifiedContactName = scanner.nextLine();
- int indexOfModifiedContact = mobilePhone.findContact(modifiedContactName);
- scanner.nextLine();
- System.out.println(indexOfModifiedContact);
- System.out.println("Would you like to change the name of the contact you would like to change? (input yes or no)");
- String BooleanOfName = scanner.nextLine();
- BooleanOfName.toLowerCase();
- System.out.println(BooleanOfName);
- if (BooleanOfName.equals("yes")){
- System.out.println("Enter the new name of the contact");
- String newContactName = scanner.nextLine();
- mobilePhone.getMyContacts().get(indexOfModifiedContact + 1).setName(newContactName);
- }
- else if (BooleanOfName == "no"){
- System.out.println("You do not want to change the inputted contact");
- } else {
- System.out.println("You did not enter either yes or no");
- }
- System.out.println("Would you like to change the phone number of the contact you would like to change?(input YES or NO ");
- String BooleanOfName2 = scanner.nextLine();
- BooleanOfName2.toLowerCase();
- if (BooleanOfName2 == "Yes"){
- System.out.println("Enter the new phone number of the contact");
- String newPhoneNumber = scanner.nextLine();
- mobilePhone.getMyContacts().get(indexOfModifiedContact).setPhoneNumber(newPhoneNumber);
- }
- if (BooleanOfName2.equals("no")){
- System.out.println(BooleanOfName2 + " Was not edited");
- }
- }
- private static void queryContact(){
- System.out.println("Enter the name of the contact you would like to query");
- String queryContactName = scanner.nextLine();
- scanner.nextLine();
- int indexOfQueriedContact = mobilePhone.findContact(queryContactName);
- System.out.println(indexOfQueriedContact);
- if(indexOfQueriedContact >=0){
- System.out.println("The item you entered exists in the list and has position " + indexOfQueriedContact);
- }else {
- System.out.println("The item you entered does not exist in the contact list");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement