8 Replies - 292 Views - Last Post: 16 April 2013 - 09:12 AM Rate Topic: -----

#1 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 01:06 AM

Ok, This is a topic that i am doing in Uni, what it is when i craete methods and i need them to print something i use the System.out.print or println, Now my tutor has said that i should now stop using these as if i was to upgrade my program to a GUI driven one the print statements wont work.

So with this code now:

    public void addStudent(Student aStudent)
    {
        Iterator<Student> addIterator = studentList.iterator();
        
        while (addIterator.hasNext())
        {
            Student ob = addIterator.next();
            
            if (ob.getStudentID() == aStudent.getStudentID())
            {
               
                System.out.println("This Student ID "
                        + ""+ aStudent.getStudentID()+ " Is Already Used"
                        + "\n Try Adding Again......");
                return;
            }
        }
        
        
        System.out.println("\nStudent "+ aStudent.getForeName() + " "
                + "" + aStudent.getSurName() +" "
                + "Successfully Added To System.....");
       
        studentList.addLast(aStudent);
    }



If the user enteres an object that already exixts it prints out an error, and when the student has been added it confirms that.

Now how would i do this without using print statements and without changing the method layout:
public void addStudent(Student aStudent) 


Is This A Good Question/Topic? 0
  • +

Replies To: Methods Without System.out.print... How would I do this?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,433
  • Joined: 20-September 08

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 01:39 AM

JOptionPane.showMessageDialog
http://docs.oracle.c...a.awt.Component,%20java.lang.Object)

This post has been edited by g00se: 16 April 2013 - 01:39 AM
Reason for edit:: link

Was This Post Helpful? 0
  • +
  • -

#3 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 06:19 AM

Without having a message box being opened??
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,433
  • Joined: 20-September 08

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 07:18 AM

The code i posted DOES open a message box - that's the point. If you mean without having a window open, as in 'my app's a console app' then pass null for the first argument

This post has been edited by g00se: 16 April 2013 - 07:19 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1992
  • View blog
  • Posts: 4,140
  • Joined: 11-December 07

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 07:25 AM

This is going to sound really blunt but you do it by deleting all the println statements:

public void addStudent(Student aStudent)
{
    Iterator<Student> addIterator = studentList.iterator();
    while (addIterator.hasNext())
    {
        Student ob = addIterator.next();
        if (ob.getStudentID() == aStudent.getStudentID())
        {
            return; // or even better, throw an exception/
        }
    }
    studentList.addLast(aStudent);
}


This method will either add the student or will avoid duplicates. If you are doing this for real you would use a Set instead of a List because this will prevent duplicated for you. But this is for class so let's keep improving it...

Each method should do one thing and do it well. Let's split things up a bit and the whole lot more readable.

public void addStudent(Student s) {
    if (!hasStudent(s)) {
        studentList.addLast(s);
    }
}

private boolean hasStudent(Student s) {
    return hasStudent(s.getStudentID());
}

private boolean hasStudent(int id) {
    //assunming id is an int...
    for(Student s : studentList) {
        if (s.getStudentID() == id) {
            return true;
        }
    }
    return false;
}


You could argue if that middle method is really necessary. It's not. You could merge the last two if you really wanted.

I promised to get your output back. You shouldn't mix that stuff in with the application logic. The caller should sort that out for you. Collections often return a boolean saying if their operation was successful. Let's implement that here. The caller can use that value to display the appropriate message.

// These methods should be in a user interface class.
public void verboseAddStudent(Student s) {
    boolean isSuccessful = addStudent(s);
    if (isSuccessful) {
        display("Successfully added " + s.getForename() + " " + s,getSurname());
    } else {
        display("Student ID " + s.getStudentID() + " is already in use.")
    }
}

private void display(Object s) {
    // Write to the console or update a GUI.
    System.out.println(s);
}

// These methods should be in a different class
public boolean addStudent(Student s) {
    if (hasStudent(s)) {
        return false;
    }
    studentList.addLast(s);
    return true;
}

private boolean hasStudent(Student s) {
    return hasStudent(s.getStudentID());
}

private boolean hasStudent(int id) {
    //assunming id is an int...
    for(Student s : studentList) {
        if (s.getStudentID() == id) {
            return true;
        }
    }
    return false;
}


One thong to note is that my code is a lot longer than yours for the same job. However, I think it's more readable, flexible and maintainable.
Was This Post Helpful? 0
  • +
  • -

#6 parry2411  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 07:31 AM

Thanks, ill try this. But just printing using the System.out is much easier. But i have to move on to doing this somehow so i can start implementing GUIs in to my program.

Thanks Again
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1992
  • View blog
  • Posts: 4,140
  • Joined: 11-December 07

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 07:39 AM

No problem. The issue is wider than println. Methods should do one thing and no more. Classes should have one responsibility and no more. If you have println scattered throughout your application logic then your methods are doing more than one thing.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,433
  • Joined: 20-September 08

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 07:40 AM

Rather than hardcoding null as the first parameter, use an instance variable of type Component and set it to null for the time being

private Component messageBoxParent = null; // assignment redundant but made for self-documentation reasons

This post has been edited by g00se: 16 April 2013 - 07:42 AM
Reason for edit:: code

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: Methods Without System.out.print... How would I do this?

Posted 16 April 2013 - 09:12 AM

Ideally, you'd change the sig on the method to let the user know it worked:
public boolean addStudent(Student aStudent) 


The message is then handled by whatever object is talking to your method. e.g.
class ConsoleUI {
	private StudentMgr mgr;
	
	private void addStudent(Student aStudent) {
		if (mgr.addStudent(aStudent)) {
			System.out.println("Student " + aStudent.getForeName() 
			+ " " + aStudent.getSurName() +" "
			+ "Successfully Added To System.....");
		} else {
			System.out.println("This Student ID "
				+ ""+ aStudent.getStudentID()+ " Is Already Used"
				+ "\n Try Adding Again......");
			}
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1