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 2 of 2

Thread: Problem with printing appropriate information of the item

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with printing appropriate information of the item

    public class Item
    {


    /**
    * @param args
    */
    /**
    * @param args
    */


    private String name;
    private double cost;
    private int qty;

    public Item(String name, double cost, int qty) {
    this.name = name;
    this.cost = cost;
    this.qty = qty;

    }

    public String getName() {
    return name;
    }

    public void setName(String Itemname) {
    name = Itemname;
    }

    public double getCost() {
    return cost;
    }

    public void setCost(double d) {
    cost = d;

    }

    public int getQty() {
    return qty;
    }

    public void setQty(int ItemQty) {
    qty = ItemQty;
    }



    public static boolean ItemFound()throws FileNotFoundException
    {

    @SuppressWarnings("resource")
    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();
    Item[]items = new Item[4];
    items[0]= new Item("n",2.2,4);
    items[0].setName("Oreo");
    items[0].setCost(6.00);
    items[0].setQty(10);

    items[1]= new Item("n",2.2,4);
    items[1].setName("Rice");
    items[1].setCost(3.99);
    items[1].setQty(20);

    items[2]= new Item("n",2.2,4);
    items[2].setName("Milk");
    items[2].setCost(0.79);
    items[2].setQty(75);

    items[3]= new Item("n",2.2,4);
    items[3].setName("Flour");
    items[3].setCost(2.49);
    items[3].setQty(5);


    items[0].getName();
    items[0].getCost();
    items[0].getQty();


    items[1].getCost();
    items[1].getName();
    items[1].getQty();

    items[2].getName();
    items[2].getCost();
    items[2].getQty();

    items[3].getName();
    items[3].getCost();
    items[3].getQty();

    for(int i=0; i<items.length; i++)
    {



    if(input.equals(items[i].getName()))
    {


    return true;

    }


    }

    return false;

    }


    public static void main(String[] args) throws FileNotFoundException
    {
    //Array array = new Array();
    System.out.println("Please Enter the Item you Would Like to Purchase");
    //Item[]items = new Item[4];
    Item itm = new Item("d", 2.2, 3);


    if(itm.ItemFound())

    {
    System.out.println("You are lucky, we found your Item.");


    }

    else
    {
    System.out.println("Did not find your item");

    }





    }




    }


    Right now it only prints whether the item is found or not but please let me know how I can print the found item along with the the name, quantity and the cost. I have tried many ways but haven't been successful... Please help me with this.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with printing appropriate information of the item

    First thing, please wrap your code in code tags so that it could be readable. Also, you have the setters and getters in your class, why don't you use them to get the name, quantity and cost of that specific item?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. Replies: 1
    Last Post: July 25th, 2011, 01:33 PM
  2. Error In Printing. Perhaps a nesting problem
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 23rd, 2011, 08:46 AM
  3. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 04:22 AM
  4. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 08:37 PM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 07:53 PM