// ************************************************************************** // TuitionCalculator is a program that adds up tuition and fees // based on user input. The entire program is a series of do-while statements // that check for valid input, and nested if statements. A flag is set to false // at the beginning of each loop and remains false until a valide entry is made // and the loop is then exited. // // Kelli Wiseth // 17-July-2004 // Platform: Wintel [Windows 2000] // JDK version: JDK 1.4.1 // Time spent: ~ 20 hours //****************************************************************************** import java.io.*; import java.text.NumberFormat; class TuitionCalculator { static final int GRAD_TUITION_UNIT_FEE=794; static final int GRAD_TUITION_FLAT_FEE=2499; static final int MAX_GRAD_UNITS=16; static final int UNDERGRAD_TUITION_UNIT_FEE=834; static final int UNDERGRAD_TUITION_FLAT_FEE=3419; static final int MAX_UNDERGRAD_UNITS=24; static final int STUDENT_HEALTH_SVS_FEE=133; static final int STUDENT_AID_FEE=98; static final int COMPUT_ACCOUNT_FEE=73; static final int LOCKER_FEE=43; static final int NEW_STUDENT_KIT_FEE=27; static final int TUTOR_SVC_FEE=45; static final int GGU_SWEAT_FEE=65; static final int PARKING_FEE=67; static final int ID_STORED_VALUE_CARD_FEE=47; public static void main (String args[]) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String student_name, num_units, sweatshirt_size="none"; char answer; boolean flag=false; boolean stored_value=false; boolean grad_student=false; boolean sweat=false; int total_units=0; int id_card_value=0; int total_fees=(STUDENT_HEALTH_SVS_FEE + STUDENT_AID_FEE + COMPUT_ACCOUNT_FEE); int tuition=0; //Enter name System.out.print("Enter your name: "); System.out.flush(); student_name=stdin.readLine(); flag=false; //reset flag to false before each entry-validation routine //check for graduate or undergraduate, and set graduate boolean for future use do { System.out.print("Are you a (g)raduate student, or an (u)ndergrad? Please enter g or u: "); answer=stdin.readLine().charAt(0); if (((answer =='g') || (answer =='G')) || ((answer =='u') | (answer =='U'))) flag=true; else System.out.println("The letter \'" + answer + "\' isn't a valid response. Please try again. "); } while (flag==false); if (answer=='g' || answer=='G') grad_student=true; flag=false; //reset flag to false before each entry-validation routine do { System.out.print("How many units would you like to take, " + student_name + "? "); System.out.flush(); total_units=Integer.parseInt(stdin.readLine()); if (total_units <=0) { System.out.print("Sorry, " + student_name + ", please enter more than 0 units: "); flag=false; } if ((grad_student==false) && (total_units > MAX_UNDERGRAD_UNITS)) { System.out.print("Sorry, " + student_name + ", " + total_units + " is too many units.\n " + "Undergrads should take no more than " + MAX_UNDERGRAD_UNITS + " units;\n " + "Please try again, but enter " + (total_units-MAX_UNDERGRAD_UNITS) + " fewer units. \n"); flag=false; } if ((grad_student==true) && (total_units > MAX_GRAD_UNITS)) { System.out.print("Sorry, " + student_name + ", " + total_units + " is too many units.\n " + "Graduate students should take no more than " + MAX_GRAD_UNITS + " units;\n " + "Please try again, but enter " + (total_units-MAX_GRAD_UNITS) + " fewer units. \n"); flag=false; } if ((total_units>0) && (total_units <= MAX_UNDERGRAD_UNITS) && (!grad_student)) { if (total_units < 12) tuition = (total_units * UNDERGRAD_TUITION_UNIT_FEE); if ((total_units >=12) && (total_units <=18)) tuition = UNDERGRAD_TUITION_FLAT_FEE; if (total_units > 18) tuition = (UNDERGRAD_TUITION_FLAT_FEE + ((total_units - 18) * UNDERGRAD_TUITION_UNIT_FEE)); flag=true; } if ((total_units>0) && (total_units <= MAX_GRAD_UNITS) && (grad_student)) { if (total_units < 8) tuition = (total_units * GRAD_TUITION_UNIT_FEE); if ((total_units >=8) && (total_units <=12)) tuition = GRAD_TUITION_FLAT_FEE; if (total_units > 12) tuition = (GRAD_TUITION_FLAT_FEE + ((total_units - 12) * GRAD_TUITION_UNIT_FEE)); flag=true; } } while (flag==false); flag=false; //reset flag to false before each entry-validation routine do { System.out.print("Do you want a locker? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println(answer + " isn't a valid response. Please try again. "); } while (flag==false); if ((answer =='y') || (answer =='Y')) total_fees = total_fees + LOCKER_FEE; flag=false; //reset flag to false before each entry-validation routine do { System.out.print("Do you want a new student kit? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println("The letter \'" + answer + "\' isn't a valid response. Please try again. "); } while (flag==false); if ((answer =='y') || (answer =='Y')) total_fees=total_fees + NEW_STUDENT_KIT_FEE; flag=false; //reset flag to false before each entry-validation routine do { System.out.print("Do you want to register for the tutoring service? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println(answer + " isn't a valid response. Please try again. "); } while (flag==false); if ((answer =='y') || (answer =='Y')) total_fees = total_fees + TUTOR_SVC_FEE; flag=false; //reset flag to false before each entry-validation routine do { System.out.print("Would you like a university sweatshirt? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println("The letter \'" + answer + "\' isn't a valid response. Please try again. "); } while (flag==false); flag=false; //reset flag to false before each entry-validation routine if ((answer =='y') || (answer =='Y')) { total_fees = total_fees + GGU_SWEAT_FEE; sweat=true; do //shouldn't get performed unless student wants a sweatshirt { System.out.print("Please choose a size [(S)mall, (M)edium, (L)arge]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if ( ((answer =='s') || (answer =='S')) || ((answer =='m') || (answer =='M')) || ((answer=='l') || (answer=='L'))) { flag=true; if ((answer == 's') || (answer == 'S')) sweatshirt_size = "Small"; if ((answer == 'm') || (answer == 'M')) sweatshirt_size = "Medium"; if ((answer == 'l') || (answer == 'L')) sweatshirt_size = "Large"; } else System.out.println("The letter \'" + answer + "\' isn't a valid response. Please try again. "); } while (flag==false); }// end of sweatshirt if conditional with embedded do-loop for error checking flag=false; //reset flag to false before each entry-validation routine do { System.out.print("Do you want a parking permit? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println("The letter \'" + answer + "\' isn't a valid response. Please try again. "); } while (flag==false); if ((answer =='y') || (answer =='Y')) total_fees = total_fees + PARKING_FEE; flag=false; //reset flag to false before each entry-validation routine do { System.out.print("Would you like to purchase the ID/Debit card? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println("The letter \'" + answer + "\' isn't a valid response. Please try again. "); } while (flag==false); flag=false; //reset flag to false before each entry-validation routine if ((answer =='y') || (answer =='Y')) { total_fees = total_fees + ID_STORED_VALUE_CARD_FEE; do { System.out.print("Do you want to add money to the card now? [(Y)es or (N)o]: "); System.out.flush(); answer=stdin.readLine().charAt(0); if (((answer =='y') || (answer =='Y')) || ((answer =='n') | (answer =='N'))) flag=true; else System.out.println("The letter \'" + answer + "\' isn't a valid response. " + " Please try again. "); } while (flag==false); if ((answer =='y') || (answer =='Y')) { stored_value=true; do { System.out.print("In whole-dollar increments, how much money\n" + " (up to $500) do you want to add to the ID card? "); System.out.flush(); id_card_value=Integer.parseInt(stdin.readLine()); if (id_card_value<=0) System.out.println("Please enter a value greater than 0."); if (id_card_value>500) System.out.println("For security reasons, maximum value is $500. " + "Please enter a value up to $500. "); } while (id_card_value<=0 || id_card_value>500); }//end of nested if for stored value amount }//end of overarching if branch for stored value card NumberFormat money = NumberFormat.getCurrencyInstance(); //object for formatting output System.out.println(); System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Tuition and Fees Summary for: " + student_name); System.out.println("-----------------------------------------------"); System.out.println("Summer Semester, 2004"); System.out.println(); System.out.println("Your tuition due is: " + money.format(tuition)); System.out.println("Total fees: " + money.format(total_fees)); if (stored_value) System.out.println("ID-Debit card value: " + money.format(id_card_value)); System.out.println("------------------------------------------------"); System.out.println("Total due: " + money.format(tuition + total_fees + id_card_value)); System.out.println(); if (sweat) System.out.println("Your sweatshirt, size " + sweatshirt_size + ", will arrive in a few days."); }//main method }//TuitionCalculator class