Need help with Reading File Input than Calculating!
I am have a real hard time with this program i have to write. The program has to
Read in from the file
- Name
- Asking Price
- Sale Price
- Amount still owed on Mortgage
Print out AND write to a file a table that contains the following:
- Name
- Asking Price
- Mortgage Amount
- Selling Price
- Realtor Commission
Property Taxes Net price
Calculations:
- Realtor Commission = Selling Price * 6%
- Property Taxes = Selling Price * 10.5%
- Net Price = Asking Price - Mortgage Amount - Realtor Commission
- Total Profit / Loss = Accummulate the net prices
i have no clue how to separate the data and do separate calculations!! A sample data file looks like this...
Hopkins 250000 223000 209000
Smith 305699 299999 297500
Forrest 124999 124999 53250
Fitch 214999 202500 200000
There is no way i can read the data then do the calculation开发者_开发问答s and then write the new data to a new file, please help!
Since you are using Java, you should try to create an Object model for your application before proceeding. It is difficult to explain the entire approach but perhaps you could start with creating a class diagram for the application.
I'll try to give some hints without being too specific. I want to highlight that the following is just one of the ways to do it and may not be the best approach.
At a high level, think about the operations you want to perform and the data that the operations will work on. In your case, for ex, you have 3 activities - read from file, perform calculations and then output data. Create separate worker classes for these activities. Also think of classes that will store the data. These classes would be useful to pass along the data between worker classes.
Next, design how the classes will communicate with each other to get the job done. For ex, you could have a controller class that manages the 3 activities by collaborating with the other components to get the job done. It could call the file-reader component to get the data from the file, then it would send the data to calculating component to do the calculation and get back results which would then be passed to the writer-component.
Again, this is a simplistic approach but may not be the best solution. As you go along, review and refactor as necessary.
try {
float netTotal = 0;
String thisLine = null;
FileOutputStream out = new FileOutputStream("out.txt");
BufferedReader br = new BufferedReader(new FileReader("inputFile.txt"));
while ((thisLine = br.readLine()) != null) { // while loop begins here
String[] parts = thisLine.split("[\\t\\n\\r ]+");
int askingPrice = Integer.parseInt(parts[1]);
int salePrice = Integer.parseInt(parts[2]);
int amountOwed = Integer.parseInt(parts[3]);
float commission = salePrice * 0.06;
float tax = salePrice * 0.105;
float netPrice = askingPrice - amountOwed - commission;
netTotal += netPrice;
out.write((parts[0] + "\t").getBytes()); //name
out.write((askingPrice + "\t").getBytes()); //asking price
out.write((amountOwed + "\t").getBytes()); //mortgage amount
out.write((salePrice + "\t").getBytes()); //selling price
out.write((commission + "\t").getBytes()); //realtor commission
out.write((tax + "\t").getBytes()); //sales tax
out.write((netPrice + "\t\n").getBytes()); //net price
}
out.close();
br.close();
System.out.println("Net profit/loss: " + netTotal);
}
catch (Exception e) {
e.printStackTrace();
}
精彩评论