3 Replies - 942 Views - Last Post: 03 December 2012 - 07:58 PM Rate Topic: -----

#1 mindlessjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-February 12

Abstract classes and outputs

Posted 24 April 2012 - 01:26 PM

I'm new to inheritance and abstract classes.

For this example I'm designing a phone book that can have a person or a business. The person has a title, first name, last name, and a phone number. The business has a business name and a phone number.
I created an abstract class with abstract method getName
(This may sound really simplistic to you guys but bear with me!)
public  abstract class PhoneBook {
	
	private String phone;

	public boolean setPhone(String p) //final
	{
		boolean flag = false;
		if(p.length()!= 10)
		{
			flag = false;
		}

		for (int i = 0; i < p.length(); i++)
		{
			if(Character.isDigit(p.charAt(i)))
			{
				phone = p;
				flag = true;
			}
		}
		return flag;
	}

	public abstract String getName();

}



My two subclasses are Person and Business. The person's getName method concatenates the title, f name, l name.
In the main I created a PhoneBook array (abstract array) that can hold both a person or a business.

I'm having difficulties with the output...how do I access getPhone (in the abstract class) to output it?

This is the main (I'm currently only working on the person part)

import javax.swing.*;
public class PhoneBookEntries {


	public static final int MAX = 100;

	public static void main(String[] args) {

		PhoneBook[] phone = new PhoneBook[MAX];
		int selection;
		int i = 0;

		do{
			selection = Integer.parseInt(JOptionPane.showInputDialog("Would you like to add a\n1.person\n2.business\nto the phone book?"));

		switch(selection)
		{
		case 1: phone[i]= fillPerson();
			break;
		case 2: phone[i] = fillBusiness();
			break;
		}

		}while(i < MAX && JOptionPane.showConfirmDialog(null, "Add another entry to phone book?")==JOptionPane.YES_OPTION);

		[b]//output
		String output;
		output = phone[i].getName(); //how do i include the phone number?
		JOptionPane.showMessageDialog(null, output);[/b]

	}

	private static PhoneBook fillPerson()
	{
		Person someone = new Person();
		someone.setTitle(JOptionPane.showInputDialog("Enter your title\n(Mr., Mrs., Ms., or Dr.)"));
		someone.setFName(JOptionPane.showInputDialog("Enter the first name of the person: "));
		someone.setLName(JOptionPane.showInputDialog("Enter the last name of the person: "));
		while(!someone.setPhone(JOptionPane.showInputDialog("Enter your 10 digit phone number: ")))
			JOptionPane.showMessageDialog(null, "Error. Please enter only 10 numerical values\n(examle: 7034567890");
		return someone;
	}

	private static void fillBusiness()
	{

	}

}



getName I can easily access because I have PhoneBook[]. I was thinking I need a toString in Person to mush everything together (the while name and the phone number) but then in the main I can't access that toString because I don't have a Person instantiated?
Sorry if this is confusing...I'm just typing out my (poor) train of thought...

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract classes and outputs

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Abstract classes and outputs

Posted 24 April 2012 - 01:41 PM

View Postmindlessjava, on 24 April 2012 - 04:26 PM, said:

I'm having difficulties with the output...how do I access getPhone (in the abstract class) to output it?

You shouln'd and it would be a very bad design
Your abstarct class shouldn't know nothing about its descendant so it shouldn't know that Personal and Business exist at all. What if you add International ? You would have to change your abstarct clas. This is a NO NO !!!

Your abstarct class can have an abstract print() method that the descendants will have to implement.

An I couldn't resist in rewriting you phone method in a more efficient/simple way


public boolean setPhone(String p) //final
    {
        if(p.length()!= 10)
           return false;
        
        for (int i = 0; i < p.length(); i++)
        {
            if(!Character.isDigit(p.charAt(i)))
                return false;
        }
        phone = p;
        return true;
    }


Was This Post Helpful? 0
  • +
  • -

#3 xkograjml  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 12

Re: Abstract classes and outputs

Posted 03 December 2012 - 07:55 PM

Howdy! This is my 1st comment here so I just wanted to give a quick shout out and tell you I
truly enjoy reading your posts. Can you recommend any other blogs/websites/forums that deal
with the same subjects? Thanks! red bottoms
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Abstract classes and outputs

Posted 03 December 2012 - 07:58 PM

Ouf... that post is 5 months old and you just wake up ???
We usually reply faster :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1