Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;//Required to read input from keyboard
- /**
- * This program allows the user to order a textbook
- *
- * Enter your details here:
- * Name: Brad Bieselin
- * Lab Day and Time: Friday, 10:25am
- *
- */
- public class TextBookOrder {
- public static void main(String[] args){
- // Create a Scanner object to read input.
- Scanner keyboard = new Scanner (System.in);
- String userName;//name of user buying textbook
- boolean discount = false;//discount flag set to false
- String ULevel;//accepts undergraduate level input as String
- char undergradLevel;//saves character entered for
- //undergraduate standing:SOPHOMORE(S),FRESHMEN(F),SENIOR(R),JUNIOR(J)
- int courseId=0; //Course id of course based on undergrad level
- double cost = 100.0; // Cost of the Textbook
- double tax;
- final double TAX_RATE = .08;//state tax rate
- final String DISCOUNT_CODE="UA_STUDENT_DISCOUNT"; //Financial discount code for textbook
- String userCode;
- String textbookName="";//name of the textbook to be bought
- //additional variables declared
- String studentInput; //prompts user if he/she is a student
- char firstChar; // takes the first character
- String upperCaseInput; // forces the letter to be uppercase if user writes lower case letter
- double discountCost; //Final cost of textbook after discount
- double finalCost; //Final cost of book after discount and tax
- // Prompt user and get first name.
- System.out.println("Welcome to UAlbany bookstore.");
- /*TASK #1 add lines for task 1 here to check if profession of user is student before you
- continue with rest of your order*/
- System.out.println("Are you a student?" +
- " Y = Yes, N = No");
- studentInput = keyboard.nextLine();
- upperCaseInput = studentInput.toUpperCase(); // convert it to uppercase, incase if user enters lower case y.
- firstChar = upperCaseInput.charAt(0);
- if (firstChar == 'Y') {
- //continue with rest of your order if user is student
- System.out.print("Enter your first name: ");
- userName = keyboard.nextLine();
- /* Determine if user is eligible for discount by
- having the same discount code as set in the variable DISCOUNT_CODE.
- ADD LINES HERE FOR TASK #2*/
- System.out.print("Enter your discount code if applies: ");
- userCode=keyboard.nextLine();
- if (userCode.equals(DISCOUNT_CODE)) {
- discount = true;
- System.out.println("Correct code! You are eligible for a discount!");
- }
- else if (!userCode.equals(DISCOUNT_CODE)) {
- System.out.println("Invalid code. You are not eligible for a discount.");
- }
- // Prompt user and get undergraduate standing choice between:
- //Freshmen, Sophomore, Junior and Senior.
- System.out.println("Enter your undergraduate standing or level\n"
- + "between:\nFreshmen(F), Sophomore(S), Junior(J) and Senior(R):\n"
- + "Enter letter code only");
- ULevel = keyboard.nextLine();
- //Add code here to extract only the character entered by user
- undergradLevel = ULevel.charAt(0);
- if (undergradLevel == 'F') {
- System.out.println("You are a Freshmen!");
- }
- else if (undergradLevel =='S') {
- System.out.println("You are a Sophomore!");
- }
- else if (undergradLevel =='J') {
- System.out.println("You are a Junior!");
- }
- else if (undergradLevel =='R') {
- System.out.println("You are a Senior!");
- }
- else {
- System.out.println("Enter either of the four options.");
- System.exit(0);
- }
- /* Enter the course id for which you need to buy the textbook.
- ADD LINES HERE FOR TASK #3A and #3B
- Set the textbook name and cost variables appropriately*/
- // Task #3A
- System.out.println("Enter your undergraduate standing or level\n"
- + "between:\nFreshmen(F), Sophomore(S), Junior(J) and Senior(R):\n"
- + "Enter letter code only");
- ULevel = keyboard.nextLine(); // Takes the user's undergrad level
- upperCaseInput = ULevel.toUpperCase(); // Forces the input to upper case, incase user enters "f" which is not the same as F.
- firstChar = upperCaseInput.charAt(0); // Takes the first character of the input.
- switch (firstChar) {
- case 'F':
- System.out.println("You are a Freshmen!");
- break;
- case 'S':
- System.out.println("You are a Sophomore!");
- break;
- case 'J':
- System.out.println("You are a Junior!");
- break;
- case 'R':
- System.out.println("You are a Senior!");
- break;
- default:
- System.out.println("Please enter something valid!");
- System.exit(0);
- }
- // end of task #3A
- // start of task #3B, switch statement to determine which undergrad level displays which textbooks.
- System.out.println("\nList of undergrad level, corresponding required courses ids, textbooks and their cost:\n\t");
- if (firstChar == 'F') {
- switch (firstChar) { // Start of Switch Statement that displays the textbooks corresponding to user's undergrad level
- case 'F':
- System.out.println("\tFreshmen Level(F)-CourseID- 101-Textbook-Intro to Elements of Computing- $50");
- System.out.println("\tFreshmen Level(F)-CourseID- 107-Textbook-Intro to Web Programming- $55");
- break;
- }
- }
- else if (firstChar=='S') {
- switch (firstChar) {
- case 'S':
- System.out.println("\tSophomore Level(S)-CourseID- 210-Textbook-Intro to Discrete Structure- $60");
- System.out.println("\tSophomore Level(S)-CourseID- 213-Textbook-Intro to Data Structure- $65");
- break;
- }
- }
- else if (firstChar=='J') {
- switch (firstChar) {
- case 'J':
- System.out.println("\tJunior Level(J)-CourseID- 333-Textbook-Intro to Programming Hardware and Software- $70");
- System.out.println("\tJunior Level(J)-CourseID- 311-Textbook-Intro to Priciples of Programming Language- $75");
- break;
- }
- }
- else if (firstChar=='R') {
- switch (firstChar) {
- case 'R':
- System.out.println("\tSenior Level(R)-CourseID- 403-Textbook-Advanced Algorithms and Data Structures- $80");
- System.out.println("\tSenior Level(R)-CourseID- 405-Textbook-Advanced Object Oriented Programming- $90\n\n");
- break;
- }
- }
- else {
- switch (firstChar) {
- default:
- System.out.println("Enter a valid value");
- break;
- }
- }
- // Start of 3B, Switch Statement
- System.out.println("Enter the course Id would you like to buy a textbook for?");
- switch (firstChar) {
- case 'F':
- System.out.println("101-Textbook-Intro to Elements of Computing-$50\n107-Textbook-Intro to Web Programming- $55");
- break;
- case 'S':
- System.out.println("210-Textbook-Intro to Discrete Structure- $60\n213-Textbook-Intro to Data Structure- $65");
- break;
- case 'J':
- System.out.println("333-Textbook-Intro to Programming Hardware and Software- $70\n311-Textbook-Intro to Principles of Programming Language-$75");
- break;
- case 'R':
- System.out.println("403-Textbook-Advanced Algorithms and Data Structures- $80\n405-Textbook-Advanced Object Oriented Programming- $90");
- break;
- default:
- System.out.println("Enter in a proper value (F), (S), (J), (R)");
- break;
- }
- courseId = keyboard.nextInt();
- // Consume the remaining newline character.
- keyboard.nextLine();
- // Set the courseId, textbook cost and textbook name variables appropriately.
- switch (courseId){
- case 101:
- textbookName = "Intro to Elements of Computing";
- cost = 50.0;
- System.out.println(textbookName + " is $" +cost);
- break;
- case 107:
- textbookName = "Intro to Web Programming";
- cost = 55.0;
- System.out.println(textbookName + " is $" +cost);
- break;
- case 210:
- textbookName = "Intro to Discrete Structure";
- cost = 60.0;
- System.out.println(textbookName + " is $" +cost);
- break;
- case 213:
- textbookName = "Intro to Data Structure";
- cost = 65.0;
- System.out.println(textbookName + "is $" +cost);
- break;
- case 333:
- textbookName = "Intro to Programming Hardware and Software";
- cost = 70.0;
- System.out.println(textbookName + "is $" + cost);
- break;
- case 311:
- textbookName = "Intro to Priciples of Programming Language";
- cost = 75.0;
- System.out.println(textbookName + "is $" + cost);
- break;
- case 403:
- textbookName = "Advanced Algorithms and Data Structures";
- cost = 80.0;
- System.out.println(textbookName + "is $" + cost);
- break;
- case 405:
- textbookName = "Advanced Object Oriented Programming";
- cost = 90.0;
- System.out.println(textbookName + "is $" + cost);
- break;
- default:
- System.out.println("Invalid Course ID");
- System.exit(0);
- break;
- }
- /* Apply discount if user is eligible.
- Apply $25 discount if user financial aid code
- matches with DISCOUNT_CODE variable
- ADD LINES FOR TASK #4 HERE*/
- discountCost = cost;
- if (discount) {
- discountCost -= 25.0;
- System.out.println("You are eligible for the discount.");
- }
- else {
- }
- // EDIT PROGRAM FOR TASK #5
- // SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
- System.out.printf("\nThe cost of your order " +
- "is: $%.2f\n", discountCost);
- // Calculate and display tax and total cost.
- tax = discountCost * TAX_RATE;
- System.out.printf("The tax is: $%.2f\n", tax);
- System.out.printf("The total due is: $%.2f\n",
- (tax + discountCost));
- finalCost = tax + discountCost;
- System.out.printf("\nHello "+userName+", you ordered the textbook:"+textbookName+
- " with courseId:"+courseId+" with a cost of:$%.2f\n", finalCost);
- System.out.println("Your order is ready " +
- "for pickup at UAlbany bookstore.");
- }
- else {
- System.out.println("Sorry! The bookstore is only available to UAlbany students.");
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement