Originally Posted by Goodwine
|
this is a simple school-project so i hope someone can help me
basically i just need to solve some doubts about using files
|
Ok.
Originally Posted by Goodwine
|
|
Well, my program is an abstract class User which is extended by Customer and Admin classes, then i want to write customer and admin objects on a file for each kind
|
Ok, but we don't do homework verbatim. Gladly help on what to think about or what to look for.
Originally Posted by Goodwine
|
|
first doubt, can i put a method in User to write objects on files to save code typing on the other classes?, like
|
Yes, very simple idea here - and it will work.
Originally Posted by Goodwine
|
|
an example will be on Customer add a method like
|
A little more details, more work,... still basic answer is yes. Just a matter of study, practice and reading various explainations.
Originally Posted by Goodwine
|
|
Supposing i can, or even if i cant and have to write it twice, do i need to use the constructor of FileOutputStream(String, boolean) so i can add objects, or what should i do instead of that?
|
Constructors, then methods, thus:
|
Code:
|
class FirstClass
{
SecondClass variable = new SecondClass();//
variable.doSomething();//
} |
Which for file classes often take a String name for the file name but it gets dicey just to grab one and code, be prepared to study or go ahead and fill application to get old job back flippin burgers. I pushed Pizza, did better than at Burgers 'N Stuffit.
Originally Posted by Goodwine
|
also, Customer has an "account" field which is an integer, and if i write many customers, how do i look for specifically one according to that field?
(the admin has a String "account" field but i guess it would be similar)
|
read in the customers and numbers and so on from a file, start with a short + simple array in the class to resemble a file. Go to bigger and better later.
You can start with an OO approach, just give names to whatever you can think of then write class Customer{} and so on .... stuff variables in each as you think of what matters.
Originally Posted by Goodwine
|
another doubt is... when i want to modify a customer's fields, supposing he has a float field called "owes" which indicates how much money does he owe to the store, the the customer pays and now i have to reduce the "owes" by the amount that was paid,
i will first look for the Customer object on the file the open it and then edit it and then save it again
but how do i replace that customer for the new one?
|
That is what the work is all about. Work awhile, then ask a question or two.
Originally Posted by Goodwine
|
|
and if i want to delete a customer forever? what should i do?
|
Set a field ( member variable ) to some value. Never delete due to issues in the real world. Somebody delete something, Head Moe In Charge need to know who delete and why. Usually but not always, start with negative one to signifiy Deletions, Backpedalling, Backflow and other Blindside work. Give special handling to anything with (-1) value. Use zero to signifiy Blank Customer, no name, no history, un-used Customer number and so on.
Originally Posted by Goodwine
|
last doubt, i want the admin to be able to show all customers in a list by name/customer number (account)/ amount debt (owes)
in this case i have to read all objects to an array, right?
|
Pretty much, figure out how to get a file open first. Before that, just do:
|
Code:
|
Customer[] customerArray = {"Bill","Boe","Moe"}; |
Which will actually be strings at first. When we move to an acutal Customer class as I supposedly coded here we can promote this simple sample to working code.
You will get a lot of people telling you to think, but they won't tell you what to think about. Deal with it, we had to.