Menu class for Java object











up vote
-2
down vote

favorite












New here so not sure if Im doing this right.



I had to create a selection menu for a Contact Directory project for school. This is the second time Ive had to create a menu for a particular project so I recycled the menu from my previous project to accommodate this Contact Directory and all went well.



But I realized Im basically just listing the Objects methods for the user to select from in both scenarios so I got bored and decided to try to generalize the menu class I had to work with any directory-type class and this is what I came up with.



Im fairly new to programming so Im just looking for insight as to how to improve my code or if theres a better way to do this.



    package menu2;

import java.lang.reflect.Method;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;

public class Menu2 {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
private String name;
private int selection;
private int count;
private Class c;
private String menuDisplay = " ";
private Method methods;
/**
*creates menu to navigate directory list
* @throws IOException
*/

public Menu2(Object o) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this.c = o.getClass();
menuView();
selection = 0;
do {
selection = menu();
if(selection != count) {
methods[selection-1].invoke(o);
}
} while (selection != count);
}

/**
*displays menu options
* @return
*/
public void menuView() {
methods = c.getDeclaredMethods();
Method temp = new Method[methods.length];
count = 0;
for (int i = 0; i < methods.length; i++) {
System.out.println("public method: " + c.getSimpleName());
if(methods[i].getParameterCount()== 0 && !methods[i].getName().equalsIgnoreCase("iterator")&&!methods[i].getName().equalsIgnoreCase("tostring")){
menuDisplay += (count+1)+". "+methods[i].getName()+"n";
temp[count] = methods[i];
count++;
}
}
menuDisplay += count+". Quit";
methods = temp;

/*Method x = new Method[count];
for(int i = 0; i < count;i++){
x[i] = temp[i];
}
methods = x;*/
}

public int menu() {
String choiceStr;
int choice;
choiceStr = JOptionPane.showInputDialog(menuDisplay);
choice = Integer.parseInt(choiceStr);
return choice;
}
}


This menu is instantiated inside of ContactDirectory constructor which is instantiated in main.
Everything works it would seem but any advice, better code practice insight is welcome.



Driver:



package driver;
import contactdir.ContactDir;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class Driver {
public static void main(String args) throws IOException, IllegalAccessException, InvocationTargetException{
ContactDir dir = new ContactDir();
//Menu dirMenu = new Menu();
}
}


Directory:



package contactdir;

import contactinfo.ContactInfo;

import javax.swing.JOptionPane;
import jsjf.*;
import fileman.FileMan;
import java.awt.Dimension;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import menu2.Menu2;

public class ContactDir implements Iterable<ContactInfo>, Serializable {

private ArrayOrderedList<ContactInfo> list;
private FileMan<ContactInfo> fman;
private JScrollPane scroll;
private JTextArea text;
private boolean value = false;

public ContactDir() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
this.list = new ArrayOrderedList<>();
this.fman = new FileMan<>();
Menu2 menu = new Menu2(this);
}

public void addContact(){
list.add(new ContactInfo());

}

public void addContact(ContactInfo contact){
if(contact != null && list != null){

list.add(contact);
}
}

public void deleteContact(ContactInfo contact){
if(contact != null && list != null)
list.remove(contact);
}

/**
*deletes contact from list
* @param name
* @return
*/
public boolean deleteContact(String name){
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}
public boolean deleteContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to remove:");
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}

/**
*finds contact in list
* @param name
* @return
*/
public ContactInfo findContact(String name){

for(ContactInfo info : list )
{
if(info.getName().equalsIgnoreCase(name))
{
return info;
}

}
return null;
}

/**
*updates existing contact in list
* @param name
* @return
*/
public boolean updateContact(String name){

value = false;
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}
public boolean updateContact(){

value = false;
String name = JOptionPane.showInputDialog("Enter the name of the contact to update:");
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}

/**
*displays contact in list
* @param name
* @return
*/
public boolean displayContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to find:");
if(list != null){
JOptionPane.showMessageDialog(null,findContact(name).toString());
value = true;
}
else
JOptionPane.showMessageDialog(null,"Object not found");
return value;
}

/**
*displays all contacts in list
*/
public void displayDir(){

text = new JTextArea((this.toString()));
text.setLineWrap(true);
text.setWrapStyleWord(true);
scroll = new JScrollPane(text);
scroll.setPreferredSize( new Dimension( 500, 500 ) );
if(list != null)
JOptionPane.showMessageDialog(null, scroll);
}

@Override
public String toString() {
return "Contact Directory:n" + list;
}

/**
*saves list to file
* @return
* @throws IOException
*/
public boolean save() throws IOException{

fman.save("Directory.txt", list);
return value;
}

/**
*loads list from file
* @return
* @throws IOException
*/
public boolean load() throws IOException{

list = fman.load("Directory.txt");

return value;
}

@Override
public Iterator<ContactInfo> iterator() {
return list.iterator();//To change body of generated methods, choose Tools | Templates.
}

/**
* @param args the command line arguments
*/


}


PS all of this is beyond project requirements Im just trying out new things and learning.










share|improve this question




















  • 2




    If this code works fine, then this question is off topic on Stack Overflow. It may be good for our sister site Code Review, but please remember to check their rules before posting.
    – Joe C
    Nov 10 at 21:47






  • 2




    I should also warn you that posting an entire homework solution on a public website such as this may open yourself up for accusations of plagiarism from your school. Please check your school's academic honesty policy for more information.
    – Joe C
    Nov 10 at 21:52












  • Okay I will try Code Review. And this homework has already been submitted in a simpler form I just wanted to try making it better.
    – bl00dbath
    Nov 10 at 22:24















up vote
-2
down vote

favorite












New here so not sure if Im doing this right.



I had to create a selection menu for a Contact Directory project for school. This is the second time Ive had to create a menu for a particular project so I recycled the menu from my previous project to accommodate this Contact Directory and all went well.



But I realized Im basically just listing the Objects methods for the user to select from in both scenarios so I got bored and decided to try to generalize the menu class I had to work with any directory-type class and this is what I came up with.



Im fairly new to programming so Im just looking for insight as to how to improve my code or if theres a better way to do this.



    package menu2;

import java.lang.reflect.Method;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;

public class Menu2 {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
private String name;
private int selection;
private int count;
private Class c;
private String menuDisplay = " ";
private Method methods;
/**
*creates menu to navigate directory list
* @throws IOException
*/

public Menu2(Object o) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this.c = o.getClass();
menuView();
selection = 0;
do {
selection = menu();
if(selection != count) {
methods[selection-1].invoke(o);
}
} while (selection != count);
}

/**
*displays menu options
* @return
*/
public void menuView() {
methods = c.getDeclaredMethods();
Method temp = new Method[methods.length];
count = 0;
for (int i = 0; i < methods.length; i++) {
System.out.println("public method: " + c.getSimpleName());
if(methods[i].getParameterCount()== 0 && !methods[i].getName().equalsIgnoreCase("iterator")&&!methods[i].getName().equalsIgnoreCase("tostring")){
menuDisplay += (count+1)+". "+methods[i].getName()+"n";
temp[count] = methods[i];
count++;
}
}
menuDisplay += count+". Quit";
methods = temp;

/*Method x = new Method[count];
for(int i = 0; i < count;i++){
x[i] = temp[i];
}
methods = x;*/
}

public int menu() {
String choiceStr;
int choice;
choiceStr = JOptionPane.showInputDialog(menuDisplay);
choice = Integer.parseInt(choiceStr);
return choice;
}
}


This menu is instantiated inside of ContactDirectory constructor which is instantiated in main.
Everything works it would seem but any advice, better code practice insight is welcome.



Driver:



package driver;
import contactdir.ContactDir;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class Driver {
public static void main(String args) throws IOException, IllegalAccessException, InvocationTargetException{
ContactDir dir = new ContactDir();
//Menu dirMenu = new Menu();
}
}


Directory:



package contactdir;

import contactinfo.ContactInfo;

import javax.swing.JOptionPane;
import jsjf.*;
import fileman.FileMan;
import java.awt.Dimension;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import menu2.Menu2;

public class ContactDir implements Iterable<ContactInfo>, Serializable {

private ArrayOrderedList<ContactInfo> list;
private FileMan<ContactInfo> fman;
private JScrollPane scroll;
private JTextArea text;
private boolean value = false;

public ContactDir() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
this.list = new ArrayOrderedList<>();
this.fman = new FileMan<>();
Menu2 menu = new Menu2(this);
}

public void addContact(){
list.add(new ContactInfo());

}

public void addContact(ContactInfo contact){
if(contact != null && list != null){

list.add(contact);
}
}

public void deleteContact(ContactInfo contact){
if(contact != null && list != null)
list.remove(contact);
}

/**
*deletes contact from list
* @param name
* @return
*/
public boolean deleteContact(String name){
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}
public boolean deleteContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to remove:");
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}

/**
*finds contact in list
* @param name
* @return
*/
public ContactInfo findContact(String name){

for(ContactInfo info : list )
{
if(info.getName().equalsIgnoreCase(name))
{
return info;
}

}
return null;
}

/**
*updates existing contact in list
* @param name
* @return
*/
public boolean updateContact(String name){

value = false;
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}
public boolean updateContact(){

value = false;
String name = JOptionPane.showInputDialog("Enter the name of the contact to update:");
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}

/**
*displays contact in list
* @param name
* @return
*/
public boolean displayContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to find:");
if(list != null){
JOptionPane.showMessageDialog(null,findContact(name).toString());
value = true;
}
else
JOptionPane.showMessageDialog(null,"Object not found");
return value;
}

/**
*displays all contacts in list
*/
public void displayDir(){

text = new JTextArea((this.toString()));
text.setLineWrap(true);
text.setWrapStyleWord(true);
scroll = new JScrollPane(text);
scroll.setPreferredSize( new Dimension( 500, 500 ) );
if(list != null)
JOptionPane.showMessageDialog(null, scroll);
}

@Override
public String toString() {
return "Contact Directory:n" + list;
}

/**
*saves list to file
* @return
* @throws IOException
*/
public boolean save() throws IOException{

fman.save("Directory.txt", list);
return value;
}

/**
*loads list from file
* @return
* @throws IOException
*/
public boolean load() throws IOException{

list = fman.load("Directory.txt");

return value;
}

@Override
public Iterator<ContactInfo> iterator() {
return list.iterator();//To change body of generated methods, choose Tools | Templates.
}

/**
* @param args the command line arguments
*/


}


PS all of this is beyond project requirements Im just trying out new things and learning.










share|improve this question




















  • 2




    If this code works fine, then this question is off topic on Stack Overflow. It may be good for our sister site Code Review, but please remember to check their rules before posting.
    – Joe C
    Nov 10 at 21:47






  • 2




    I should also warn you that posting an entire homework solution on a public website such as this may open yourself up for accusations of plagiarism from your school. Please check your school's academic honesty policy for more information.
    – Joe C
    Nov 10 at 21:52












  • Okay I will try Code Review. And this homework has already been submitted in a simpler form I just wanted to try making it better.
    – bl00dbath
    Nov 10 at 22:24













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











New here so not sure if Im doing this right.



I had to create a selection menu for a Contact Directory project for school. This is the second time Ive had to create a menu for a particular project so I recycled the menu from my previous project to accommodate this Contact Directory and all went well.



But I realized Im basically just listing the Objects methods for the user to select from in both scenarios so I got bored and decided to try to generalize the menu class I had to work with any directory-type class and this is what I came up with.



Im fairly new to programming so Im just looking for insight as to how to improve my code or if theres a better way to do this.



    package menu2;

import java.lang.reflect.Method;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;

public class Menu2 {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
private String name;
private int selection;
private int count;
private Class c;
private String menuDisplay = " ";
private Method methods;
/**
*creates menu to navigate directory list
* @throws IOException
*/

public Menu2(Object o) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this.c = o.getClass();
menuView();
selection = 0;
do {
selection = menu();
if(selection != count) {
methods[selection-1].invoke(o);
}
} while (selection != count);
}

/**
*displays menu options
* @return
*/
public void menuView() {
methods = c.getDeclaredMethods();
Method temp = new Method[methods.length];
count = 0;
for (int i = 0; i < methods.length; i++) {
System.out.println("public method: " + c.getSimpleName());
if(methods[i].getParameterCount()== 0 && !methods[i].getName().equalsIgnoreCase("iterator")&&!methods[i].getName().equalsIgnoreCase("tostring")){
menuDisplay += (count+1)+". "+methods[i].getName()+"n";
temp[count] = methods[i];
count++;
}
}
menuDisplay += count+". Quit";
methods = temp;

/*Method x = new Method[count];
for(int i = 0; i < count;i++){
x[i] = temp[i];
}
methods = x;*/
}

public int menu() {
String choiceStr;
int choice;
choiceStr = JOptionPane.showInputDialog(menuDisplay);
choice = Integer.parseInt(choiceStr);
return choice;
}
}


This menu is instantiated inside of ContactDirectory constructor which is instantiated in main.
Everything works it would seem but any advice, better code practice insight is welcome.



Driver:



package driver;
import contactdir.ContactDir;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class Driver {
public static void main(String args) throws IOException, IllegalAccessException, InvocationTargetException{
ContactDir dir = new ContactDir();
//Menu dirMenu = new Menu();
}
}


Directory:



package contactdir;

import contactinfo.ContactInfo;

import javax.swing.JOptionPane;
import jsjf.*;
import fileman.FileMan;
import java.awt.Dimension;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import menu2.Menu2;

public class ContactDir implements Iterable<ContactInfo>, Serializable {

private ArrayOrderedList<ContactInfo> list;
private FileMan<ContactInfo> fman;
private JScrollPane scroll;
private JTextArea text;
private boolean value = false;

public ContactDir() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
this.list = new ArrayOrderedList<>();
this.fman = new FileMan<>();
Menu2 menu = new Menu2(this);
}

public void addContact(){
list.add(new ContactInfo());

}

public void addContact(ContactInfo contact){
if(contact != null && list != null){

list.add(contact);
}
}

public void deleteContact(ContactInfo contact){
if(contact != null && list != null)
list.remove(contact);
}

/**
*deletes contact from list
* @param name
* @return
*/
public boolean deleteContact(String name){
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}
public boolean deleteContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to remove:");
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}

/**
*finds contact in list
* @param name
* @return
*/
public ContactInfo findContact(String name){

for(ContactInfo info : list )
{
if(info.getName().equalsIgnoreCase(name))
{
return info;
}

}
return null;
}

/**
*updates existing contact in list
* @param name
* @return
*/
public boolean updateContact(String name){

value = false;
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}
public boolean updateContact(){

value = false;
String name = JOptionPane.showInputDialog("Enter the name of the contact to update:");
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}

/**
*displays contact in list
* @param name
* @return
*/
public boolean displayContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to find:");
if(list != null){
JOptionPane.showMessageDialog(null,findContact(name).toString());
value = true;
}
else
JOptionPane.showMessageDialog(null,"Object not found");
return value;
}

/**
*displays all contacts in list
*/
public void displayDir(){

text = new JTextArea((this.toString()));
text.setLineWrap(true);
text.setWrapStyleWord(true);
scroll = new JScrollPane(text);
scroll.setPreferredSize( new Dimension( 500, 500 ) );
if(list != null)
JOptionPane.showMessageDialog(null, scroll);
}

@Override
public String toString() {
return "Contact Directory:n" + list;
}

/**
*saves list to file
* @return
* @throws IOException
*/
public boolean save() throws IOException{

fman.save("Directory.txt", list);
return value;
}

/**
*loads list from file
* @return
* @throws IOException
*/
public boolean load() throws IOException{

list = fman.load("Directory.txt");

return value;
}

@Override
public Iterator<ContactInfo> iterator() {
return list.iterator();//To change body of generated methods, choose Tools | Templates.
}

/**
* @param args the command line arguments
*/


}


PS all of this is beyond project requirements Im just trying out new things and learning.










share|improve this question















New here so not sure if Im doing this right.



I had to create a selection menu for a Contact Directory project for school. This is the second time Ive had to create a menu for a particular project so I recycled the menu from my previous project to accommodate this Contact Directory and all went well.



But I realized Im basically just listing the Objects methods for the user to select from in both scenarios so I got bored and decided to try to generalize the menu class I had to work with any directory-type class and this is what I came up with.



Im fairly new to programming so Im just looking for insight as to how to improve my code or if theres a better way to do this.



    package menu2;

import java.lang.reflect.Method;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;

public class Menu2 {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
private String name;
private int selection;
private int count;
private Class c;
private String menuDisplay = " ";
private Method methods;
/**
*creates menu to navigate directory list
* @throws IOException
*/

public Menu2(Object o) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
this.c = o.getClass();
menuView();
selection = 0;
do {
selection = menu();
if(selection != count) {
methods[selection-1].invoke(o);
}
} while (selection != count);
}

/**
*displays menu options
* @return
*/
public void menuView() {
methods = c.getDeclaredMethods();
Method temp = new Method[methods.length];
count = 0;
for (int i = 0; i < methods.length; i++) {
System.out.println("public method: " + c.getSimpleName());
if(methods[i].getParameterCount()== 0 && !methods[i].getName().equalsIgnoreCase("iterator")&&!methods[i].getName().equalsIgnoreCase("tostring")){
menuDisplay += (count+1)+". "+methods[i].getName()+"n";
temp[count] = methods[i];
count++;
}
}
menuDisplay += count+". Quit";
methods = temp;

/*Method x = new Method[count];
for(int i = 0; i < count;i++){
x[i] = temp[i];
}
methods = x;*/
}

public int menu() {
String choiceStr;
int choice;
choiceStr = JOptionPane.showInputDialog(menuDisplay);
choice = Integer.parseInt(choiceStr);
return choice;
}
}


This menu is instantiated inside of ContactDirectory constructor which is instantiated in main.
Everything works it would seem but any advice, better code practice insight is welcome.



Driver:



package driver;
import contactdir.ContactDir;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class Driver {
public static void main(String args) throws IOException, IllegalAccessException, InvocationTargetException{
ContactDir dir = new ContactDir();
//Menu dirMenu = new Menu();
}
}


Directory:



package contactdir;

import contactinfo.ContactInfo;

import javax.swing.JOptionPane;
import jsjf.*;
import fileman.FileMan;
import java.awt.Dimension;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import menu2.Menu2;

public class ContactDir implements Iterable<ContactInfo>, Serializable {

private ArrayOrderedList<ContactInfo> list;
private FileMan<ContactInfo> fman;
private JScrollPane scroll;
private JTextArea text;
private boolean value = false;

public ContactDir() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
this.list = new ArrayOrderedList<>();
this.fman = new FileMan<>();
Menu2 menu = new Menu2(this);
}

public void addContact(){
list.add(new ContactInfo());

}

public void addContact(ContactInfo contact){
if(contact != null && list != null){

list.add(contact);
}
}

public void deleteContact(ContactInfo contact){
if(contact != null && list != null)
list.remove(contact);
}

/**
*deletes contact from list
* @param name
* @return
*/
public boolean deleteContact(String name){
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}
public boolean deleteContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to remove:");
boolean value = false;
if(list != null){
deleteContact(findContact(name));
value = true;
}
return value;
}

/**
*finds contact in list
* @param name
* @return
*/
public ContactInfo findContact(String name){

for(ContactInfo info : list )
{
if(info.getName().equalsIgnoreCase(name))
{
return info;
}

}
return null;
}

/**
*updates existing contact in list
* @param name
* @return
*/
public boolean updateContact(String name){

value = false;
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}
public boolean updateContact(){

value = false;
String name = JOptionPane.showInputDialog("Enter the name of the contact to update:");
ContactInfo temp = findContact(name);
String choice = "Update Name? Update Address? Update Email? Update Work Number? Update Cell Number?";
Scanner scan = new Scanner(choice);
scan.useDelimiter("\?");
String par = "";
int confirm = 1;

while(temp != null && confirm != JOptionPane.YES_OPTION){
par = scan.next();
System.out.println("par : "+par);
confirm = JOptionPane.showConfirmDialog(null,par+"?");
}
par = par.trim();

if(par.equalsIgnoreCase("update name")){
JOptionPane.showMessageDialog(null, "Enter the name of the contact...");
String n = JOptionPane.showInputDialog("name: ");
temp.setName(n);
value = true;
}
else if(par.equalsIgnoreCase("Update Address")){
JOptionPane.showMessageDialog(null, "Enter the address of the contact...");
String n = JOptionPane.showInputDialog("address: ");
temp.setAddress(n);
value = true;
}
else if (par.equalsIgnoreCase("update email")){
JOptionPane.showMessageDialog(null, "Enter the email of the contact...");
String n = JOptionPane.showInputDialog("email: ");
temp.setEmail(n);
value = true;
}
else if(par.equalsIgnoreCase("update work number")){
JOptionPane.showMessageDialog(null, "Enter the work number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setWorknum(n);
value = true;
}
else if(par.equalsIgnoreCase("update cell number")){
JOptionPane.showMessageDialog(null, "Enter the cell number of the contact...");
String n = JOptionPane.showInputDialog("number: ");
temp.setCellnum(n);
value = true;
}
else{
System.out.println("Error");
}

return value;
}

/**
*displays contact in list
* @param name
* @return
*/
public boolean displayContact(){
String name = JOptionPane.showInputDialog("Enter the name of the contact to find:");
if(list != null){
JOptionPane.showMessageDialog(null,findContact(name).toString());
value = true;
}
else
JOptionPane.showMessageDialog(null,"Object not found");
return value;
}

/**
*displays all contacts in list
*/
public void displayDir(){

text = new JTextArea((this.toString()));
text.setLineWrap(true);
text.setWrapStyleWord(true);
scroll = new JScrollPane(text);
scroll.setPreferredSize( new Dimension( 500, 500 ) );
if(list != null)
JOptionPane.showMessageDialog(null, scroll);
}

@Override
public String toString() {
return "Contact Directory:n" + list;
}

/**
*saves list to file
* @return
* @throws IOException
*/
public boolean save() throws IOException{

fman.save("Directory.txt", list);
return value;
}

/**
*loads list from file
* @return
* @throws IOException
*/
public boolean load() throws IOException{

list = fman.load("Directory.txt");

return value;
}

@Override
public Iterator<ContactInfo> iterator() {
return list.iterator();//To change body of generated methods, choose Tools | Templates.
}

/**
* @param args the command line arguments
*/


}


PS all of this is beyond project requirements Im just trying out new things and learning.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 2:43

























asked Nov 10 at 21:42









bl00dbath

11




11








  • 2




    If this code works fine, then this question is off topic on Stack Overflow. It may be good for our sister site Code Review, but please remember to check their rules before posting.
    – Joe C
    Nov 10 at 21:47






  • 2




    I should also warn you that posting an entire homework solution on a public website such as this may open yourself up for accusations of plagiarism from your school. Please check your school's academic honesty policy for more information.
    – Joe C
    Nov 10 at 21:52












  • Okay I will try Code Review. And this homework has already been submitted in a simpler form I just wanted to try making it better.
    – bl00dbath
    Nov 10 at 22:24














  • 2




    If this code works fine, then this question is off topic on Stack Overflow. It may be good for our sister site Code Review, but please remember to check their rules before posting.
    – Joe C
    Nov 10 at 21:47






  • 2




    I should also warn you that posting an entire homework solution on a public website such as this may open yourself up for accusations of plagiarism from your school. Please check your school's academic honesty policy for more information.
    – Joe C
    Nov 10 at 21:52












  • Okay I will try Code Review. And this homework has already been submitted in a simpler form I just wanted to try making it better.
    – bl00dbath
    Nov 10 at 22:24








2




2




If this code works fine, then this question is off topic on Stack Overflow. It may be good for our sister site Code Review, but please remember to check their rules before posting.
– Joe C
Nov 10 at 21:47




If this code works fine, then this question is off topic on Stack Overflow. It may be good for our sister site Code Review, but please remember to check their rules before posting.
– Joe C
Nov 10 at 21:47




2




2




I should also warn you that posting an entire homework solution on a public website such as this may open yourself up for accusations of plagiarism from your school. Please check your school's academic honesty policy for more information.
– Joe C
Nov 10 at 21:52






I should also warn you that posting an entire homework solution on a public website such as this may open yourself up for accusations of plagiarism from your school. Please check your school's academic honesty policy for more information.
– Joe C
Nov 10 at 21:52














Okay I will try Code Review. And this homework has already been submitted in a simpler form I just wanted to try making it better.
– bl00dbath
Nov 10 at 22:24




Okay I will try Code Review. And this homework has already been submitted in a simpler form I just wanted to try making it better.
– bl00dbath
Nov 10 at 22:24

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243697%2fmenu-class-for-java-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243697%2fmenu-class-for-java-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation