Instructions: Using a modular approach, modify Project 2 to include methods. In
ID: 3914780 • Letter: I
Question
Instructions:
Using a modular approach, modify Project 2 to include methods.
In addition to the main method code the following static methods.
A method that displays the title.
A method that accepts arguments and will calculate and return the boarding charge before taxes (this would include the fee for high risk breeds).
A method that accepts an argument and will calculate and return the tax amount.
A method that accepts arguments and will calculate and return the total price of boarding (including taxes).
A method that displays the same output as Project 2 (include the average of all bills on a line after the total of all bills output - see sample output below).
Notes:
All methods must be coded as instructed above. Modifications to method arguments, content, etc. will result in a major error deductions.
The Scanner class must be used to read and process the file.
Read the text file from the main method.
Sample output should be the same as Project 2 with the exception of the additional line for the average bills (see below).
Sample Output
Madison Kennel & Grooming
(two blank lines here)
Customer:
Breed:
Weight: xx.x pounds
Boarding days: xx days(s)
Subtotal for xx days: $xx.xx
Tax amount: $xx.xx
Total due: $xx.xx
Blank line between each account
Two blank lines after the last account
Total Fees for the billing cycle: $xx.xx
Average Fees for the billing: $xx.xx
Project 2?
1 import java.util.Scanner;//for the Scanner Class
2
3 import java.io.*;//for file I/O classes
4
5 public class PetGrooming{
6
7 public static void main(String[] args) throws IOException
8
9 {
10 //open the file
11 File file = new File("boarding.txt");
12
13 Scanner inputFile = new Scanner(file);//read the file using the Scanner class
14
15 //declare variables
16
17 String firstName, lastName;
18
19 String dogBreed;
20
21 double dogWeight;
22
23 int daysBoarding;
24
25 double subTotal;
26
27 double total;
28
29 double taxAmount;
30
31 double fee;
32
33 double PricePerDay;
34
35 final double TAX = 0.07; // tax 7%
36
37 final double HIGH_RISK_FEE = 30;
38
39 double totalFee = 0;
40
41 String title = new String("Madison Kennel & Grooming");
42
43 System.out.println(title);
44
45 //loop processes the lines read from the file,
46 //until the end of the file is encountered.
47
48 while (inputFile.hasNextLine())
49
50 {
51
52 fee = 0;// intialize high risk dog fee
53
54
55
56 firstName = inputFile.nextLine();
57
58 lastName = inputFile.nextLine();
59
60 dogBreed = inputFile.nextLine();
61
62 dogWeight = inputFile.nextDouble();
63
64 daysBoarding = inputFile.nextInt();
65
66 if (inputFile.hasNextLine())//consume the newline character
67
68
69 {
70
71 inputFile.nextLine(); //consume the break line character
72
73 }
74
75 if (inputFile.hasNextLine())//consume the new line character
76 {
77 inputFile.nextLine(); //this handles the end of the file to avoid an exception
78
79
80 }
81
82 if (dogBreed.equals("Pit Bull") || dogBreed.equals("Doberman") || dogBreed.equals("Rottweiler"))
83
84 {
85
86 fee = HIGH_RISK_FEE;// high risk dog fee
87
88 }
89
90 if(dogWeight<20&&daysBoarding<7)
91
92 {
93
94 PricePerDay=14.99;
95
96 }
97
98 /*the weight of pet's less than 20 and the
99
100 number of boarding 7 or more
101
102 */
103
104 else if(dogWeight<20&&daysBoarding>=7)
105
106 {
107
108 PricePerDay=13.99;
109
110 }
111
112 /*
113
114 the weight of pet's greater or equal to 20 and less than or equal to 75,the
115
116 number of boarding days less than 7
117
118 */
119
120 else if(dogWeight>=20&&dogWeight<=75&&daysBoarding<7)
121
122 {
123
124 PricePerDay=19.99;
125
126 }
127
128 /*
129
130 the weight of pet's greater or equal to 20 and less than or equal to 75,the
131
132 number of boarding days greater than 7
133
134 */
135
136 else if(dogWeight>=20&&dogWeight<=75&&daysBoarding>=7)
137
138 {
139
140 PricePerDay=18.99;
141
142 }
143
144 //the weight of pet's greater than 75 and the number of boarding less than 7
145
146 else if(dogWeight>75&&daysBoarding<7)
147
148 {
149
150 PricePerDay=24.99;
151
152 }
153
154 //the weight of pet's greater than 75 and the number of boarding 7 or more
155
156 else
157
158 {
159
160 PricePerDay=23.99;
161
162 }
163
164 double boardingCost = PricePerDay * daysBoarding;
165
166 subTotal = boardingCost + fee;
167
168 taxAmount = subTotal * TAX;
169
170 total = subTotal + taxAmount;
171
172 //add the total fee
173 totalFee+= total;
174
175 //display the output
176 System.out.println();
177
178 System.out.println();
179
180 System.out.println("Customer: " + firstName + " " + lastName);
181
182 System.out.println("Breed: " + dogBreed);
183
184 System.out.println("Weight: " + dogWeight + " lbs");
185
186 System.out.println("Length of Stay: " + daysBoarding + " days");
187
188 System.out.printf("Sub Total: $%.2f%n" , subTotal);
189
190 System.out.printf("Tax: $%.2f%n" , taxAmount);
191
192 System.out.printf("Total: $%.2f%n" , total);
193
194
195 }
196
197 // close the file.
198
199 inputFile.close();
200
201 System.out.println(" ");
202
203 //output the total fee for billing cycle outside of the loop
204
205 System.out.printf("Total fees for billing cycle: $%.2f%n",totalFee);
206
207 }
208
209 }
text file
Gene
Smith
Boxer
5
7
Mark
Johnson
Pit Bull
20
5
Mike
Hampton
Hound
13
3
Dave
Bryant
Doberman
25
4
Vivian
Jones
Rottweiler
30
2
Regina
Medley
Husky
35
8
Explanation / Answer
Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;//for the Scanner Class
import java.io.*;//for file I/O classes
public class PetGrooming {
private static void printTitle(){
String title = new String("Madison Kennel & Grooming");
System.out.println(title);
}
private static double calculateSubTotal(String dogBreed, double dogWeight, int daysBoarding){
double subTotal;
double fee = 0;
double PricePerDay;
final double HIGH_RISK_FEE = 30;
if (dogBreed.equals("Pit Bull") || dogBreed.equals("Doberman") || dogBreed.equals("Rottweiler"))
{
fee = HIGH_RISK_FEE;// high risk dog fee
}
if (dogWeight < 20 && daysBoarding < 7)
{
PricePerDay = 14.99;
}
/*
* the weight of pet's less than 20 and the
*
* number of boarding 7 or more
*
*/
else if (dogWeight < 20 && daysBoarding >= 7)
{
PricePerDay = 13.99;
}
/*
*
* the weight of pet's greater or equal to 20 and less than or equal
* to 75,the
*
* number of boarding days less than 7
*
*/
else if (dogWeight >= 20 && dogWeight <= 75 && daysBoarding < 7)
{
PricePerDay = 19.99;
}
/*
*
* the weight of pet's greater or equal to 20 and less than or equal
* to 75,the
*
* number of boarding days greater than 7
*
*/
else if (dogWeight >= 20 && dogWeight <= 75 && daysBoarding >= 7)
{
PricePerDay = 18.99;
}
// the weight of pet's greater than 75 and the number of boarding
// less than 7
else if (dogWeight > 75 && daysBoarding < 7)
{
PricePerDay = 24.99;
}
// the weight of pet's greater than 75 and the number of boarding 7
// or more
else
{
PricePerDay = 23.99;
}
double boardingCost = PricePerDay * daysBoarding;
subTotal = boardingCost + fee;
return subTotal;
}
private static double calculateTax(double total){
final double TAX = 0.07; // tax 7%
return total * TAX;
}
private static double calculateTotal(double subTotal, double tax){
return subTotal + tax;
}
private static void print(String firstName, String lastName, String dogBreed, double dogWeight, int daysBoarding,
double subTotal, double taxAmount, double total){
// display the output
System.out.println();
System.out.println();
System.out.println("Customer: " + firstName + " " + lastName);
System.out.println("Breed: " + dogBreed);
System.out.println("Weight: " + dogWeight + " lbs");
System.out.println("Length of Stay: " + daysBoarding + " days");
System.out.printf("Sub Total: $%.2f%n", subTotal);
System.out.printf("Tax: $%.2f%n", taxAmount);
System.out.printf("Total: $%.2f%n", total);
}
public static void main(String[] args) throws IOException
{
// open the file
File file = new File("boarding.txt");
Scanner inputFile = new Scanner(file);// read the file using the Scanner
// class
// declare variables
String firstName, lastName;
String dogBreed;
double dogWeight;
int daysBoarding;
double subTotal;
double total;
double taxAmount;
double totalFee = 0;
int count = 0;
printTitle();
// loop processes the lines read from the file,
// until the end of the file is encountered.
while (inputFile.hasNextLine())
{
firstName = inputFile.nextLine();
lastName = inputFile.nextLine();
dogBreed = inputFile.nextLine();
dogWeight = inputFile.nextDouble();
daysBoarding = inputFile.nextInt();
if (inputFile.hasNextLine())// consume the newline character
{
inputFile.nextLine(); // consume the break line character
}
if (inputFile.hasNextLine())// consume the new line character
{
inputFile.nextLine(); // this handles the end of the file to
// avoid an exception
}
subTotal = calculateSubTotal(dogBreed, dogWeight, daysBoarding);
taxAmount = calculateTax(subTotal);
total = calculateTotal(subTotal, taxAmount);
print(firstName, lastName, dogBreed, dogWeight, daysBoarding, subTotal, taxAmount, total);
// add the total fee
totalFee += total;
count++;
}
// close the file.
inputFile.close();
System.out.println(" ");
// output the total fee for billing cycle outside of the loop
System.out.printf("Total fees for billing cycle: $%.2f%n", totalFee);
System.out.printf("Average fees for billing cycle: $%.2f%n", totalFee/count);
}
}
output
------
Madison Kennel & Grooming
Customer: Gene Smith
Breed: Boxer
Weight: 5.0 lbs
Length of Stay: 7 days
Sub Total: $97.93
Tax: $6.86
Total: $104.79
Customer: Mark Johnson
Breed: Pit Bull
Weight: 20.0 lbs
Length of Stay: 5 days
Sub Total: $129.95
Tax: $9.10
Total: $139.05
Customer: Mike Hampton
Breed: Hound
Weight: 13.0 lbs
Length of Stay: 3 days
Sub Total: $44.97
Tax: $3.15
Total: $48.12
Customer: Dave Bryant
Breed: Doberman
Weight: 25.0 lbs
Length of Stay: 4 days
Sub Total: $109.96
Tax: $7.70
Total: $117.66
Customer: Vivian Jones
Breed: Rottweiler
Weight: 30.0 lbs
Length of Stay: 2 days
Sub Total: $69.98
Tax: $4.90
Total: $74.88
Customer: Regina Medley
Breed: Husky
Weight: 35.0 lbs
Length of Stay: 8 days
Sub Total: $151.92
Tax: $10.63
Total: $162.55
Total fees for billing cycle: $647.04
Average fees for billing cycle: $107.84