3 Replies - 1704 Views - Last Post: 16 March 2013 - 05:31 AM Rate Topic: -----

#1 DJ2334   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-February 13

Help with abstract method!

Posted 16 March 2013 - 04:38 AM

My abstract method (abstract String getDisplayText();) in the Product class is supposed to make my program display another message for the author that is shown in the Book class and Product DB class, but the messages aren't displaying when I run my program.

When it currently runs all I get is:

Quote

Welcome to the Product Selector

Enter product code: java

Code: java
Description: Murach's Beginning Java
Price: $49.50

Product count: 1

Continue? (y/n):


This is what I want it to display:


Quote

Welcome to the Product Selector

Enter product code: java

Code: java
Description: Murach's Beginning Java
Price: $49.50
Author: Andrea Steelman


Product count: 1

Continue? (y/n):


This is the superclass:

package a190.pkg06;

import java.util.Scanner;

public class ProductApp
{
    public static void main(String[] args) 
    {
        // display a welcome message
        System.out.println("Welcome to the Product Selector");
        System.out.println();
        
        // perform 1 or more selections
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
            System.out.print("Enter product code: ");
            String productCode = sc.next();  // read the product code
            sc.nextLine();  // discard any other data entered on the line
            
            // get the Product object
            Product p = ProductDB.getProduct(productCode);
            
            // display the output
            System.out.println();
            if (p != null)
                System.out.println(p);
            else
                System.out.println("No product matches this product code.\n");
            
            System.out.println("Product count: " + Product.getCount() + "\n");
            
            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.nextLine();
            System.out.println();
        }
    }
}



The product class:

package a190.pkg06;

import java.text.NumberFormat;

public abstract class Product 
{
    private String code;
    private String description;
    private double price;
    protected static int count = 0;
    
    public Product() 
    {
        
    }
    
    public void setCode(String code)
    {
        this.code = code;
    }
    public String getCode()
    {
        return code;
    }
    
    public void setDescription(String description)
    {
        this.description = description;
    }
    public String getDescription()
    {
        return description;
    }
    
    public void setPrice(double price)
    {
        this.price = price;
    }
    public double getPrice()
    {
        return price;
    }
    public String getFormattedPrice()
    {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(price);
    }
    
    // regular constructors and methods for instance variables
    
    @Override
    public String toString()
    {
        return "Code: " + code + "\n" +
               "Description: " + description + "\n" +
               "Price: " + this.getFormattedPrice() + "\n";
    }
    
    @Override
    public boolean equals(Object object)
    {
        if (object instanceof Product)
        {
            Product product2 = (Product) object;
            if
            (
                code.equals(product2.getCode()) &&
                description.equals(product2.getDescription()) &&
                price == product2.getPrice()
            )
                return true;
        }
        return false;
    }
    public static int getCount()
    {
        return count;
    }
    abstract String getDisplayText();  // an abstract method
}



The classes that displays a message for getDisplayText

package a190.pkg06;

public class Book extends Product 
{
    private String author;
    
    public Book()
    {
        super();
        author = "";
        count++;
    }
    
    public void setAuthor(String author)
    {
        this.author = author;
    }
    public String getAuthor()
    {
        return author;
    }
    
    // regular constructor and methods for the Book class
    
    @Override
    public String getDisplayText()  // implement the abstract method
    {
        return super.toString() +
                "Author: " + author + "\n";
    }
}



ProductDB Class:

package a190.pkg06;

public class ProductDB
{
  public static Product getProduct(String productCode)
  {
      // In a more realistic application, this code would 
      // get the data for the product from a file or database
      // For now, this code just uses if/else statements
      // to return the correct product data
      
      Product p = null;
      
      if (productCode.equalsIgnoreCase("java") ||
         productCode.equalsIgnoreCase("jsps") ||
         productCode.equalsIgnoreCase("mcb2"))
      {
          Book b = new Book();
          if (productCode.equalsIgnoreCase("java"))
          {
              b.setCode(productCode);
              b.setDescription("Murach's Beginning Java");
              b.setPrice(49.50);
              b.setAuthor("Andrea Steelman");
          }
          else if (productCode.equalsIgnoreCase("jsps"))
          {
              b.setCode(productCode);
              b.setDescription("Murach's Java Servlets and JSP");
              b.setPrice(49.50);
              b.setAuthor("Andrea Steelman");
          }
          else if (productCode.equalsIgnoreCase("mcb2"))
          {
              b.setCode(productCode);
              b.setDescription("Murach's Mainframe COBOL");
              b.setPrice(59.50);
              b.setAuthor("Mike Murach");
          }
          p = b; // set Product variable equal to the Book object
      }
      return p;
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with abstract method!

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Help with abstract method!

Posted 16 March 2013 - 04:50 AM

You never call the getDisplayText anywhere.

This post has been edited by sepp2k: 16 March 2013 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Help with abstract method!

Posted 16 March 2013 - 05:07 AM

It's cool if you just want to System.out.println(p);. What's really happening there is System.out.println(p.toString());. So, the toString method is what you want to override. You don't need display.

Also, use a contructor! No setters! You don't need them and your code could be a quarter the size it is.
Was This Post Helpful? 0
  • +
  • -

#4 DJ2334   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-February 13

Re: Help with abstract method!

Posted 16 March 2013 - 05:31 AM

View Postbaavgai, on 16 March 2013 - 05:07 AM, said:

It's cool if you just want to System.out.println(p);. What's really happening there is System.out.println(p.toString());. So, the toString method is what you want to override. You don't need display.

Also, use a contructor! No setters! You don't need them and your code could be a quarter the size it is.


I figured it out. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1