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: need to pass objects from 3 diff methods at random to a diff class and store them in an array...

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    My Mood
    Tolerant
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question need to pass objects from 3 diff methods at random to a diff class and store them in an array...

    hi,

    in my progrm there are three diff array of objects...namely garments..gadgets and home app...now one who buys from each of these sections will have to make a bill at last...
    when he choses to make the bill he will be shown the list of products he bought and their details (like price...brand...etc)...so i thought that while he orders each product(which is done in a previous method called purchase()...)....(each product is stored as an object in there diif arrays namely garments...gadgets ...appliances)....each of those object will be copied in a new array in a diif class...then that array print will give me the desired result...

    is this approach correct...???
    and if its correct then how can i pull out a specific obj frm a stored array of object and then save it in a new array....???...



    i'm coding in core java...using inheritance...


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: need to pass objects from 3 diff methods at random to a diff class and store them in an array...

    Im sorry, but im having a hard time understanding the question. Could you try explaining the problem a bit more?

    From what i gather, you have three arrays: garments, gadgets and appliances. After that im a bit lost You want to copy the objects in each of these to a fourth array and print that? Why not just iterate each of those three arrays?

    But if you want to, then i guess one would make class for each type that extends ( inherits ) a more general class.

    So that in the end youd have

    Stuff as the parent class
    Gadget, Appliance and Garment classes that inherit the suff.
    Then you'd have ArrayList<Stuff> listOfAppliancesGadgetsAndGarnments. Since they all inherit Stuff they fit into that array just ok.

    Anyhow, hopefully i was of some help. As i mentioned, i didn't understand the question fully.

  3. The Following User Says Thank You to Wizand For This Useful Post:

    abhishek333 (June 9th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    My Mood
    Tolerant
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need to pass objects from 3 diff methods at random to a diff class and store them in an array...

    product(superclass)

    garments(sub cls of product)
    appliances(sub cls of products)


    gadgets(sub cls of app) home app(sub cls of app)


    *garments have no sub clses
    *but appliances have two more sub clses...(gadgets and home app)
    *each of the three clases garments ...gadgets ....home app...has three diff constructors....
    *now there can be multiple no. of garments ..home app...and gadgets...
    *since i'm not using file handling i have used there diff arrays to input the data....those arr runs in main func creating obj of garments...gadgets etc....
    *//loop to set data for GARMENTS.......................................... .................................


    System.out.println("Enter NO. OF RECORDS IN GARMENTS SECTION");
    n=Integer.parseInt(br.readLine());

    for(int i=0;i<n;i++)
    {
    System.out.println("Enter product code");
    c=Integer.parseInt(br.readLine());
    System.out.println("Enter product price");
    p=Double.parseDouble(br.readLine());
    System.out.println("Enter product stock available");
    s=Integer.parseInt(br.readLine());
    System.out.println("Enter product brand");
    b=br.readLine();
    System.out.println("Enter product description");
    d=br.readLine();
    System.out.println("Enter product size ");
    sz=Integer.parseInt(br.readLine());

    G[i]=new garments(c,p,s,b,d,sz);

    }


    *G[i] is the array to set garments data..similarly there are GA[]...hom[]..arrays to set data for other objects...

    *now if i buy a product from garments..then from gadgets...i have to run separate loops...
    ********
    my prob is i have done the buying part...but i want to print the list of product i have bought....just like actual cash memo looks like...
    so now i have to extract data frm garments array..then a gadgets array...then copy it to a separate(new array)...then print that array...i cant do this part...how can i extract two diff objects(with diff variables...) and save it ...a make a new array of objects...

    hopefully u can get my point...it was my first post..so it will take time to be concise and appropiate...
    newayz even if u wont get my point ...thanks 4 a response...!!!

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: need to pass objects from 3 diff methods at random to a diff class and store them in an array...

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    abhishek333 (July 15th, 2014)

  7. #5
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: need to pass objects from 3 diff methods at random to a diff class and store them in an array...

    Hi again,

    I still might be a little in the dark with the issue, there being a language barrier of sorts but ill try my best

    First of all, welcome to the forums. Just follow the mod's instructions and you'll fit in fine, i am sure.

    I think you should pay a little more attention to your naming conventions. I assume G[] is an array of garments. In Java, it is *very* advisable to use the camel case and other agreed-upon naming practices! If only to be able to clearly and unambiguously rely your code to other Java -programmers. You can google "java naming conventions" and you shall find tons of pages on the issue. Here's one: Using Java Naming Conventions.

    Anyway, to the point. If it as you said, that Garment and Appliance inherit Product, then HomeAppliance and Gadget inherit Appliance, that means all of your objects are Products as well. You can use Product as the type of your collection and add all objects to it. One thing you should read about is ArrayList and other Collections in Java. They are way more flexible to use that raw arrays. You could also override the toString -method of each class to print each instance as you'd like. Ill write a small example here, hopefully i have understood your issue even remotely correct .

    import java.util.ArrayList;
     
    public class ProductExample {
     
    	public static void main(String[] args) {
     
    		//You mentioned that you have diffirent array for diffirent objects, trying to copy that here:
    		Appliance[] gadgets = new Appliance[3];
    		Appliance[] homeAppliances = new Appliance[3];
    		Garment[] garments = new Garment[3];
     
    		// Now creating some example test data. Im using the raw lists instead of ArrayLists because in your posted code
    		// you didnt either. But you probably should :)
    		for ( int i = 0; i < 3; i++ ) {
    			gadgets[i] = new Gadget("gadget_" + i, 20d+i);
    			homeAppliances[i] = new HomeAppliance("homeApp_" + i, 30d+i);
    			garments[i] = new Garment("garment_" + i, + 24d+i);
    		}
     
    		//Now creating the *Product* ArrayList that can take all of the above:
    		ArrayList<Product> allProductsList = new ArrayList<Product>();
     
    		//There are better ways of filling the products list and iterating thru the raw lists but for the sake of the example 
    		//im using the long way to illuminate the point
    		//So, going thru all the diffirent arrays and adding them to the products list:
    		for ( int i = 0; i < gadgets.length; i++) {
    			allProductsList.add(gadgets[i]); 
    		}
    		for ( int i = 0; i < homeAppliances.length; i++ ) {
    			allProductsList.add(homeAppliances[i]);
    		}
    		for ( int i = 0; i < garments.length; i++ ) {
    			allProductsList.add( garments[i] );
    		}
     
    		//At this point we have all the different products in a single array and we can just go through it and use the overwritten toString.
    		//Using for-each here. 
    		for ( Product currentProduct : allProductsList ) {
    			System.out.println(currentProduct.toString());
    		}
    	}
    }
     
    	//The classes
    	/**
    	 * The mother class of all products.  
    	 */
    	class Product {
    		//Some instance variables for the example
    		String name;
    		Double price;
     
    		//Constructor to set the variables
    		public Product(String name, Double price) {
    			this.name = name;
    			this.price = price;
    		}
     
    		//Overriding the toString method for custom printing
    		@Override
    		public String toString() {
    			return "I am a " + this.getClass().getName() +  ". My name is " + name + " and i cost " + price;
    		}
    	}
     
    	//Appliance that extends Product and then Gadget and HomeAppliance that extend the Appliance class.
    	class Appliance extends Product {
    		public Appliance(String name, Double price) {
    			super(name, price);
    		}
    	}
    	class Gadget extends Appliance {
    		public Gadget(String name, Double price) {
    			super(name, price);
    		}
    	}
    	class HomeAppliance extends Appliance {
    		public HomeAppliance(String name, Double price) {
    			super(name, price);
    		}
    	}
     
    	// The garment class that isnt Appliance but is a product.
    	class Garment extends Product{
    		public Garment(String name, Double price) {
    			super(name, price);
    		}
    	}

    The output should be:
    I am a main.Gadget  and my name is gadget_0 and i cost 20.0
    I am a main.Gadget  and my name is gadget_1 and i cost 21.0
    I am a main.Gadget  and my name is gadget_2 and i cost 22.0
    I am a main.HomeAppliance  and my name is homeApp_0 and i cost 30.0
    I am a main.HomeAppliance  and my name is homeApp_1 and i cost 31.0
    I am a main.HomeAppliance  and my name is homeApp_2 and i cost 32.0
    I am a main.Garment  and my name is garment_0 and i cost 24.0
    I am a main.Garment  and my name is garment_1 and i cost 25.0
    I am a main.Garment  and my name is garment_2 and i cost 26.0

    Hope you got something out of this . Good luck!

  8. The Following User Says Thank You to Wizand For This Useful Post:

    abhishek333 (July 15th, 2014)

Similar Threads

  1. Replies: 1
    Last Post: August 8th, 2012, 11:46 PM
  2. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  3. How to store objects from a class inn an array?
    By dironic88 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 7th, 2011, 02:42 PM
  4. Diff between spring created been and instance of class
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 12:54 AM
  5. Diff Bw JREvs JDKvs JVM
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: December 23rd, 2009, 08:03 AM

Tags for this Thread