Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I need help with my Business Application

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with my Business Application

    So this application gets input from the user and converts it into a string. It then uses various methods to set instance variables of this class and ends up printing those classes to display information to the user about that products.

    package javaapplication5;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    public class Product
    {
        private String product;
        private String description;
        private double price;
     
        public Product()
        {
            String product = "";
            String description = "";
            double price = 0.0;
        }
     
     
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            Product productObject = new Product();
     
            System.out.println("Enter a product for information. Choose either Java or C++");
            String productName = sc.next();
            productObject.setProductInfo(productName);
            System.out.println("Prduct: " + productObject.getProduct() + " Description: " + productObject.getDescription() + " Price " + productObject.getPrice());
     
     
        public void setProductInfo(String product)
        {
            Product productObject = new Product();
            String productName = productObject.getProduct();
            if(product.equalsIgnoreCase("java"))
            {
                productObject.setProduct("Murach's Java Programing");
                productObject.setDescription("Learn java with this fabulous book");
                productObject.setPrice(49.95);
            }
            else if(product.equalsIgnoreCase("C++"))
            {
                productObject.setProduct("Murach's C++ Programing");
                productObject.setDescription("Learn C++ with this fabulous book");
                productObject.setPrice(39.95);
            }
            else
                productObject.setProduct("Unkown");
        }
     
     
        public String getProduct() 
        {
            return product;
        }
     
     
        public void setProduct(String product) 
        {
            this.product = product;
        }
     
     
        public String getDescription() 
        {
     
            return description;
        }
     
     
        public void setDescription(String description)
        {
            this.description = description;
        }
     
     
        public double getPrice()
        {
            return price;
        }
     
     
        public void setPrice(double price) 
        {
            this.price = price;
        }
    }
    I am getting a logical error. My output is this:

    Enter a product for information. Choose either Java or C++
    java
    Prduct: null Description: null Price 0.0
    Last edited by cooljava50544; April 24th, 2014 at 12:48 PM. Reason: Fixed the old bug, found a new one. Fixed code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help with my Business Application

    Please fix the code tags. The ending one is missing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with my Business Application

    Done.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I need help with my Business Application

    In your setProductInfo() method, you are creating a new Product object and setting those values, instead of modifying the "this" Product (the "this" Product is the instance of Product which the method was called on).
    What I'm saying is: in the setProductInfo() method, get rid of this:
    Product productObject = new Product();
    String productName = productObject.getProduct();
    and every time you do something like this:
    productObject.setProduct("Murach's Java Programing");
    Instead do this:
    setProduct("Murach's Java Programing");
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with my Business Application

    Quote Originally Posted by aussiemcgr View Post
    In your setProductInfo() method, you are creating a new Product object and setting those values, instead of modifying the "this" Product (the "this" Product is the instance of Product which the method was called on).
    What I'm saying is: in the setProductInfo() method, get rid of this:
    Product productObject = new Product();
    String productName = productObject.getProduct();
    and every time you do something like this:
    productObject.setProduct("Murach's Java Programing");
    Instead do this:
    setProduct("Murach's Java Programing");
    Thanks!

Similar Threads

  1. Using Pictures with a ComboBox for my business need help
    By loui345 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2013, 07:02 PM
  2. name for project business
    By BLUEJ in forum The Cafe
    Replies: 2
    Last Post: December 19th, 2012, 11:06 AM
  3. Calculating Business days in java
    By narranil2 in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 17th, 2010, 12:08 PM

Tags for this Thread