I am a beginner in java....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: I am a beginner in java....

  1. #1
    Join Date
    Nov 2012
    Posts
    1

    Unhappy I am a beginner in java....

    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");

    }





    }




    }


    For now my code only finds whether the item is available or not. But what i want to do is to print out the name of the item found along with its quantity and cost.
    Please help me with this. I have been struggling with it for a long time

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: I am a beginner in java....

    Why is your ItemFound method creating several items. A method should do exactly what it's name suggests it should do, nothing more and nothing less. By all means have a method that initialises your data but call it something that says that's what it is doing.

    Also why are you passing values into the constructor of each item and then immediately changing the values with setXXX, why not pass the correct values into the constructor to start with. And why are you calling getXXX on all your items and ignoring the returned values?

    If you want to print out the details of the item then you need a method that takes the item name as a parameter and returns the item with that name. Once you have the item you can then use the getXXX methods to extract the details from it. Remember to handle the case where someone asks for an item you don't have. In such a case your method can either return null or possibly throw an exception, which will need handling in the calling code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center