Here's another JAVA Program I want to show off; this is another one I did more than a year ago. A description is provided at the top of this computer file and all the necessary comments should be there. Enjoy reading!
/*
* WHILE Loop Lab -
* City Water Company Water Bill Program
*
* File Name: CityWaterCompanyWaterBillProgram.java
*
* Developed by Gregory Desrosiers
* Student of Champlain College St-Lambert
* St-Lambert, Quebec, Canada
*
* Teacher: Mrs. Linda McGirr
*
* This program is designed to calculate and print out bills for a
* city water company. The billing system consists of uploading
* input data from a text file, selecting the case of what the pay
* rate is based on the account's code, then determining the price
* and saving the results on another text file as well as
* displaying it in a dialog box.
*
* The program contains a method to convert gallons to litres
* which is then studied for payment processing.
*
* Tested Input File: WaterProgramTestInputData.txt
* Output File: WaterCompanyBill.txt
* (This will be found in the same directory as the project.)
*
* Coding Start Date & Time:
* Saturday, October 29, 2011, 8:05 PM
*
* Coding End Date & Time:
*
* Due Date : November 7, 2011
*
*/
import java.io.*;
import javax.swing.JOptionPane;
public class CityWaterCompanyWaterBillProgram
{
public static void main(String[] args) throws IOException
{
String inputFile,
charToString,
accountNumberString,
currentTextLine,
waterUsedInGallonsString = "",
yesOrNoString;
boolean yesOrNoQuestionAnswered = true,
inputQuestion = true,
codeValid = true,
accountNumberDigitValid = true,
faultyWaterCharacter = false;
char currentCharacter = 0,
accountNumberDigit,
waterRateCode,
waterUsedInGallonsCharacter,
yesOrNo;
double waterUsedInLitres;
float pST,
gST,
subTotal = 0,
subTotalPlusGSTAndDiscount,
discountedTotal,
disCount,
finalTotal;
int characterTestNumber,
lineLength,
waterUsedInGallonsStringLength,
waterUsedInGallons;
final double gallonsToLitres = 3.78541178;
// Retrieved from Google Calculator
final float baseCostH = 5.00f,
baseCostC = 1000.00f,
payRateH = 0.0005f,
payRateC = 0.00025f,
rateGST = 0.05f,
ratePST = 0.075f,
lowDiscountRate = 0.05f,
highDiscountRate = 0.10f;
PrintWriter outputFile = new PrintWriter("WaterCompanyBill.txt");
JOptionPane.showMessageDialog(null, "Welcome to the McGirr" +
" Water Payment System, powered" +
" by JAVA. This program will" +
" produce a water bill for" +
" Montreal’s water company.");
yesOrNoString =
JOptionPane.showInputDialog("Is this your first time" +
" using this program?" +
" Type in 'Y' for yes" +
" or other character for" +
" no.\nThis is also where" +
" you can see the format" +
" the input file must be.");
if (yesOrNoString != null)
{
yesOrNo = yesOrNoString.charAt(0);
if (yesOrNo == 'Y' || yesOrNo == 'y')
{
JOptionPane.showMessageDialog(null, "What this program does" +
" is it loads a file contain" +
"ing the raw data (as a text" +
" file, .txt) and takes the " +
"raw data for calculations.\n" +
"It reads the account number " +
"and the code that is output" +
"ted in a file called " +
"WaterCompanyBill. The " +
"data for consumption\n(in " +
"gallons) is converted to " +
"litres which the money is" +
" then processed, depending" +
" on the account code and how" +
" many litres\nof water have " +
"been consumed. The discount " +
"is affected before the tax in" +
" the subtotal, where it gives" +
" a 5% discount for\nsubtotals " +
"ranging from $20.00 to $75.00" +
" or a 10% discount for sub" +
"totals between $75.01 and " +
"$999.99. If the total is " +
"outside\nthe range, no " +
"discount is applied. Either" +
" have a discount or not, the" +
" final steps remain the same.");
JOptionPane.showMessageDialog(null, "In order to run the prog" +
"ram, create a text file that ho" +
"lds the data needed to produce " +
"the bill.\n\nBe sure that the" +
" text looks like this:\n\n1325" +
" C 234524\n2135 H 332\n9423 I" +
" 78932\n\nwhere the four digits" +
" on the left is the account num" +
"ber, the letter in the middle " +
"is the account code,\nand the" +
" number on the right is the " +
"consumption (in gallons)." +
" A space seperates each of t" +
"he components.\n\nWARNING: Do" +
" not enter labels. This progra" +
"m will generate errors if the i" +
"nput text file is not like the" +
" example in\nthis dialog box." +
" Do not add more than one spa" +
"ce between the values because "+
"the program will recognize the" +
"\nextra spaces as invalid charac" +
"ters.\n\nThe program will now a" +
"sk you to enter the file name.");
}
}
else
{
JOptionPane.showMessageDialog(null, "Error: No entry for" +
" the first question. Please" +
" restart the program and " +
"answer the question.");
yesOrNoQuestionAnswered = false;
}
if (yesOrNoQuestionAnswered)
{
inputFile =
JOptionPane.showInputDialog("Enter the file in which" +
" the bill is to be" +
" produced.\nDo not enter" +
" the .txt file extension" +
" unless it is part of the" +
" name itself.\n.txt files" +
" only.");
// This is when the user clicks on Cancel, no matter what is
// typed inside the input dialog box's entry box.
if (inputFile == null)
{
JOptionPane.showMessageDialog(null, "Error: No file name" +
" entered.\nPlease run the program" +
" again and enter the text file" +
" name.");
inputQuestion = false;
}
else
{
FileReader textFileReader = new FileReader(inputFile + ".txt");
BufferedReader inputTextFile = new BufferedReader(textFileReader);
currentTextLine = inputTextFile.readLine();
/*
* If the text file can be read, it is tested to see if the first
* line does not contain anything. If it passes, then it can go
* for processing.
*/
if (currentTextLine == null)
{
JOptionPane.showMessageDialog(null, "Error: No text written" +
" in the file.\nPlease restart" +
" the program and enter a file" +
" name containing the input da" +
"ta.");
inputQuestion = false;
}
else
{
outputFile.println("Please note that there are approximate" +
" values in this file, but only the" +
" last decimal place is harmed.");
outputFile.println("The cost is determined by the consumpt" +
"ion in litres. To check the cost, mult" +
"iply consumption by 3.78541178, then a" +
"nalyze.");
outputFile.println("");
outputFile.println("Account | Code | Consumption | Subtotal" +
" | Discount | Discounted Price | GST " +
" | Discounted Price | PST | " +
"Final Cost");
outputFile.println("Number | | (Gallons) | " +
" | | | " +
" | Plus GST | |");
outputFile.println("");
System.out.println("Please note that there are approximate" +
" values in this file, but only the" +
" last decimal place is harmed.");
System.out.println("The cost is determined by the consumpt" +
"ion in litres. To check the cost, mult" +
"iply consumption by 3.78541178, then a" +
"nalyze.");
System.out.println("");
System.out.println("Account | Code | Consumption | Subtotal" +
" | Discount | Discounted Price | GST " +
" | Discounted Price | PST | " +
"Final Cost");
System.out.println("Number | | (Gallons) | " +
" | | | " +
" | Plus GST | |");
System.out.println("");
// All of the processing happens inside this loop.
while(currentTextLine != null && codeValid && accountNumberDigitValid &&
!faultyWaterCharacter)
{
currentCharacter = 0;
accountNumberString = "";
while (currentCharacter >= 0 && currentCharacter <= 3 &&
accountNumberDigitValid)
{
accountNumberDigit = currentTextLine.charAt(currentCharacter);
characterTestNumber = (int)accountNumberDigit;
if (characterTestNumber < 48 || characterTestNumber > 57)
{
accountNumberDigitValid = false;
currentCharacter += 4;
}
else
{
// The toString method is to convert the character to
// a string which can be then assigned to the
// variable holding the account number.
charToString = Character.toString(accountNumberDigit);
accountNumberString += charToString;
currentCharacter++;
}
}
/*
* To prevent the program from generating an exception error
* of converting a mixed string (numbers and nonnumerical
* characters), this conditional statement will stop the
* loop only if one of digits is not valid. Otherwise,
* it is safe to convert the string into an integer.
*/
if (accountNumberDigitValid == false)
break;
waterRateCode = currentTextLine.charAt(5);
// Testing to see if waterRateCode contains an invalid
// character.
if (waterRateCode != 'H' && waterRateCode != 'h' &&
waterRateCode != 'C' && waterRateCode != 'c' &&
waterRateCode != 'I' && waterRateCode != 'i')
{
codeValid = false;
break;
}
else
{
lineLength = currentTextLine.length();
currentCharacter = 7;
waterUsedInGallonsString = "";
/*
*
*/
while (currentCharacter < lineLength && !faultyWaterCharacter)
{
waterUsedInGallonsCharacter =
currentTextLine.charAt(currentCharacter);
characterTestNumber =
(int)waterUsedInGallonsCharacter;
if (characterTestNumber < 48 || characterTestNumber > 57)
faultyWaterCharacter = true;
else
{
charToString =
Character.toString(waterUsedInGallonsCharacter);
waterUsedInGallonsString += charToString;
currentCharacter++;
}
}
if (faultyWaterCharacter)
break;
else
waterUsedInGallons =
Integer.parseInt(waterUsedInGallonsString);
}
waterUsedInLitres = waterUsedInGallons * gallonsToLitres;
switch (waterRateCode)
{
case 'H':
case 'h':
{
subTotal = (float)(baseCostH + payRateH * waterUsedInLitres);
break;
}
case 'C':
case 'c':
{
if (waterUsedInLitres <= 4000000)
{
subTotal = baseCostC;
}
else
{
subTotal = (float)(baseCostC +
(waterUsedInLitres - 4000000) * payRateC);
}
break;
}
case 'I':
case 'i':
{
if (waterUsedInLitres <= 4000000)
subTotal = 1000;
else if (waterUsedInLitres > 4000000 &&
waterUsedInLitres <= 10000000)
subTotal = 2000;
else
subTotal = 3000;
}
/*
* No default is needed because the letter is
* already checked if it's valid. Otherwise
* it's duplicate coding.
*/
}
disCount = 0.00f;
discountedTotal = subTotal;
if (subTotal > 20.00 && subTotal <= 75.00)
{
disCount = subTotal * lowDiscountRate;
discountedTotal = subTotal - disCount;
gST = discountedTotal * rateGST;
subTotalPlusGSTAndDiscount = subTotal + gST;
pST = subTotalPlusGSTAndDiscount * ratePST;
finalTotal = subTotalPlusGSTAndDiscount + pST;
}
else if (subTotal > 75.00 && subTotal < 1000.00)
{
disCount = subTotal * highDiscountRate;
discountedTotal = subTotal - disCount;
gST = discountedTotal * rateGST;
subTotalPlusGSTAndDiscount =
discountedTotal + gST;
pST = subTotalPlusGSTAndDiscount * ratePST;
finalTotal = subTotalPlusGSTAndDiscount + pST;
}
/*
* To make output easier, we will use the printf
* statement and use subTotalPlusGSTAndDiscount
* to display the price plus GST that doesn't
* have a discount.
*/
else
{
gST = subTotal * rateGST;
subTotalPlusGSTAndDiscount = subTotal + gST;
pST = subTotalPlusGSTAndDiscount * ratePST;
finalTotal = subTotalPlusGSTAndDiscount + pST;
}
outputFile.print(accountNumberString + " " +
waterRateCode + " " +
waterUsedInGallons);
System.out.print(accountNumberString + " " +
waterRateCode + " " +
waterUsedInGallons);
waterUsedInGallonsStringLength =
waterUsedInGallonsString.length();
// The switch statement may have its cases duplicated,
// but this is to tell the compiler how many spaces
// should the consumption and the subtotal be
// seperated to create a neat appearence.
switch(waterUsedInGallonsStringLength)
{
case 1:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 2:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 3:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 4:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 5:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 6:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 7:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 8:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
// There is no default because it won't be possible
// for the consumption to be negative numbers or
// have 9 digits or more.
}
/*
* The following format lines produce the contents of the
* output file in such a way where it aligns them according
* to the field size for each field. This can only work if
* the font for both the console and the output text editor
* is set to Consolas or Lucida Console.
*/
outputFile.printf("$%9.2f", subTotal);
outputFile.printf(" $%5.2f", disCount);
outputFile.printf(" $%9.2f", discountedTotal);
outputFile.printf(" $%7.2f", gST);
outputFile.printf(" $%9.2f", subTotalPlusGSTAndDiscount);
outputFile.printf(" $%8.2f", pST);
outputFile.printf(" $%9.2f", finalTotal);
outputFile.println();
System.out.printf("$%9.2f", subTotal);
System.out.printf(" $%5.2f", disCount);
System.out.printf(" $%9.2f", discountedTotal);
System.out.printf(" $%7.2f", gST);
System.out.printf(" $%9.2f", subTotalPlusGSTAndDiscount);
System.out.printf(" $%8.2f", pST);
System.out.printf(" $%9.2f", finalTotal);
System.out.println();
currentTextLine = inputTextFile.readLine();
}
inputTextFile.close();
}
}
}
/*
* The three following conditions are only to see if an error
* has been encountered during the program and it declares
* a variable has a problem, in which the following prevent
* the program from crashing due to improper handling or
* exception errors.
*/
if (accountNumberDigitValid == false)
JOptionPane.showMessageDialog(null, "Error: Invalid Account"
+ " Number.\nPlease make sure"
+ " all the characters in the"
+ " account number are digits"
+ " or have no spaces, " +
"then run the program again.");
else if (codeValid == false)
JOptionPane.showMessageDialog(null, "Error: Invalid"
+ " Account Code.\n"
+ "Please correct the code"
+ " and run the program" +
" again. ");
else if (faultyWaterCharacter)
JOptionPane.showMessageDialog(null, "Error: Invalid"
+ " Character in Water"
+ " Quantity\nPlease " +
"make sure all the " +
"characters in the " +
"Consumpution field are " +
"digits or have no space" +
"s, then run the program" +
" again.");
/*
* If the full text file successfully completes the loop,
* the literal "Produced by the McGirr Water Payment System"
* is printed at the bottom of the bill.
*/
if (accountNumberDigitValid && codeValid &&
faultyWaterCharacter == false && yesOrNoQuestionAnswered
&& inputQuestion)
{
outputFile.println();
outputFile.println("Produced by the McGirr Water " +
"Payment System.");
System.out.println();
System.out.println("Produced by the McGirr Water " +
"Payment System.");
JOptionPane.showMessageDialog(null, "Thank you.\nThe" +
" bill has been produced.");
}
/*
* This is only if an error is encountered during the loop,
* because the program can't erase the output file when it
* encounters one.
*/
else if (accountNumberDigitValid == false || codeValid == false ||
faultyWaterCharacter)
JOptionPane.showMessageDialog(null, "Some of the outpu" +
"t has been made, except" +
" the lines where the er" +
"ror was found and those" +
" after it.");
/*
* The following condition is to let the dialog box display only
* if the program directs the user to the loop once the file
* is read. It is not executed if the question on the user using
* the program the first time is not answered.
*/
if ((accountNumberDigitValid && codeValid &&
faultyWaterCharacter == false && yesOrNoQuestionAnswered
&& inputQuestion) || (accountNumberDigitValid == false
|| codeValid == false || faultyWaterCharacter))
JOptionPane.showMessageDialog(null, "If the font you're" +
" using on Notepad or any" +
" text editor is not set " +
"to Consolas or Lucida Co" +
"nsole,\nyour bill is not" +
" going to look nice beca" +
"use of how many spaces s" +
"eperate the fields and h" +
"ow each\nfont family has" +
" a different space width" +
".\n\nThe bill can also b" +
"e seen on the screen thr" +
"ough an IDE's console or" +
" if you're running the p" +
"rogram\non the command p" +
"rompt.");
JOptionPane.showMessageDialog(null, "Program Complete.");
outputFile.close();
System.exit(0);
}
}
* WHILE Loop Lab -
* City Water Company Water Bill Program
*
* File Name: CityWaterCompanyWaterBillProgram.java
*
* Developed by Gregory Desrosiers
* Student of Champlain College St-Lambert
* St-Lambert, Quebec, Canada
*
* Teacher: Mrs. Linda McGirr
*
* This program is designed to calculate and print out bills for a
* city water company. The billing system consists of uploading
* input data from a text file, selecting the case of what the pay
* rate is based on the account's code, then determining the price
* and saving the results on another text file as well as
* displaying it in a dialog box.
*
* The program contains a method to convert gallons to litres
* which is then studied for payment processing.
*
* Tested Input File: WaterProgramTestInputData.txt
* Output File: WaterCompanyBill.txt
* (This will be found in the same directory as the project.)
*
* Coding Start Date & Time:
* Saturday, October 29, 2011, 8:05 PM
*
* Coding End Date & Time:
*
* Due Date : November 7, 2011
*
*/
import java.io.*;
import javax.swing.JOptionPane;
public class CityWaterCompanyWaterBillProgram
{
public static void main(String[] args) throws IOException
{
String inputFile,
charToString,
accountNumberString,
currentTextLine,
waterUsedInGallonsString = "",
yesOrNoString;
boolean yesOrNoQuestionAnswered = true,
inputQuestion = true,
codeValid = true,
accountNumberDigitValid = true,
faultyWaterCharacter = false;
char currentCharacter = 0,
accountNumberDigit,
waterRateCode,
waterUsedInGallonsCharacter,
yesOrNo;
double waterUsedInLitres;
float pST,
gST,
subTotal = 0,
subTotalPlusGSTAndDiscount,
discountedTotal,
disCount,
finalTotal;
int characterTestNumber,
lineLength,
waterUsedInGallonsStringLength,
waterUsedInGallons;
final double gallonsToLitres = 3.78541178;
// Retrieved from Google Calculator
final float baseCostH = 5.00f,
baseCostC = 1000.00f,
payRateH = 0.0005f,
payRateC = 0.00025f,
rateGST = 0.05f,
ratePST = 0.075f,
lowDiscountRate = 0.05f,
highDiscountRate = 0.10f;
PrintWriter outputFile = new PrintWriter("WaterCompanyBill.txt");
JOptionPane.showMessageDialog(null, "Welcome to the McGirr" +
" Water Payment System, powered" +
" by JAVA. This program will" +
" produce a water bill for" +
" Montreal’s water company.");
yesOrNoString =
JOptionPane.showInputDialog("Is this your first time" +
" using this program?" +
" Type in 'Y' for yes" +
" or other character for" +
" no.\nThis is also where" +
" you can see the format" +
" the input file must be.");
if (yesOrNoString != null)
{
yesOrNo = yesOrNoString.charAt(0);
if (yesOrNo == 'Y' || yesOrNo == 'y')
{
JOptionPane.showMessageDialog(null, "What this program does" +
" is it loads a file contain" +
"ing the raw data (as a text" +
" file, .txt) and takes the " +
"raw data for calculations.\n" +
"It reads the account number " +
"and the code that is output" +
"ted in a file called " +
"WaterCompanyBill. The " +
"data for consumption\n(in " +
"gallons) is converted to " +
"litres which the money is" +
" then processed, depending" +
" on the account code and how" +
" many litres\nof water have " +
"been consumed. The discount " +
"is affected before the tax in" +
" the subtotal, where it gives" +
" a 5% discount for\nsubtotals " +
"ranging from $20.00 to $75.00" +
" or a 10% discount for sub" +
"totals between $75.01 and " +
"$999.99. If the total is " +
"outside\nthe range, no " +
"discount is applied. Either" +
" have a discount or not, the" +
" final steps remain the same.");
JOptionPane.showMessageDialog(null, "In order to run the prog" +
"ram, create a text file that ho" +
"lds the data needed to produce " +
"the bill.\n\nBe sure that the" +
" text looks like this:\n\n1325" +
" C 234524\n2135 H 332\n9423 I" +
" 78932\n\nwhere the four digits" +
" on the left is the account num" +
"ber, the letter in the middle " +
"is the account code,\nand the" +
" number on the right is the " +
"consumption (in gallons)." +
" A space seperates each of t" +
"he components.\n\nWARNING: Do" +
" not enter labels. This progra" +
"m will generate errors if the i" +
"nput text file is not like the" +
" example in\nthis dialog box." +
" Do not add more than one spa" +
"ce between the values because "+
"the program will recognize the" +
"\nextra spaces as invalid charac" +
"ters.\n\nThe program will now a" +
"sk you to enter the file name.");
}
}
else
{
JOptionPane.showMessageDialog(null, "Error: No entry for" +
" the first question. Please" +
" restart the program and " +
"answer the question.");
yesOrNoQuestionAnswered = false;
}
if (yesOrNoQuestionAnswered)
{
inputFile =
JOptionPane.showInputDialog("Enter the file in which" +
" the bill is to be" +
" produced.\nDo not enter" +
" the .txt file extension" +
" unless it is part of the" +
" name itself.\n.txt files" +
" only.");
// This is when the user clicks on Cancel, no matter what is
// typed inside the input dialog box's entry box.
if (inputFile == null)
{
JOptionPane.showMessageDialog(null, "Error: No file name" +
" entered.\nPlease run the program" +
" again and enter the text file" +
" name.");
inputQuestion = false;
}
else
{
FileReader textFileReader = new FileReader(inputFile + ".txt");
BufferedReader inputTextFile = new BufferedReader(textFileReader);
currentTextLine = inputTextFile.readLine();
/*
* If the text file can be read, it is tested to see if the first
* line does not contain anything. If it passes, then it can go
* for processing.
*/
if (currentTextLine == null)
{
JOptionPane.showMessageDialog(null, "Error: No text written" +
" in the file.\nPlease restart" +
" the program and enter a file" +
" name containing the input da" +
"ta.");
inputQuestion = false;
}
else
{
outputFile.println("Please note that there are approximate" +
" values in this file, but only the" +
" last decimal place is harmed.");
outputFile.println("The cost is determined by the consumpt" +
"ion in litres. To check the cost, mult" +
"iply consumption by 3.78541178, then a" +
"nalyze.");
outputFile.println("");
outputFile.println("Account | Code | Consumption | Subtotal" +
" | Discount | Discounted Price | GST " +
" | Discounted Price | PST | " +
"Final Cost");
outputFile.println("Number | | (Gallons) | " +
" | | | " +
" | Plus GST | |");
outputFile.println("");
System.out.println("Please note that there are approximate" +
" values in this file, but only the" +
" last decimal place is harmed.");
System.out.println("The cost is determined by the consumpt" +
"ion in litres. To check the cost, mult" +
"iply consumption by 3.78541178, then a" +
"nalyze.");
System.out.println("");
System.out.println("Account | Code | Consumption | Subtotal" +
" | Discount | Discounted Price | GST " +
" | Discounted Price | PST | " +
"Final Cost");
System.out.println("Number | | (Gallons) | " +
" | | | " +
" | Plus GST | |");
System.out.println("");
// All of the processing happens inside this loop.
while(currentTextLine != null && codeValid && accountNumberDigitValid &&
!faultyWaterCharacter)
{
currentCharacter = 0;
accountNumberString = "";
while (currentCharacter >= 0 && currentCharacter <= 3 &&
accountNumberDigitValid)
{
accountNumberDigit = currentTextLine.charAt(currentCharacter);
characterTestNumber = (int)accountNumberDigit;
if (characterTestNumber < 48 || characterTestNumber > 57)
{
accountNumberDigitValid = false;
currentCharacter += 4;
}
else
{
// The toString method is to convert the character to
// a string which can be then assigned to the
// variable holding the account number.
charToString = Character.toString(accountNumberDigit);
accountNumberString += charToString;
currentCharacter++;
}
}
/*
* To prevent the program from generating an exception error
* of converting a mixed string (numbers and nonnumerical
* characters), this conditional statement will stop the
* loop only if one of digits is not valid. Otherwise,
* it is safe to convert the string into an integer.
*/
if (accountNumberDigitValid == false)
break;
waterRateCode = currentTextLine.charAt(5);
// Testing to see if waterRateCode contains an invalid
// character.
if (waterRateCode != 'H' && waterRateCode != 'h' &&
waterRateCode != 'C' && waterRateCode != 'c' &&
waterRateCode != 'I' && waterRateCode != 'i')
{
codeValid = false;
break;
}
else
{
lineLength = currentTextLine.length();
currentCharacter = 7;
waterUsedInGallonsString = "";
/*
*
*/
while (currentCharacter < lineLength && !faultyWaterCharacter)
{
waterUsedInGallonsCharacter =
currentTextLine.charAt(currentCharacter);
characterTestNumber =
(int)waterUsedInGallonsCharacter;
if (characterTestNumber < 48 || characterTestNumber > 57)
faultyWaterCharacter = true;
else
{
charToString =
Character.toString(waterUsedInGallonsCharacter);
waterUsedInGallonsString += charToString;
currentCharacter++;
}
}
if (faultyWaterCharacter)
break;
else
waterUsedInGallons =
Integer.parseInt(waterUsedInGallonsString);
}
waterUsedInLitres = waterUsedInGallons * gallonsToLitres;
switch (waterRateCode)
{
case 'H':
case 'h':
{
subTotal = (float)(baseCostH + payRateH * waterUsedInLitres);
break;
}
case 'C':
case 'c':
{
if (waterUsedInLitres <= 4000000)
{
subTotal = baseCostC;
}
else
{
subTotal = (float)(baseCostC +
(waterUsedInLitres - 4000000) * payRateC);
}
break;
}
case 'I':
case 'i':
{
if (waterUsedInLitres <= 4000000)
subTotal = 1000;
else if (waterUsedInLitres > 4000000 &&
waterUsedInLitres <= 10000000)
subTotal = 2000;
else
subTotal = 3000;
}
/*
* No default is needed because the letter is
* already checked if it's valid. Otherwise
* it's duplicate coding.
*/
}
disCount = 0.00f;
discountedTotal = subTotal;
if (subTotal > 20.00 && subTotal <= 75.00)
{
disCount = subTotal * lowDiscountRate;
discountedTotal = subTotal - disCount;
gST = discountedTotal * rateGST;
subTotalPlusGSTAndDiscount = subTotal + gST;
pST = subTotalPlusGSTAndDiscount * ratePST;
finalTotal = subTotalPlusGSTAndDiscount + pST;
}
else if (subTotal > 75.00 && subTotal < 1000.00)
{
disCount = subTotal * highDiscountRate;
discountedTotal = subTotal - disCount;
gST = discountedTotal * rateGST;
subTotalPlusGSTAndDiscount =
discountedTotal + gST;
pST = subTotalPlusGSTAndDiscount * ratePST;
finalTotal = subTotalPlusGSTAndDiscount + pST;
}
/*
* To make output easier, we will use the printf
* statement and use subTotalPlusGSTAndDiscount
* to display the price plus GST that doesn't
* have a discount.
*/
else
{
gST = subTotal * rateGST;
subTotalPlusGSTAndDiscount = subTotal + gST;
pST = subTotalPlusGSTAndDiscount * ratePST;
finalTotal = subTotalPlusGSTAndDiscount + pST;
}
outputFile.print(accountNumberString + " " +
waterRateCode + " " +
waterUsedInGallons);
System.out.print(accountNumberString + " " +
waterRateCode + " " +
waterUsedInGallons);
waterUsedInGallonsStringLength =
waterUsedInGallonsString.length();
// The switch statement may have its cases duplicated,
// but this is to tell the compiler how many spaces
// should the consumption and the subtotal be
// seperated to create a neat appearence.
switch(waterUsedInGallonsStringLength)
{
case 1:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 2:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 3:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 4:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 5:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 6:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 7:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
case 8:
{
outputFile.print(" ");
System.out.print(" ");
break;
}
// There is no default because it won't be possible
// for the consumption to be negative numbers or
// have 9 digits or more.
}
/*
* The following format lines produce the contents of the
* output file in such a way where it aligns them according
* to the field size for each field. This can only work if
* the font for both the console and the output text editor
* is set to Consolas or Lucida Console.
*/
outputFile.printf("$%9.2f", subTotal);
outputFile.printf(" $%5.2f", disCount);
outputFile.printf(" $%9.2f", discountedTotal);
outputFile.printf(" $%7.2f", gST);
outputFile.printf(" $%9.2f", subTotalPlusGSTAndDiscount);
outputFile.printf(" $%8.2f", pST);
outputFile.printf(" $%9.2f", finalTotal);
outputFile.println();
System.out.printf("$%9.2f", subTotal);
System.out.printf(" $%5.2f", disCount);
System.out.printf(" $%9.2f", discountedTotal);
System.out.printf(" $%7.2f", gST);
System.out.printf(" $%9.2f", subTotalPlusGSTAndDiscount);
System.out.printf(" $%8.2f", pST);
System.out.printf(" $%9.2f", finalTotal);
System.out.println();
currentTextLine = inputTextFile.readLine();
}
inputTextFile.close();
}
}
}
/*
* The three following conditions are only to see if an error
* has been encountered during the program and it declares
* a variable has a problem, in which the following prevent
* the program from crashing due to improper handling or
* exception errors.
*/
if (accountNumberDigitValid == false)
JOptionPane.showMessageDialog(null, "Error: Invalid Account"
+ " Number.\nPlease make sure"
+ " all the characters in the"
+ " account number are digits"
+ " or have no spaces, " +
"then run the program again.");
else if (codeValid == false)
JOptionPane.showMessageDialog(null, "Error: Invalid"
+ " Account Code.\n"
+ "Please correct the code"
+ " and run the program" +
" again. ");
else if (faultyWaterCharacter)
JOptionPane.showMessageDialog(null, "Error: Invalid"
+ " Character in Water"
+ " Quantity\nPlease " +
"make sure all the " +
"characters in the " +
"Consumpution field are " +
"digits or have no space" +
"s, then run the program" +
" again.");
/*
* If the full text file successfully completes the loop,
* the literal "Produced by the McGirr Water Payment System"
* is printed at the bottom of the bill.
*/
if (accountNumberDigitValid && codeValid &&
faultyWaterCharacter == false && yesOrNoQuestionAnswered
&& inputQuestion)
{
outputFile.println();
outputFile.println("Produced by the McGirr Water " +
"Payment System.");
System.out.println();
System.out.println("Produced by the McGirr Water " +
"Payment System.");
JOptionPane.showMessageDialog(null, "Thank you.\nThe" +
" bill has been produced.");
}
/*
* This is only if an error is encountered during the loop,
* because the program can't erase the output file when it
* encounters one.
*/
else if (accountNumberDigitValid == false || codeValid == false ||
faultyWaterCharacter)
JOptionPane.showMessageDialog(null, "Some of the outpu" +
"t has been made, except" +
" the lines where the er" +
"ror was found and those" +
" after it.");
/*
* The following condition is to let the dialog box display only
* if the program directs the user to the loop once the file
* is read. It is not executed if the question on the user using
* the program the first time is not answered.
*/
if ((accountNumberDigitValid && codeValid &&
faultyWaterCharacter == false && yesOrNoQuestionAnswered
&& inputQuestion) || (accountNumberDigitValid == false
|| codeValid == false || faultyWaterCharacter))
JOptionPane.showMessageDialog(null, "If the font you're" +
" using on Notepad or any" +
" text editor is not set " +
"to Consolas or Lucida Co" +
"nsole,\nyour bill is not" +
" going to look nice beca" +
"use of how many spaces s" +
"eperate the fields and h" +
"ow each\nfont family has" +
" a different space width" +
".\n\nThe bill can also b" +
"e seen on the screen thr" +
"ough an IDE's console or" +
" if you're running the p" +
"rogram\non the command p" +
"rompt.");
JOptionPane.showMessageDialog(null, "Program Complete.");
outputFile.close();
System.exit(0);
}
}
0 comments:
Post a Comment