Results 1 to 11 of 11
Thread: Storing in an Array
- 10-13-2009, 03:27 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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. 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:public void setOrderProduct
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; } }
- 10-13-2009, 04:16 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-13-2009, 08:18 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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 }
- 10-14-2009, 04:27 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
My concern is this.
how you going to handle this? What's the logic you have in your mind.//IF MORE PRODUCTS ARE ADDED, YOU HAVE TO RESIZE THE
//orderProduct and orderQuantity arrays.
- 10-14-2009, 05:11 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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)
- 10-14-2009, 08:14 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-14-2009, 09:01 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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.
- 10-14-2009, 09:15 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-14-2009, 03:08 PM #9gcampton Guest
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.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; }
Vehicle = the objects for the array named brokenArray.
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...Java Code:if(brokenArray.length >= Count) { brokenArray[0]=addElements(brokenArray[0]); }Last edited by gcampton; 10-14-2009 at 03:19 PM.
- 10-15-2009, 01:14 AM #10
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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.
- 10-15-2009, 05:12 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Storing high score and sorting the array
By Implode in forum New To JavaReplies: 8Last Post: 09-28-2009, 12:43 AM -
storing a string in an array
By tiyani in forum New To JavaReplies: 3Last Post: 08-12-2009, 07:25 PM -
Storing Files
By superwaxer in forum Advanced JavaReplies: 6Last Post: 05-07-2009, 02:37 PM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM -
Storing Array from HTTP Post
By kskinner in forum New To JavaReplies: 1Last Post: 09-08-2008, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks