Results 1 to 11 of 11
  1. #1
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default Storing in an Array

    Hey guys,

    Hopefully here I can find some "simple" help for someone fairly new to java like me. Other forums people were helpful its just that I still find it hard to understand some of the concepts they are giving me. I'm working on a program with 9 classes, I've filled most of the object classes according to what I've been taught so far but I am stuck on this class:

    Particular I'm stuck on the method called
    Java Code:
    public void setOrderProduct
    . If someone could please look at the TODO in that method and give me an idea (in simple terms if possible.. hehe) on what needs to be done I'd REALLY appreciate it. I'm trying to get an early start on this project I know I'm gonna need LOTS of time.

    Java Code:
    import java.text.NumberFormat;
    import java.util.Arrays;
    
    public class Order {
    	//class variables
    	private Customer orderCustomer;
    	private Clerk orderClerk;
    	private Product[] orderProduct;
    	private int[] orderQuantity;
    	
    	//store totals
    	private double subtotal;
    	private double tax;
    	private double total;
    	
    	//constant defining tax
    	private static double TAX_RATE = 0.0825;
    
    	//keep track of how many products were added
    	private int productCount;
    	
    	//constructor
    	public Order(){
    		orderProduct = new Product[1];
    		orderQuantity = new int[1];
    	}
    	
    	//setter to assign customer
    	public void setOrderCustomer(Customer aCustomer){
    		orderCustomer = aCustomer;
    	}
    	
    	//setter to assign clerk
    	public void setOrderClerk(Clerk aClerk){
    		orderClerk = aClerk;
    	}
    	
    	public void setOrderProduct(Product aProduct, int aQty){
    		//TODO
    		//EACH TIME A USER ADDS A PRODUCT TO THE ORDER
    		//IF IT IS THE FIRST PRODUCT ADDED TO THE ORDER THEN
    		//STORE IT IN THE orderProduct ARRAY
    		
    		
    		//IF MORE PRODUCTS ARE ADDED, YOU HAVE TO RESIZE THE 
    		//orderProduct and orderQuantity arrays.
    		
    		//the way to do that is to create temp array for each
    		//copy the current arrays into temp arrays
    		//resize the current arrays
    		//put back the temp arrays in the current arrays
    		//in the newly sized arrays
    		//add the new product
    		//add the quantity
    	}
    	
    	public void calcSubtotal(){
    		//TODO
    		//FOR LOOP THROUGH THE orderProduct array
    		//get the price
    		//get the quantity from the orderQuantity array
    		//STORE IT IN subtotal VARIABLE
    		
    	}
    	
    	public void calcTax(){
    		//TODO
    		//CALCULATE THE TAX
    		//STORE IT IN tax VARIABLE
    	}
    	
    	public void calcTotal(){
    		//TODO
    		//CALCULATE THE TOTAL
    		//STORE IT IN total VARIABLE
    	}
    
    	public String toString(){
    		NumberFormat nf =  NumberFormat.getCurrencyInstance();
    		
    		String result = "";
    		
    		result += "CASHIER @ REGISTER\n " + orderClerk.getFirstName() + " " + orderClerk.getLastName() +" @ " + orderClerk.getRegisterNbr() + "\n\n";
    		
    		//TODO
    		//ADD REST OF SUMMARY TO RESULT
    		//SEE LINE ABOVE FOR EXAMPLE OF HOW TO GET INFORMATION FROM 
    		//OTHER OBJECTS THAT ARE AVAILABLE WITHIN THE ORDER CLASS
    		//SEE PROJECT HANDOUT TO GET IDEA OF HOW YOUR SUMMARY SHOULD LOOK LIKE
    		
    		
    		return result;
    	} 
    	
    	//Returns the number of items sold
    	public int getNumberItemsSold(){
    		int totalQty = 0;
    		for (int i = 0;i<orderQuantity.length;i++){
    			totalQty += orderQuantity[i];
    		}
    		
    		return totalQty;
    	}
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just looking into your comment on that method, you want to increase the size gradually. So how can you you that using Arrays. Why don't you use ArrayList, using Arrays you cannot increase the size, each time you've redefine it with new size. It's not a good idea in terms or memory allocation and deallocation. ArrayList will do that for you.

  3. #3
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Unfortunately our professor wants to do it a certain way, so far this is what I've got:

    Java Code:
    public void setOrderProduct(Product aProduct, int aQty){
    		
    		for (int i=0; i < orderProduct.length; i++){
    			if (orderProduct[i].getSku().compareTo(aProduct.getSku())==0);  //added
    		}
    		
    		//TODO
    		//EACH TIME A USER ADDS A PRODUCT TO THE ORDER
    		//IF IT IS THE FIRST PRODUCT ADDED TO THE ORDER THEN
    		//STORE IT IN THE orderProduct ARRAY
            
    		
    		//IF MORE PRODUCTS ARE ADDED, YOU HAVE TO RESIZE THE 
    		//orderProduct and orderQuantity arrays.
    		
    		//the way to do that is to create temp array for each
    		//copy the current arrays into temp arrays
    		//resize the current arrays
    		//put back the temp arrays in the current arrays
    		//in the newly sized arrays
    		//add the new product
    		//add the quantity
    	}

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    My concern is this.

    //IF MORE PRODUCTS ARE ADDED, YOU HAVE TO RESIZE THE
    //orderProduct and orderQuantity arrays.
    how you going to handle this? What's the logic you have in your mind.

  5. #5
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    The teacher told us it will be along the lines of creating a temp array copying the values of the original array into the temp and then expanding the array (not that I know specifically how to do this or anything)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Start by writing a private helper method getProductPosition(Product product).
    The method look at the array of products and return the index of the supplied product in the array. Make it return -1 if the product is not in the array.
    You then use that method in your setOrderProduct method.

  7. #7
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Hmm I don't think we'll need any other methods, professor has it outlined in such a way that we should only have to fill in the to-do's. I even asked if I could use an arrayList and he says arrays only.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I think you are wrong. I don't think you are limited to those methods only. You can still use that one method if you want but it would not be good practice. So the first part of the method is to locate the index of the supplied product in the array. Write that part first.

  9. #9
    gcampton Guest

    Default

    Java Code:
    public Vehicle[] addElements(Vehicle[]d)
    {
        Vehicle[] tempArray = new Vehicle[d.length+1];
        for(int i=0;i<d.length;i++)
        {
            tempArray[i]=d[i];
        }
        d=tempArray;
        return d;
    }
    Hopefully that helps ^^, this is the method I use, not that it's invoked but it's there just incase it is needed if your arrays are constantly changing then you should be using arraylist, when creating your array simply do a check for size before adding elements.
    Vehicle = the objects for the array named brokenArray.
    Java Code:
    if(brokenArray.length >= Count)
    {
        brokenArray[0]=addElements(brokenArray[0]);
    }
    EDIT: note that's for a multidimensional array to add 1 element to 1 row, hence the numbering of element so typically would be invoked by brokenArray[CAR]=addElements, for a standard array take out element numbers eg. brokenArray[]=addElements or perhaps brokeArray=addElements... can't remember...
    Last edited by gcampton; 10-14-2009 at 04:19 PM.

  10. #10
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Thanks guys I'll need some time to look over that code to really understand it.

    I e-mailed the teacher and to my dissapointment we cannot use arrayList, I believe he'd like us to stick with his outline as much as possible to learn the concepts we're currently working on.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you really want to stick with arrays, you can do the following. I've done the same thing several times.

    Initialize an array with new size you want. Then use the System.arraycopy() to copy the existing array into the new. You have to define several parameters correctly, like starting index to copy and so on. Then you've to fill the rest of the array in proper way.

    Look at the following code segment.

    Java Code:
        public static void main(String[] args) {
            int[] iArray = new int[5];
    
            // Fill the array
            for(int i = 0; i < iArray.length; i++) {
                iArray[i] = i;
            }
    
            // Temp array
            int[] temp = new int[iArray.length + 3];
    
            // Copy array
            System.arraycopy(iArray, 0, temp, 0, iArray.length);
    
            for(int j = iArray.length; j < temp.length; j++) {
                temp[j] = 0;
            }
    
            for(int index = 0; index < temp.length; index++) {
                System.out.println(temp[index]);
            }
        }

Similar Threads

  1. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 01:43 AM
  2. storing a string in an array
    By tiyani in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 08:25 PM
  3. Storing Files
    By superwaxer in forum Advanced Java
    Replies: 6
    Last Post: 05-07-2009, 03:37 PM
  4. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:32 AM
  5. Storing Array from HTTP Post
    By kskinner in forum New To Java
    Replies: 1
    Last Post: 09-08-2008, 07:00 AM

Posting Permissions

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