This is the part of the code for the Sales option, i modified based on the array that u created for me. As you can see in the Main Display part of the code, i am unable to make use of the Forloop because this affected the alignment of the Main Display.
i try to put some of almost repeated task in Class Method but unsuccessfully. Can u tell me how to implement it?
This looks much better. Let me start with a couple of quick questions. Why do you need parameters for totalCostPrice, totalSellingPrice and difference? Can't you calculate these from the other parameters like below?
Based on your other question, it looks like that might be the way to go. You could eliminate three parameters and three instance variables.
I'm still unclear on what you are trying to accomplish. Do you need to be able to simulate selling some of the product so the inventory decreases and the number sold increases?
If so, then you need mutator methods in your Product class. I'm making a guess that this might be close to what you need.
publicvoid sellProduct(int quantitySold){
prdQtySold -= quantitySold;
prdQtyPrepared += quantitySold;
//If you do NOT follow Adam Duffy's approach (second block of code above), then you'll probably need to put a copy the first block here.
//Since I'm not sure what your goal is, it is hard for me to advise you.
}
If you put in the suggestions that I made in the previous post, then the code that you are having problems with can be modified as follows:
if(prdCode.equalsIgnoreCase("c1") && inventory[0].prdQtyPrepared >= prdQty){
inventory[0].sellProduct(prdQty);//let class modify instance variables
System.out.println("Quantity prepared =" + inventory[0].getQtyPrepared());//never use instance variables directly, use accessor methods
System.out.print("Quantity Sold =" + inventory[0].getQtySold());//same as above
}
elseif(prdCode.equalsIgnoreCase("c2") && inventory[1].prdQtyPrepared >= prdQty)
//more like above
}
In OOP you almost NEVER access the fields directly. They are made private to prevent you from doing that. Instead, you use accessor methods (sometimes called 'get' methods). Also, notice that I let the object take care of adding and subtracting inventory. That should almost NEVER be in the main method.
I know you are getting close to your deadline. But if I was your teacher, this would be a much better grade that what you had.
I hope this helps, and I hope that you can see a little of the OOP design going on.
Thanks a lot. I hope that i can meet the deadline. By the way, do u have any ideas using I/O file to the class variable instead of just exist during program run-time.
This is probably past your deadline, but here are some suggestions just in case. It has been awhile since I've done this, so I hope I haven't left anything critical out. You probably have to throw IOException or use a try-catch block. (No offense, but this seems WAY beyond a typical assignment for someone who needed help creating an array.)
This is actually a two-stage process. You have to write a small program to create the file as well as read the file in where you are currently creating the array elements.
Here is the guts of the class that will create the file.
//put in main method of a class to create data file
FileOutputStream ostream = new FileOutputStream("filename");
ObjectOutputStream p = new ObjectOutputStream(ostream);
Product inventory = new Product[7];
// Create the Product objects in the arrays.
inventory[0] = new Product("Computer", "DELL", 2, 0, 1000, 1700, 0, 0, 0);
//rest of data
p.writeObject(inventory);//write array to file
p.flush();//flush the buffer
ostream.close();//close the file
This would go in the MainDisplay class.
//put in main method of a class to create data file
FileInputStream instream = new FileInputStream("filename");
ObjectInputStream n = new ObjectInputStream(instream);
Product inventory = (Product[])p.readObject();
instream.close();//close the file
It doesn't work I/O method that u show me. Actually, I have one main program file and another one text file will be created for the array storage and retriaval.
Maybe time zone difference, i still have 3 hour bfore deadline.
> Hi TWC, > > It doesn't work I/O method that u show me.
What problems are you encountering? > Actually, I have one main program file and another one > text file will be created for the array storage and > retriaval.
I did not know that you already had a text file. The way that I showed you does not work with text files. Without knowing how the text file is set up, there is no way for me to know how to help you use it. Sorry.
> Maybe time zone difference, i still have 3 hour bfore > deadline.
I'm sure it is long past now. I hope you have learned a little about OOP in this.
I have submitted my coursework. Thanks for the advice to help get my program up and running. I have very limited knowledge in OOP and Java. Do u have any good introductory book for Java to recommend so that i can have a better understanding of it.
Thanks......A Million..........And Have Nice Day.......
> Hi TWC, > > I have submitted my coursework. > Thanks for the advice to help get my program up and > running. > I have very limited knowledge in OOP and Java. > Do u have any good introductory book for Java to recommend > so that i can have a better understanding of it. > > Thanks......A Million..........And Have Nice Day.......
Here are two suggestions. Go to www.javabook.org and you will find a collegiate textbook called "Java Au Natural" that is available for download. Also "Thinking In Java" by Bruce Eckel is available in electronic form on the net. It is aimed more at experienced programmers that are switching to Java, and my not be ideal for a beginner. But it is free, so why not give it a look.
Certainly, you must have a textbook for the course that you are taking. Read it too. It should explain the fundamentals of OOP while it is teaching java.