Mobile
You are given an incomplete
Mobile class.A Mobile object created using the
Mobile class should have the properties like brand, ram, battery, isOnCall, and song.Implement the
Mobile class to initialize the mentioned properties and add the following methods,
Method:
charging
Description: When this method is called, it should set the value of the battery to 100, if the battery is already 100 then log "Mobile Fully Charged" and call removeCharging
method:
removeCharging
Description:
It should log "Please remove charging"
method:
playMusic
Description:
It should log a text with the song, as shown in the sample output.
method:
stopMusic
Description:
It should log "Music Stopped"
method:
makeCall
Description:
When this method is called, it should set the value of the isOnCall to true and log "Calling ..."
method:
endCall
Description:
When this method is called, it should log "No ongoing call to end" if isOnCall is false, else log "Call Ended" and set the value of the isOnCall to false
0 <=
battery <= 100
sample input
Apple
2 GB
90
Waka Waka
false
sample output
Mobile charged 90%
Mobile charged 100%
Playing Waka Waka song
Music stopped
No ongoing call to end
Calling...
Call Ended
public class Phone {
//private fields
private String label;
private String phonNum;
//constructor: with 2 parameters
public Phone(String label, String ph){
this.label = label;
this.phonNum = ph;
}
//constructor: with another phone instance
public Phone(Phone phone){
if(phone!=null){
this.phonNum = phone.phonNum;
this.label = phone.label;
}
}
/**
* @return the label
*/
public String getLabel() {
return label;
}
/**
* @return the phonNum
*/
public String getPhonNum() {
return phonNum;
}
}
import java.util.ArrayList;
/**
* Class: ContactInfo: It maintains phone numbers for a name
*/
public class ContactInfo {
//private fields
private String name;
private ArrayList<Phone> phoneNums;
/**
* default constructor
*/
public ContactInfo(){
this.name = "unknown";//set to unknown
this.phoneNums = new ArrayList<Phone>();//create empty list
}
/**
* Parameterized constructor: ContactInfo
* @param name
* @param phoneNumber
*/
public ContactInfo(String name,ArrayList<Phone> phoneNumber){
this.name = name;
this.phoneNums = new ArrayList<Phone>();
//deep copy
if(phoneNumber!=null && !phoneNumber.isEmpty()){
for(Phone p : phoneNumber){
this.phoneNums.add(p);
}
}
}
/**
* getter for name field
* @return
*/
public String getName(){
return this.name;
}
/**
* getter for phoneNums
* @return
*/
public ArrayList<Phone> getPhones(){
return this.phoneNums;
}
/**
* Add a phone to phoneNums
* It will return true if addition is successful
* But it return false if similar label or phone number already ests in list
* without adding it to the list
* @param p
* @return
*/
public boolean addPhone(Phone p){
boolean isAdded = false;
if(!isPhoneExists(p)){//check if isPhoneExists is false
this.phoneNums.add(p);//add p
isAdded = true;//set boolean field to true
}
return isAdded;
}
/**
* Remove phoneNum from the list of phones
* Returns true if successful
* If the list is empty or there is no such phoneNum then it will
* return false, without removing anything
* @param phonNum
* @return
*/
public boolean removePhone(String phonNum){
int index = getIndexOfPhoneNumber(phonNum);//get the index of given phoneNum in list
if(this.phoneNums.isEmpty() || index==-1)//index will be -1 if phoneNum not in list
return false;
this.phoneNums.remove(index);//otherwise it will remove from the list
return true;
}
/**
* helper method to return in which indec the phoneNum exists
* If not found will return -1
* @param phoneNum
* @return
*/
private int getIndexOfPhoneNumber(String phoneNum){
int index = -1;
if(!this.phoneNums.isEmpty()){
for(int i = 0; i < this.phoneNums.size(); i++){
Phone p = this.phoneNums.get(i);
if(p.getPhonNum().equals(phoneNum)){
index = i;
break;
}
}
}
return index;
}
/**
* Helper method to retun false if phone with simalalr label
* or phone number exists in the list
* true otherwise
* @param phone
* @return
*/
private boolean isPhoneExists(Phone phone){
boolean isExist = false;
if(phone!=null && !this.phoneNums.isEmpty()){
for(Phone p : this.phoneNums){
if(p.getLabel().equals(phone.getLabel()) ||
p.getPhonNum().equals(phone.getPhonNum())){
isExist = true;
break;
}
}
}
return isExist;
}
}
package Program13;
import java.util.ArrayList;
/**
*This lass will test different methods of Phone and ContactInfo classes
*/
public class TestClass {
public static void main(String[] args){
System.out.println("*****Testing Phone Class*************");
Phone p1 = new Phone("iphone","(513)000-0000");
System.out.println("p1.getLabel()-->"+p1.getLabel());
System.out.println("p1.getPhonNum()-->"+p1.getPhonNum());
Phone p2 = new Phone("samsung","(513)111-1111");
System.out.println("p2.getLabel()-->"+p2.getLabel());
System.out.println("p2.getPhonNum()-->"+p2.getPhonNum());
Phone p3 = new Phone(p2);
System.out.println("p3.getLabel()-->"+p3.getLabel());
System.out.println("p3.getPhonNum()-->"+p3.getPhonNum());
//p1.setLabel("mobile"): This will not work since there is no public method in Phone class with this name
System.out.println("*****Testing ContactInfo Class*************");
ContactInfo pp1 = new ContactInfo();
System.out.println( pp1.getName());
System.out.println(pp1.getPhones());
Phone p4 = new Phone("mobile","(555)444-4444");
ArrayList<Phone> list1 = new ArrayList<Phone>();
boolean status = list1.add(p4);
System.out.println(status);
pp1 = new ContactInfo("Jim Carey",list1);
System.out.println(pp1);
System.out.println( pp1.getName());
System.out.println(pp1.getPhones());
for(Phone phone: pp1.getPhones()){
System.out.println(phone.getLabel()+ " "+ phone.getPhonNum());
}
status = pp1.addPhone(new Phone("home","(666)666-6666"));
System.out.println(status);
for(Phone phone: pp1.getPhones()){
System.out.println(phone.getLabel()+ " "+ phone.getPhonNum());
}
Phone p5 = new Phone("main","(666)666-6666");
System.out.println(p5);
status = pp1.addPhone(p5);
System.out.println(status);
status = pp1.addPhone(new Phone("home","(123)000-8723"));
System.out.println(status);
status = pp1.removePhone("(000)000-0000");
System.out.println(status);
status = pp1.removePhone("(555)444-4444");
System.out.println(status);
for(Phone phone: pp1.getPhones()){
System.out.println(phone.getLabel()+ " "+ phone.getPhonNum());
}
}
}
Comments
Leave a comment