Java Help....posted a few days ago...

Ruffian998

Member
Feb 9, 2005
25
0
0
Ok I posted this a few days ago. Here is what I need the program to do. Basically i need to creat a SalesPerson array the size of 20. It will read data from a txt file which follows this format (Name followed by sales on the next line). Once the program reads the line, it will see if it has any information for that salesperson in the record array and if not, it will create a new entry for that salesperson. And im going to have commission calculated like this:

0% commission on sales <= $1000.00
5% commission on sales > $1000.00 and <= $2500.00
10% commission on sales > $2500.00

And it will add this new commission to this salespersons sales and commissions.

Ex: On sales of $5000.00 the commission would be
(0% of first $1000) + (5% of $1500) + (10% of (sales -2500))

This is what I have: First part of the program (there are two):



class Salesman
{
public class Salesman()
{
String name;
double Sale;
int numOfSales;
int totalSales;
int Commission;
}

}




OK now second program:

import java.io.*;

class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader inFile;
PrintWriter outFile;

inFile = new BufferedReader(new FileReader("newsales.txt"));
outFile = new PrintWriter(new FileWriter("newsales.txt"));

int numOfSalesman = 0;
Salesman[] records = new Salesman[20];
String name=inFile.readLine();

While (name!=null);
{
for(int i = 0;i <= numOfSalesman;i++)
{
if(name.equals(records.name))
{
double Sale = Double.parseDouble(inFile.readLine());
//Find Commission
if (Sale > 1000.00 && <= 2500.00)
{
int Commission = Sale*0.05;
}
else if (Sale > 2500.00 && < 5000.00)
{
int Commission = Sale*0.10;
}
else if (Sale > 5000.00)
{
int Commission = (1500*0.05) + (Sale-2500)*0.10;
}
}
else
{
numOfSalesman++;
double Sale = Double.parseDouble(inFile.readLine());

//Find Commission
if (Sale > 1000.00 && <= 2500.00)
{
int Commission = Sale*0.05;
}
else if (Sale > 2500.00 && < 5000.00)
{
int Commission = Sale*0.10;
}
else if (Sale > 5000.00)
{
int Commission = (1500*0.05) + (Sale-2500)*0.10;
}
//Create new salesman
Salesman = new Salesman(name, Sale, Commission);
}

}

}
}
}



I can give you the data file you need it. The salespeople are colors. We used colors instead of names for some reason...irrelevant. Anyways, I have it sort the names and it will go through the data file and read it. While going through the data file, if it finds the same name twice, I need it to store the amount of money it made. So say first time its see yellow made 500, and then second time it sees it made 1000, I need it to add the two together for 1500. Also, I need it to print out all this information. All help is greatly aprreciated. And for some reason, I keep getting

Also, does anyone see any problems with my Salesman program because I keep getting errors in it, its says im missing the { and the }. Also if you notice any errors in the other please point them out.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
I don't understand what you are having trouble with. If you don't want to do something until two conditions are met, I'd guess use a boolean i.e. boolean first = false; boolean second = false; When its first found, set first to true, when found again (first is already true), set second to true also, and have an if that only executes given that both are true.

As for the bracket issues, you should post your code using the 'attach code' button so the code is indented correctly and it's a lot easier to spot the odd bracket.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Basically this program needs to read the firstline of the data. The first line will be a name. It will remember that name. Then it will read the next line which is the money earned for that person above.

It should keep doing this going through and recording the name and comission. The only thing is, I need it to record both amounts earned if it comes across the same name twice. I also need it to print out the comission for each person once its done finding the comission based on the formula I have in the program.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Hmm, where to begin ...

1. You can't use if(name.equals(records[ i ].name)) to search for an existing record. You'll need to either build a search function to find a name within the Salesman array or use an ArrayList or HashMap structure which are much better suited for this sort of thing.
2. Why are your commission calculations done in the main program? Your Salesman object has sales and commission properties, so use them! Set up a constructor for your Salesman class that lets you pass a sales amount (and subsequently calculates the commission property), and set up a method that lets you add sales to existing Salesman objects.
3. What is Salesman[ i ] = new Salesman(name, Sale, Commission);? Your Salesman array is called records, not Salesman.
4. Where do you read in the next salesman name at the end of your for loop?
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Mr. Chad I was hoping you would reply. How do I go about using an ArrayList? I dont really need to search. At the end of the program, all that should happen is it should say the name of the salesman and then the total comission earned for every salesman.

How would I go about letting it add to existing sales in Salesman objects?

I'm not quite sure I follow with question number 4.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
for the comission calculations would I do something like this?

public Salesman Comission()
{
// this would include the whole While statement thats in Main???

}
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
A HashMap would be easiest, but I don't know if that's allowed for your assignment. A HashMap is a collection of key-value pairs. You could use your name as the key and the Salesman object as the value.

HashMap oRecords = new HashMap();
if (oRecords.containsKey(name)){
...Salesman oSalesman = (Salesman)oRecords.get(name);
}
else{
...// create new salesman
...Salesman oSalesman = new Salesman();
...oRecords.put(name, oSalesman);
}

Your sales and commissions information should be stored and calculated as part of the Salesman object. If you have a constructor for Salesman like:

Salesman(double sales){
...this.totalSales = sales;
...this.numofSales = 1;
}

You can set up a function to add sales:

public void addSales(double sale){
...this.totalSales += sale;
...this.numOfSales++;
}

As for my 4th question, follow the logic of your loops. You read in the first line of the file before the loop, then you read in a second line to get the sales amount. Where do you read in the next name from the file? Also, why do you have a while (name != null) and a for loop?
 

Ruffian998

Member
Feb 9, 2005
25
0
0
Yeah I doubt I could use hashmap as we havent even learned it yet in class, do you have another idea? We're suppose to use an Array.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Well, that's up to you. You have an array now, you just need some kind of function to find a matching Salesman object in the array.
 

Ruffian998

Member
Feb 9, 2005
25
0
0
As for my 4th question, follow the logic of your loops. You read in the first line of the file before the loop, then you read in a second line to get the sales amount. Where do you read in the next name from the file? Also, why do you have a while (name != null) and a for loop?

I take it i shouldnt have a while and a for loop. How would I go about changing that cause I dont see any other way to do that.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
There are a number of tutorials out there on File I/O. Read up on them and look at the code samples. Try building the basic IO framework (read each line and spit it back out to the console), then add the more complex object creation and manipulation code.