Help with add Customer application

I am currently in a Java class and the class has been pretty simple. We have learned a lot of stuff but for the course project we are assumed to find information on our own about things we can not already do to make a full program for the last grade. The way I want things to function should get me an A+ and i've spent a lot of time with it.

My problem is that when I add a new customer to the text file it automatically generates a custID. I want this to start from 1 and continue over and over. This works great, until you restart the program. Then the program restarts at 1 and the text file goes from 1, 2, 3, 4, 5 back to 1 because the program's first input starts at 1. I want to read from the last line in the text file first integer custID set the custID to that number then when a new customer is added continue to +1.

The text file strings look like so "1, Name, Email, Phone, City, Street; " whats in between the quotes is what is printed on each line, with what the user has input.

Re: Help with add Customer application

Suggestions:

Split up the god-class above into several smaller classes, each focusing on a single purpose. It's a whole lot easier fixing well separated small classes than trying to fix one huge complex g'mish of code. It's a whole lot easier for us to understand what you're doing and be able to help you if you make these changes.

In particular your GUI or view code should not do any file I&O which should be in its own class

And you should have a Customer class that encapsulates the concept of a customer and his properties.

And you should have a database or Customer collection class that holds the Customer objects, perhaps in an ArrayList. The GUI can iterate through this collection for display, can add to it and remove from it.

The I&O classes can read from a file to add Customers to the CustomerCollection class.

Likely the CustomerCollection will keep track of customerId values and possibly create new id's for new Customer objects added to it.

Re: Help with add Customer application

I want to read from the last line in the text file first integer custID set the custID to that number then when a new customer is added continue to +1.

Can't you just do that?

What I mean is where you have "//create file on system" instead *check* for existence of the file. If it exists read it, set cuID and close it again. If it does not exist, create it. From that point on you should be good: the file will exist and cuID will have the appropriate value.

A separate point, but it strikes me that you have rather a lot in the panel's constructor. Perhaps some other class could represent the collection of customers? This other class class could be responsible for the logic of locating/creating the file and knowing what ID to assign a new customer. [Edit] continue down this track and you eventually end up at Fubarable's excellent advice...

Re: Help with add Customer application

Originally Posted by pbrockway2

Can't you just do that?

What I mean is where you have "//create file on system" instead *check* for existence of the file. If it exists read it, set cuID and close it again. If it does not exist, create it. From that point on you should be good: the file will exist and cuID will have the appropriate value.

A separate point, but it strikes me that you have rather a lot in the panel's constructor. Perhaps some other class could represent the collection of customers? This other class class could be responsible for the logic of locating/creating the file and knowing what ID to assign a new customer. [Edit] continue down this track and you eventually end up at Fubarable's excellent advice...

Thank you both for your replys and help! I am new to Java and it isnt exactly my major but I do understand there are coding ethics I should be following. I have been getting a bit messy with my class. I do actually have a Customer class that creates the customer object and formats it to ID, name, phone, email, city, street;

Yes checking for the file then reading the ID to set it would work perfect! I understand the logic behind it but the answer im looking for is how to skim through text files to pull information out. Like I need to command the program to find the last line of the file and then only pull the first part of the string before the , to get just the last customerID number. I have no idea how to do this.