Note that there are some explanatory texts on larger screens.

plurals
  1. POJava contact list program using singleton pattern
    text
    copied!<p>I have to made a simple program which acts as a contact list. The program has main functionality such as: adding new contact, deleting contact, view all contact and save contacts to a file. The main problem for me is that the program must use singleton pattern. I have never used it before. So just wrote it normal how I can. Can anyone help me to make it as singleton?</p> <pre><code>class Contact{ private String name; private String address; private String email; private String phone; Contact(String name, String address, String email, String phone){ this.name=name; this.address=address; this.email=email; this.phone=phone; } public Contact() { } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Contact{" + "name=" + name + ", address=" + address + ", email=" + email + ", phone=" + phone + '}'; } public int compareTo(Contact c){ return name.compareTo(c.getName()); } public void write(){ try{ Contact contact; contact = new Contact(); Contact c = contact; File file = new File("ContactList.txt"); if(!file.exists()){ file.createNewFile(); try(PrintWriter writer = new PrintWriter(new FileWriter("ContactList.txt", true))){ writer.printf("%s\r\n", name + ", " + address + ", " + email + ", " + phone); writer.flush(); writer.close(); }catch(Exception e){ e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } public class ContactList { ArrayList&lt;Contact&gt; contactlist = new ArrayList&lt;Contact&gt;(); Contact contact; private int top = 0; public static void main(String[] args){ Contact contact; contact = new Contact(); Contact c = contact; ContactList list = new ContactList(); BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in)); String choose = ""; while(true){ System.out.println("\n[1] Add contact"); System.out.println("[2] Delete contact"); System.out.println("[3] View all contacts"); System.out.println("[4] Edit contact"); System.out.println("[5] Exit"); System.out.print("Choose : "); try{ choose = keyIn.readLine(); }catch(Exception e){ System.out.println("Error"); } if(choose.equals("1")) { list.addContact(); }else if(choose.equals("2")) { list.deleteContact(); }else if(choose.equals("3")) { list.viewContacts(); }else if(choose.equals("4")) { list.editContact(); }else if(choose.equals("5")) { System.exit(0); }else { System.out.println ("Error"); } } } public void addContact(){ BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in)); String name = ""; String address = ""; String email = ""; String phone = ""; try{ System.out.print("Name: "); name = keyIn.readLine(); System.out.print("Address "); address = keyIn.readLine(); System.out.print("E-mail address: "); email = keyIn.readLine(); System.out.print("Phone number: "); phone = keyIn.readLine(); }catch(Exception e){ e.printStackTrace(); } Contact contact = new Contact(name, address, email, phone); contactlist.add(contact); top++; try{ contact.write(); }catch(Exception e){ e.printStackTrace(); } } public void deleteContact(){ BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in)); int index = 0; if(top == 0){ System.out.println("Empty contact lis"); } try{ viewContacts(); System.out.println("\nChoose number to delete: "); index = Integer.parseInt(keyIn.readLine())-1; }catch(Exception e){ e.printStackTrace(); } if(index &lt; 0 || index &gt;= top){ System.out.println("No contact match"); }else{ contactlist.remove(contact); top--; } try{ contact.write(); }catch(Exception e){ e.printStackTrace(); } } public void viewContacts(){ for(int index = 0; index &lt; top; index++){ System.out.println((index+1)+ " Name " + contactlist.get(index).getName()); System.out.println("Address: " + contactlist.get(index).getAddress()); System.out.println("E-mail: " + contactlist.get(index).getEmail()); System.out.println("Phone: " + contactlist.get(index).getPhone()); } } public void editContact(){ BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in)); int index = 0; String name = ""; String address = ""; String email = ""; String phone = ""; try{ System.out.print("Edit contact: "); index = Integer.parseInt(keyIn.readLine())-1; System.out.print("Name: "); name = keyIn.readLine(); System.out.print("Address: "); address = keyIn.readLine(); System.out.print("E-mail: "); email = keyIn.readLine(); System.out.print("Phone: "); phone = keyIn.readLine(); }catch(Exception e){ e.printStackTrace(); } Contact contact = new Contact(name, address, email, phone); contactlist.add(index, contact); try{ contact.write(); }catch(Exception e){ e.printStackTrace(); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload