Billing problem with file retrieval, multiple methods, switch and whil

Billing problem with file retrieval, multiple methods, switch and whil

Page 1 of 1

0 Replies - 1017 Views - Last Post: 30 April 2009 - 06:02 AM Rate Topic: -----

#1 Starshadow   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-April 09

Billing problem with file retrieval, multiple methods, switch and whil

Post icon  Posted 30 April 2009 - 06:02 AM

I am having a real hard time with this. I have gotten it to the point where it compiles, but I now get a buffer overflow error. I have tried researching it in my text and online, but I am getting frustrated. My instructor pretty much left the class to its own devices. Here is the project:

This program consists of calculating the bills for the We Spray Anything Company. They use planes to spray crops for a variety of problems. The rates they charge the farmer depend on what is being sprayed and how many acres one wants sprayed.

There are discounts if the amount to be sprayed is sufficiently large or if the bill exceeds a certain amount. If the area to be sprayed is greater than 1000 acres, the farmer receives a 5% discount of his total bill. In addition, any farmer whose bill is over $1500, after the 5% discount for over 1000 acres, receives a further 10% discount on the amount over $1500. This 10% discount is taken even if the farmer does not have 1000 acres.

We will be reading the data in from a file. Each line in the file will be a triple of numbers: (1) the ID number of the client, (2) a code for what is being sprayed, and (3) the number of acres being sprayed.
The main module will read in the data from a file. After reading in each triple, we will calculate the bill. Since the calculation of the bill is something which logically stands by itself, we will create a separate module calculateBill which will calculate the bill and return the amount. The line of code that does this is:

amountDue = calculateBill(type,acres);

To check the program to see if it is working, the following inputs (hopefully correctly) calculate the amount due:

Input Amount Due
1000 1 1200 $1140.00
1001 2 800 $2310.00
1002 3 300 $1200.00
1003 4 1100 $5793.00
1004 3 783 $2968.80
1005 1 1550 $1472.50

A print out on one line the client ID, type, number of acres, and the amount due. An example of what the output will look like is:

Client Number Type Acres Amount Due
1000 1 1200 $1140.00
1001 2 800 $2310.00

Note If there is bad input data, for example, the type does not have the values of 1-4 inclusive, calculateBill will return a negative value. In this case you should print out a "bad input data" for the amount:

1003 5 600 bad input data

After we have read in all of the relevant data, we will output the total amount billed for all of the clients in a separate line. You will have a while loop to control the reading of data from the file. You will continue to read in data until the clientNumber value of 0 is read in. After exiting the while loop, you will ouput a single line telling how much was due for all of the clients for this input file. Note: Be sure to close your file before exciting the program.

I have attached the txt file we are supposed to load for this. I really need some input, as I have been looking at this so long my head is beginning to spin. Thanks!

package Program4;
import java.util.*;

public class MainModule
public static void main(String[] args)throws IOException
 int	clientNumber,   //Client ID number
		type,		   //Type of billing
						//Type 1: Spraying for weeds, $1 an acre
						//Type 2: Spraying for grasshoppers, $3 an acre
						//Type 3: Spraying for army worms, $4 an acre
						//Type 4: Spraying for all of these, $6 an acre
		acres;		  //Number of acres to be sprayed

double  amountDue;	  //The amount due for the client

Scanner scan = new Scanner(new FileReader ("spraying.txt"));
clientNumber = scan.nextInt();
type = scan.nextInt();
acres = scan.nextInt();
amountDue = calculateBill(type,acres);

int sum, counter;
final int SENTINEL = 0;
counter = 0;
sum = 0;
while (clientNumber != SENTINEL)

public static double calculateBill(int type, int acres)
	  double grossCharge;
	  switch (type)
	  case '1':
	  grossCharge = (1 * acres);
	  case '2':
	  grossCharge = (3 * acres);
	  case '3':
	  grossCharge = (4 * acres);
	  case '4':
	  grossCharge = (6 * acres);
	  if (acres > 1000)
		 grossCharge *= 0.95;
	   else if (acres < 0)
		 throw new IllegalArgumentException("acres cannot be negative");
	  if (grossCharge > 1500)
			   grossCharge = 0.90*(grossCharge - 1500) + 1500;
		   return calculateBill (type, acres);  

Attached File(s)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1