Reputation: 91
I need to write a program that takes 3 student info from an input file. calculate the tuition after determining if they are a full time or part time student based on the number of credits scanned, then output all their info to an output file. So far I am pretty confident most of the code would work fine, but I am having an issue.
Our class just went over material concerning loops, and I am required to make sure the code uses just such. So far I use a while loop to scan the entire file until the end. While it does get the needed student info. It only prints the last one, so it is obviously only getting 1 total, then overwriting that same one with the next section. Here is the code.
I more or less only need help with the inputs and possibly outputs.
As for the input file, it is below
Dom Pilorusso
1037 Waterford Court
Canonsburg PA 15317
C937493021
15
Dan Madeupname
106 Cidar Lane
McMurray PA 15317
C927012312
11
Steve Arnold
281 Maple Drive
Canonsburg PA 15317
C482716209
9
public class Program4 {
// Sets the prices for full time and part time students.
static final double TUITION_PER_CREDIT = 276.00;
static final double FEE_PER_CREDIT = 15.00;
static final double SERVICE_PER_CREDIT = 7.09;
static final double FULL_TIME_TUITION = 3311.00;
static final double FULL_TIME_FEE = 184.00;
static final double FULL_TIME_SERVICE = 85.00;
public static void main(String[] args) throws FileNotFoundException
{
int i ;
String firstName =null ;
String lastName =null ;
String accountNumber =null ;
double creditsTaken = 0 ;
String address1 ;
String address, address2, address3, address4, address5, address6;
String fileName ;
double tuition = 0 ;
double fees = 0 ;
double total = 0 ;
String formatFees ;
String formatTotal ;
String formatTuition ;
//create a scanner object named inFile and assign it the file input.dat
fileName = JOptionPane.showInputDialog("Please enter the input file name. ");
Scanner inFile = new Scanner (new FileReader(fileName));
//create a PrintWriter object named outFile associated with the file output.dat
PrintWriter outFile = new PrintWriter ("tuitionAndFees.dat");
//Intended to loop the input until the end of the file.
while (inFile.hasNext())
{ firstName = inFile.next();
lastName = inFile.next();
address = inFile.next();
address2 = inFile.next();
address3 = inFile.next();
address4 = inFile.next();
address5 = inFile.next();
address6 = inFile.next();
accountNumber = inFile.next();
creditsTaken = inFile.nextDouble();
}
//If Else statement to determine if the student is a part time or full time student, and then calculates their bill.
if(creditsTaken < 12)
{ tuition = TUITION_PER_CREDIT * creditsTaken;
fees =(FEE_PER_CREDIT + SERVICE_PER_CREDIT) * creditsTaken;
total = tuition + fees;
}
else
{
tuition = FULL_TIME_TUITION;
fees = FULL_TIME_FEE + FULL_TIME_SERVICE;
total = tuition + fees;
}
formatTotal = String.format("%.2f", total);
formatFees = String.format("%.2f", fees);
formatTuition = String.format("%.2f", tuition);
//Output to file all info, needs fixed.
outFile.println("Tuition Billing Report ");
outFile.printf("CWID\t\t"+ "Name\t\t"+ "Credits\t\t"+ "Tuition\t\t"+ "Fees\t\t"+ "Total%n");
outFile.printf(accountNumber + "\t"+ firstName + " "+ lastName + "\t"+ creditsTaken + "\t\t" + formatTuition + "\t\t" + formatFees + "\t\t" +formatTotal);
inFile.close();
outFile.close();
JOptionPane.showMessageDialog(null, "The program was saved in tuitionAndFees.dat");
}
}
Upvotes: 0
Views: 1044
Reputation: 6557
In the existing code, write is performed only after reading the input file completely. This causes the problem.
Solution:
Read one student's data, calculate and write data for that student to the output file.
Repeat the same for all students.
Use line-by-line approach to process data.
Do not store the all the students data in arrays because it will increase memory usage and the data is not used any furthur in the program after writing it to output file.
Modified main method below:
public static void main(String[] args) throws FileNotFoundException {
String firstName = null;
String lastName = null;
String accountNumber = null;
double creditsTaken = 0;
String address1;
String address, address2, address3, address4, address5, address6;
String fileName;
double tuition = 0;
double fees = 0;
double total = 0;
String formatFees;
String formatTotal;
String formatTuition;
// create a scanner object named inFile and assign it the file input.dat
fileName = JOptionPane
.showInputDialog("Please enter the input file name. ");
Scanner inFile = new Scanner(new FileReader(fileName));
// create a PrintWriter object named outFile associated with the file
// output.dat
PrintWriter outFile = new PrintWriter("tuitionAndFees.dat");
// Print headers in output file
outFile.println("Tuition Billing Report ");
outFile.printf("CWID\t\t" + "Name\t\t" + "Credits\t\t" + "Tuition\t\t"
+ "Fees\t\t" + "Total%n");
// Intended to loop the input until the end of the file.
while (inFile.hasNext()) {
firstName = inFile.next();
lastName = inFile.next();
address = inFile.next();
address2 = inFile.next();
address3 = inFile.next();
address4 = inFile.next();
address5 = inFile.next();
address6 = inFile.next();
accountNumber = inFile.next();
creditsTaken = inFile.nextDouble();
// If Else statement to determine if the student is a part time or
// full time student, and then calculates their bill.
if (creditsTaken < 12) {
tuition = TUITION_PER_CREDIT * creditsTaken;
fees = (FEE_PER_CREDIT + SERVICE_PER_CREDIT) * creditsTaken;
total = tuition + fees;
}
else {
tuition = FULL_TIME_TUITION;
fees = FULL_TIME_FEE + FULL_TIME_SERVICE;
total = tuition + fees;
}
formatTotal = String.format("%.2f", total);
formatFees = String.format("%.2f", fees);
formatTuition = String.format("%.2f", tuition);
outFile.printf(accountNumber + "\t" + firstName + " " + lastName
+ "\t" + creditsTaken + "\t\t" + formatTuition + "\t\t"
+ formatFees + "\t\t" + formatTotal+"\n");
}
inFile.close();
outFile.close();
JOptionPane.showMessageDialog(null,
"The program was saved in tuitionAndFees.dat");
}
Upvotes: 0
Reputation: 1824
You only created one of each variable and inside of the while you are correclty going through the file however you are constantly assigning the values to the variables and overwriting the previous values.
You have 3 alternatives:
1 - you create a series of arrays for each variable and assign the values inside the while (this is somewhat poor structured programming)
2 - you create a class that represent your student entity with all that variables and inside the while you create instances and assign each instance to an array position (an array of Student)
3 - you read each line and do the processing you want (sometimes this makes it a little harder because you may have to have lots os accumulators and auxiliary variables)
I would go with option 2.
Upvotes: 1