Advertisement
Guest User

chpt3

a guest
Oct 4th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. import java.util.Scanner;//Required to read input from keyboard
  2.  
  3. /**
  4. * This program allows the user to order a textbook
  5. *
  6. * Enter your details here:
  7. * Name: Brad Bieselin
  8. * Lab Day and Time: Friday, 10:25am
  9. *
  10. */
  11. public class TextBookOrder {
  12. public static void main(String[] args){
  13. // Create a Scanner object to read input.
  14. Scanner keyboard = new Scanner (System.in);
  15.  
  16. String userName;//name of user buying textbook
  17. boolean discount = false;//discount flag set to false
  18. String ULevel;//accepts undergraduate level input as String
  19. char undergradLevel;//saves character entered for
  20. //undergraduate standing:SOPHOMORE(S),FRESHMEN(F),SENIOR(R),JUNIOR(J)
  21. int courseId=0; //Course id of course based on undergrad level
  22. double cost = 100.0; // Cost of the Textbook
  23. double tax;
  24. final double TAX_RATE = .08;//state tax rate
  25. final String DISCOUNT_CODE="UA_STUDENT_DISCOUNT"; //Financial discount code for textbook
  26. String userCode;
  27. String textbookName="";//name of the textbook to be bought
  28. //additional variables declared
  29. String studentInput; //prompts user if he/she is a student
  30. char firstChar; // takes the first character
  31. String upperCaseInput; // forces the letter to be uppercase if user writes lower case letter
  32. double discountCost; //Final cost of textbook after discount
  33. double finalCost; //Final cost of book after discount and tax
  34.  
  35.  
  36. // Prompt user and get first name.
  37. System.out.println("Welcome to UAlbany bookstore.");
  38.  
  39. /*TASK #1 add lines for task 1 here to check if profession of user is student before you
  40. continue with rest of your order*/
  41.  
  42. System.out.println("Are you a student?" +
  43. " Y = Yes, N = No");
  44. studentInput = keyboard.nextLine();
  45. upperCaseInput = studentInput.toUpperCase(); // convert it to uppercase, incase if user enters lower case y.
  46. firstChar = upperCaseInput.charAt(0);
  47.  
  48.  
  49.  
  50. if (firstChar == 'Y') {
  51.  
  52. //continue with rest of your order if user is student
  53. System.out.print("Enter your first name: ");
  54. userName = keyboard.nextLine();
  55.  
  56.  
  57. /* Determine if user is eligible for discount by
  58. having the same discount code as set in the variable DISCOUNT_CODE.
  59. ADD LINES HERE FOR TASK #2*/
  60.  
  61.  
  62. System.out.print("Enter your discount code if applies: ");
  63.  
  64. userCode=keyboard.nextLine();
  65. if (userCode.equals(DISCOUNT_CODE)) {
  66. discount = true;
  67. System.out.println("Correct code! You are eligible for a discount!");
  68.  
  69. }
  70.  
  71. else if (!userCode.equals(DISCOUNT_CODE)) {
  72. System.out.println("Invalid code. You are not eligible for a discount.");
  73. }
  74.  
  75. // Prompt user and get undergraduate standing choice between:
  76. //Freshmen, Sophomore, Junior and Senior.
  77.  
  78. System.out.println("Enter your undergraduate standing or level\n"
  79. + "between:\nFreshmen(F), Sophomore(S), Junior(J) and Senior(R):\n"
  80. + "Enter letter code only");
  81.  
  82. ULevel = keyboard.nextLine();
  83.  
  84.  
  85. //Add code here to extract only the character entered by user
  86.  
  87. undergradLevel = ULevel.charAt(0);
  88.  
  89. if (undergradLevel == 'F') {
  90. System.out.println("You are a Freshmen!");
  91. }
  92. else if (undergradLevel =='S') {
  93. System.out.println("You are a Sophomore!");
  94. }
  95. else if (undergradLevel =='J') {
  96. System.out.println("You are a Junior!");
  97. }
  98. else if (undergradLevel =='R') {
  99. System.out.println("You are a Senior!");
  100. }
  101. else {
  102. System.out.println("Enter either of the four options.");
  103. System.exit(0);
  104. }
  105.  
  106.  
  107. /* Enter the course id for which you need to buy the textbook.
  108. ADD LINES HERE FOR TASK #3A and #3B
  109. Set the textbook name and cost variables appropriately*/
  110.  
  111. // Task #3A
  112.  
  113. System.out.println("Enter your undergraduate standing or level\n"
  114. + "between:\nFreshmen(F), Sophomore(S), Junior(J) and Senior(R):\n"
  115. + "Enter letter code only");
  116.  
  117. ULevel = keyboard.nextLine(); // Takes the user's undergrad level
  118. upperCaseInput = ULevel.toUpperCase(); // Forces the input to upper case, incase user enters "f" which is not the same as F.
  119. firstChar = upperCaseInput.charAt(0); // Takes the first character of the input.
  120. switch (firstChar) {
  121.  
  122. case 'F':
  123. System.out.println("You are a Freshmen!");
  124. break;
  125.  
  126. case 'S':
  127. System.out.println("You are a Sophomore!");
  128. break;
  129.  
  130. case 'J':
  131. System.out.println("You are a Junior!");
  132. break;
  133.  
  134. case 'R':
  135. System.out.println("You are a Senior!");
  136. break;
  137.  
  138. default:
  139. System.out.println("Please enter something valid!");
  140. System.exit(0);
  141.  
  142. }
  143.  
  144.  
  145. // end of task #3A
  146.  
  147. // start of task #3B, switch statement to determine which undergrad level displays which textbooks.
  148.  
  149. System.out.println("\nList of undergrad level, corresponding required courses ids, textbooks and their cost:\n\t");
  150.  
  151. if (firstChar == 'F') {
  152.  
  153. switch (firstChar) { // Start of Switch Statement that displays the textbooks corresponding to user's undergrad level
  154.  
  155.  
  156. case 'F':
  157. System.out.println("\tFreshmen Level(F)-CourseID- 101-Textbook-Intro to Elements of Computing- $50");
  158. System.out.println("\tFreshmen Level(F)-CourseID- 107-Textbook-Intro to Web Programming- $55");
  159. break;
  160.  
  161. }
  162. }
  163.  
  164. else if (firstChar=='S') {
  165.  
  166. switch (firstChar) {
  167. case 'S':
  168. System.out.println("\tSophomore Level(S)-CourseID- 210-Textbook-Intro to Discrete Structure- $60");
  169. System.out.println("\tSophomore Level(S)-CourseID- 213-Textbook-Intro to Data Structure- $65");
  170. break;
  171. }
  172. }
  173.  
  174. else if (firstChar=='J') {
  175. switch (firstChar) {
  176. case 'J':
  177. System.out.println("\tJunior Level(J)-CourseID- 333-Textbook-Intro to Programming Hardware and Software- $70");
  178. System.out.println("\tJunior Level(J)-CourseID- 311-Textbook-Intro to Priciples of Programming Language- $75");
  179. break;
  180. }
  181. }
  182.  
  183. else if (firstChar=='R') {
  184. switch (firstChar) {
  185.  
  186. case 'R':
  187. System.out.println("\tSenior Level(R)-CourseID- 403-Textbook-Advanced Algorithms and Data Structures- $80");
  188. System.out.println("\tSenior Level(R)-CourseID- 405-Textbook-Advanced Object Oriented Programming- $90\n\n");
  189. break;
  190.  
  191. }
  192. }
  193. else {
  194. switch (firstChar) {
  195. default:
  196. System.out.println("Enter a valid value");
  197. break;
  198. }
  199. }
  200.  
  201.  
  202. // Start of 3B, Switch Statement
  203.  
  204. System.out.println("Enter the course Id would you like to buy a textbook for?");
  205.  
  206. switch (firstChar) {
  207.  
  208. case 'F':
  209. System.out.println("101-Textbook-Intro to Elements of Computing-$50\n107-Textbook-Intro to Web Programming- $55");
  210. break;
  211.  
  212. case 'S':
  213. System.out.println("210-Textbook-Intro to Discrete Structure- $60\n213-Textbook-Intro to Data Structure- $65");
  214. break;
  215.  
  216. case 'J':
  217. System.out.println("333-Textbook-Intro to Programming Hardware and Software- $70\n311-Textbook-Intro to Principles of Programming Language-$75");
  218. break;
  219.  
  220. case 'R':
  221. System.out.println("403-Textbook-Advanced Algorithms and Data Structures- $80\n405-Textbook-Advanced Object Oriented Programming- $90");
  222. break;
  223.  
  224. default:
  225. System.out.println("Enter in a proper value (F), (S), (J), (R)");
  226. break;
  227.  
  228.  
  229.  
  230. }
  231.  
  232. courseId = keyboard.nextInt();
  233.  
  234. // Consume the remaining newline character.
  235. keyboard.nextLine();
  236.  
  237. // Set the courseId, textbook cost and textbook name variables appropriately.
  238.  
  239.  
  240. switch (courseId){
  241.  
  242. case 101:
  243. textbookName = "Intro to Elements of Computing";
  244. cost = 50.0;
  245. System.out.println(textbookName + " is $" +cost);
  246. break;
  247.  
  248. case 107:
  249. textbookName = "Intro to Web Programming";
  250. cost = 55.0;
  251. System.out.println(textbookName + " is $" +cost);
  252. break;
  253.  
  254. case 210:
  255. textbookName = "Intro to Discrete Structure";
  256. cost = 60.0;
  257. System.out.println(textbookName + " is $" +cost);
  258. break;
  259.  
  260. case 213:
  261. textbookName = "Intro to Data Structure";
  262. cost = 65.0;
  263. System.out.println(textbookName + "is $" +cost);
  264. break;
  265.  
  266. case 333:
  267. textbookName = "Intro to Programming Hardware and Software";
  268. cost = 70.0;
  269. System.out.println(textbookName + "is $" + cost);
  270. break;
  271.  
  272. case 311:
  273. textbookName = "Intro to Priciples of Programming Language";
  274. cost = 75.0;
  275. System.out.println(textbookName + "is $" + cost);
  276. break;
  277.  
  278. case 403:
  279. textbookName = "Advanced Algorithms and Data Structures";
  280. cost = 80.0;
  281. System.out.println(textbookName + "is $" + cost);
  282. break;
  283.  
  284. case 405:
  285. textbookName = "Advanced Object Oriented Programming";
  286. cost = 90.0;
  287. System.out.println(textbookName + "is $" + cost);
  288. break;
  289.  
  290. default:
  291. System.out.println("Invalid Course ID");
  292. System.exit(0);
  293. break;
  294.  
  295.  
  296.  
  297.  
  298.  
  299. }
  300.  
  301.  
  302. /* Apply discount if user is eligible.
  303. Apply $25 discount if user financial aid code
  304. matches with DISCOUNT_CODE variable
  305.  
  306. ADD LINES FOR TASK #4 HERE*/
  307.  
  308. discountCost = cost;
  309.  
  310. if (discount) {
  311.  
  312. discountCost -= 25.0;
  313. System.out.println("You are eligible for the discount.");
  314.  
  315.  
  316. }
  317.  
  318. else {
  319.  
  320. }
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328. // EDIT PROGRAM FOR TASK #5
  329. // SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
  330. System.out.printf("\nThe cost of your order " +
  331. "is: $%.2f\n", discountCost);
  332.  
  333. // Calculate and display tax and total cost.
  334. tax = discountCost * TAX_RATE;
  335. System.out.printf("The tax is: $%.2f\n", tax);
  336. System.out.printf("The total due is: $%.2f\n",
  337. (tax + discountCost));
  338.  
  339. finalCost = tax + discountCost;
  340.  
  341. System.out.printf("\nHello "+userName+", you ordered the textbook:"+textbookName+
  342. " with courseId:"+courseId+" with a cost of:$%.2f\n", finalCost);
  343. System.out.println("Your order is ready " +
  344. "for pickup at UAlbany bookstore.");
  345.  
  346. }
  347.  
  348. else {
  349. System.out.println("Sorry! The bookstore is only available to UAlbany students.");
  350. System.exit(0);
  351. }
  352.  
  353. }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement