Skip to content

Instantly share code, notes, and snippets.

@enseitankad0
Created March 9, 2018 22:52
Show Gist options
  • Save enseitankad0/db836c31126a2f233c10f067a8b71ac6 to your computer and use it in GitHub Desktop.
Save enseitankad0/db836c31126a2f233c10f067a8b71ac6 to your computer and use it in GitHub Desktop.
MobilePhone 9.03.2018
package com.snowden;
import java.util.ArrayList;
public class Contacts extends MobilePhone {
public String name;
public String phoneNumber;
public Contacts(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
}
package com.snowden;
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
//utowrzenie kilku obiektów do tablic
private static MobilePhone contactListMeizu = new MobilePhone();
// MobilePhone.contactsList.add(new )
//Contacts contactKamil = new Contacts("Kamil", "123 456 789");
//Contacts contactsMadzia = new Contacts("Madzia", "888 777 666");
public static void main(String[] args) {
// write your code here
boolean quit = false;
int choice = 0;
printInstructions();
while (!quit) {
System.out.println("What to do with the phone? ");
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 0: //
printInstructions();
break;
case 1:
printExistingContact();
break;
case 2:
addNewContact();
break;
case 3:
//modifyItem();
break;
case 4:
// removeItem();
break;
case 5:
//searchForItem();
break;
case 6:
//processArrayList();
case 7:
quit = true;
break;
}
}
}
public static void printInstructions() {
System.out.println("\nPress ");
System.out.println("\t 0 - To print choice options.");
System.out.println("\t 1 - To print the list of stored contacts");
System.out.println("\t 2 - To add an new contact");
System.out.println("\t 3 - To update exsisting contact");
System.out.println("\t 4 - To remove exsisting contact");
System.out.println("\t 5 - To search/find contact.");
System.out.println("\t 5 - To quit the application.");
}
public static void addNewContact() {
System.out.println("Add contact name");
String contactName = scanner.nextLine();
System.out.print("Enter contact number");
String contactNumber = scanner.nextLine();
Contacts contact = new Contacts(contactName, contactNumber);
contactListMeizu.addContact(contact);
}
public static void printExistingContact(){
contactListMeizu.printContactList();
}
}
//store
//modify
//query - zapytaj czy jest kontakt
// # contacts
// Create a program that implements a simple mobile phone with the following capabilities.
// Able to store, modify, remove and query contact names.
// You will want to create a separate class for Contacts (name and phone number).
// Create a master class (MobilePhone) that holds the ArrayList of Contacts
// The MobilePhone class has the functionality listed above.
// Add a menu of options that are available.
// Options: Quit, print list of contacts, add new contact, update existing contact, remove contact
// and search/find contact.
// When adding or updating be sure to check if the contact already exists (use name)
// Be sure not to expose the inner workings of the Arraylist to MobilePhone
// e.g. no ints, no .get(i) etc
// MobilePhone should do everything with Contact objects only.
package com.snowden;
import java.util.ArrayList;
public class MobilePhone extends Main{
//Ponieważ Listy są klasami generycznymi to powinniśmy im nadać konkretny typ
// (w komentarzu ktoś ukazał, że standardowy kompilator wręcz tego wymaga w obecnej wersji).
// W naszym powyższym przykładzie mogliśmy utworzyć taką listę tablicową:
public ArrayList<Contacts> contactList = new ArrayList<Contacts>(); // moze byc <> na koncu zamiast <String>
public void addContact(Contacts contact){
contactList.add(contact);
}
public void printContactList(){
System.out.println("Contact Book is storing: "+ contactList.size()+"positions");
for(int i=0; i < contactList.size(); i++) {
System.out.println((i + 1)+". " + contactList.get(i).name + " "+ contactList.get(i).phoneNumber);
}
}
//public static void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment