JavaCode(review)
mobilePhone contact code.
◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e
2020. 5. 30. 07:23
반응형
Class Contact
package com.company;
public class lectureContact {
private String name;
private String phoneNumber; // field
//constructor
public lectureContact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
//getter
public String getName(){
return name;
}
public String getPhoneNumber(){
return phoneNumber;
}
//another method
public static lectureContact createContact(String name, String phoneNumber){
return new lectureContact(name,phoneNumber);
}
//that method is just calling constructor nothing else,
// you don't have to write code 100% as Tim,
// this is just another way to solve the challenge, there are many ways.
}
Class mobilePhone
package com.company;
import java.util.ArrayList;
public class lectureMobilePhone { //master class / hold Arraylist
private String myNumber; //field
private ArrayList<lectureContact> myContacts; //arrayList define/ class<data type> object
// data type을 contact class이용
//constructor
public lectureMobilePhone(String myNumber) {
this.myNumber = myNumber;
this.myContacts = new ArrayList<lectureContact>(); //ArrayList 공간설정
}
//method- add, modify, remove, find name, query contact(number), print
public boolean addNewContact(lectureContact contact){
if(findContact(contact.getName())>=0){ //object contact.getName
System.out.println("contact is already on file");
return false;
}
myContacts.add(contact);
return true;
}
public boolean modifyContact(lectureContact oldContact, lectureContact newContact){
int foundIndex = findContact(oldContact);
if(foundIndex<0){
System.out.println(oldContact.getName()+" was not found");
return false; // modify 안됐으니 false
} else if(findContact(newContact.getName()) != -1){
System.out.println("already existing name"); //이름중복되는경우 아웃
return false;
}
this.myContacts.set(foundIndex, newContact); //ArrayList 해당값 바꾸기(재설정).
System.out.println(oldContact.getName()+" was replaced with"+newContact.getName());
return true;
}
public boolean removeContact(lectureContact contact){
int foundIndex = findContact(contact);
if(foundIndex<0){
System.out.println(contact.getName()+" was not found");
return false;
}
this.myContacts.remove(foundIndex); //ArrayList 해당값 삭제
System.out.println(contact.getName() + " was deleted.");
return true;
}
private int findContact(lectureContact contact){
return this.myContacts.indexOf(contact);
}
private int findContact(String contactName){ //i=index
for(int i=0; i<this.myContacts.size(); i++){
lectureContact contact = this.myContacts.get(i); //class object = array i 값
if(contact.getName().equals(contactName)){
return i;
}
}
return -1;
}
public String queryContact(lectureContact contact){
if(findContact(contact)>=0){
return contact.getName();
}
return null;
}
public lectureContact queryContact(String name){
int index = findContact(name);
if(index>=0){
return this.myContacts.get(index);
}
return null;
}
public void printContact(){
System.out.println("Contact list");
for(int i=0; i<this.myContacts.size(); i++){
System.out.println((i+1)+"."+this.myContacts.get(i).getName()+" : "+
this.myContacts.get(i).getPhoneNumber());
}
}
}
Main. print out
package com.company; //class contact -> class mobile: contact-arrayList로 연결
import java.util.Scanner;
public class lectureMain {
private static Scanner scanner = new Scanner(System.in); //console input search
private static lectureMobilePhone mobilePhone = new lectureMobilePhone("011 2843 2978"); //object. call constructor
public static void main(String[] args) {
boolean quit = false; // boolean defualt는 false
startPhone();
Instruction();
while(!quit){ // !quit => quit=false일때 라는 말임.
System.out.println("\nEnter action Number");
int actionNumber = scanner.nextInt(); //consle input
scanner.nextLine();
switch(actionNumber){
case 0:
System.out.println("\nShutting down..");
quit = true;
break;
case 1:
mobilePhone.printContact();
break;
case 2:
addNewContact();
break;
case 3:
modifyContact();
break;
case 4:
removeContact();
break;
case 5:
queryContact();
break;
case 6:
Instruction();
break;
}
}
}
//add, modify, remove, query(문의), star ,instruction method
private static void addNewContact(){
System.out.println("Enter new contact name: ");
String name = scanner.nextLine(); //console string input
System.out.println("Enter phone number");
String phone = scanner.nextLine(); //console string input
lectureContact newContact = lectureContact.createContact(name, phone); //Contact object
if(mobilePhone.addNewContact(newContact)){ //mobile object.method(contact object)
System.out.println("New contact added name = "+name+" phone = "+phone);
}else{
System.out.println("already on file");
}
}
private static void modifyContact(){
System.out.println("Enter current name : ");
String name =scanner.nextLine() ;
lectureContact currentName = mobilePhone.queryContact(name);
if(currentName==null){
System.out.println("cannot be found");
return;
}
System.out.println("Enter new contact name : ");
String newName = scanner.nextLine();
System.out.println("Enter new phone number : ");
String newPhoneNumber = scanner.nextLine();
lectureContact newContact = lectureContact.createContact(newName,newPhoneNumber);
if(mobilePhone.modifyContact(currentName,newContact)){
System.out.println("modified");
}else{
System.out.println("Error modifying record");
}
}
private static void removeContact(){
System.out.println("Enter current name");
String name = scanner.nextLine();
lectureContact currentName = mobilePhone.queryContact(name);
if(currentName==null){
System.out.println("contact not found");
return;
}
if(mobilePhone.removeContact(currentName)){
System.out.println("deleted");
}else{
System.out.println("not deleted");
}
}
private static void queryContact(){
System.out.println("Enter current name : ");
String name = scanner.nextLine();
lectureContact currentName = mobilePhone.queryContact(name);
if(currentName==null){
System.out.println("contact not found");
return;
}
System.out.println("Name: "+currentName.getName()+"phone number is"+currentName.getPhoneNumber());
}
private static void startPhone(){
System.out.println("Staring phone...");
}
private static void Instruction(){
System.out.println("\n Available action : \n press ");
System.out.println("0 - to shutdown\n"+
"1 - to print contact\n"+
"2 - to add contact\n"+
"3 - to modify contact\n"+
"4 - to remove contact\n"+
"5 - to query if contact exist\n"+
"6 - to print Instruction\n");
System.out.println("Choose your action: ");
}
}
Out put
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=53079:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\ArrayList\out\production\ArrayList com.company.lectureMain
Staring phone...
Available action :
press
0 - to shutdown
1 - to print contact
2 - to add contact
3 - to modify contact
4 - to remove contact
5 - to query if contact exist
6 - to print Instruction
Choose your action:
Enter action Number
1
Contact list
Enter action Number
2
Enter new contact name:
ag
Enter phone number
01130484
New contact added name = ag phone = 01130484
Enter action Number
2
Enter new contact name:
ag
Enter phone number
01130484
contact is already on file
already on file
Enter action Number
2
Enter new contact name:
jina
Enter phone number
35983020
New contact added name = jina phone = 35983020
Enter action Number
4
Enter current name
jina
jina was deleted.
deleted
Enter action Number
5
Enter current name :
jina
contact not found
Enter action Number
6
Available action :
press
0 - to shutdown
1 - to print contact
2 - to add contact
3 - to modify contact
4 - to remove contact
5 - to query if contact exist
6 - to print Instruction
Choose your action:
Enter action Number
0
Shutting down..
Process finished with exit code 0
Out put - modify에 중복이름 입력경우
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=53319:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\ArrayList\out\production\ArrayList com.company.lectureMain
Staring phone...
Available action :
press
0 - to shutdown
1 - to print contact
2 - to add contact
3 - to modify contact
4 - to remove contact
5 - to query if contact exist
6 - to print Instruction
Choose your action:
Enter action Number
2
Enter new contact name:
john
Enter phone number
1245
New contact added name = john phone = 1245
Enter action Number
2
Enter new contact name:
pids
Enter phone number
0897
New contact added name = pids phone = 0897
Enter action Number
1
Contact list
1.john : 1245
2.pids : 0897
Enter action Number
3
Enter current name :
john
Enter new contact name :
pids
Enter new phone number :
394
already existing name
Error modifying record
Enter action Number