Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 3 Ticket.java Write a Java program called Ticket.java that prompts (asks) the user to enter a ticket number. The format of a valid ticket

Task 3 Ticket.java Write a Java program called Ticket.java that prompts (asks) the user to enter a ticket number. The format of a valid ticket is DDDC[CDD][C] where D = a required digit and C = a required character. [C] means that the character at this position is optional and [D] means the digit at this position is optional. Given the specification above, any ticket entered by the user that has a length of less than 4 or greater than 8, must automatically be invalid. In that case, an appropriate message must be displayed to the screen and no further processing is done. The DDD represents the starting price of a ticket. As in Task 2, this must be converted from characters into an integer. Since the Ticket has at least one character, it is necessary to take in the input as a String. It is necessary to check that the first three characters are digits, without using Integer.parseInt( ) Use the techniques you applied in Task 2 Once you have the integer, then the base cost of the ticket is the integer divided by 10, as a double. If any of the first three characters are not digits, then an appropriate message is displayed to the screen and no further processing is done, and no cost is displayed to the screen. Assuming that the first 3 characters are digits, then the next character must be either 'F', 'M' or 'B' If that fourth character is not one of the above, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. If the fourth character is an 'F' then the base cost of the ticket is increased by 20%, if the fourth character is an 'M', then the base cost of the ticket is increased by 10%, if the fourth character is a 'B', then the base cost of the ticket remains the same. If there are more characters in the ticket, then there must 3 extra characters. If there are less than 3 characters, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. No cost is to be displayed to the screen. OOF Assignment Part A - due: 10:00 am Mon 27th Mar this is the first and final hand in date p. 8 of 11 If there are 3 characters, then the first character of this group of 3 must be either 'D' or 'S', if the character is not one of these, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. No cost is to be displayed to the screen. If the character is either 'D' or 'S' then the next 2 characters must be digits. If they are not digits, then the ticket is invalid, an appropriate message is displayed to the screen and no further processing is done. No cost is to be displayed to the screen. If the 2 characters are digits, they are converted into an integer, using the techniques described above, Integer.parseInt( ) is not to be used (this task will be awarded 0 if it is). If the first character (of this group) is 'D', then the 2 digits become the discount that is applied to the cost of the ticket, as worked out above. For example, if the base cost of the ticket had been calculated as $20.00 and the 3 characters were D50, then the discount would be 50%, resulting in the new cost being $10.00 If the first character (of this group) is 'S', then the 2 digits become the surcharge (extra) that is applied to the cost of the ticket, as worked out above. For example, if the base count of the had been calculated as $20.00 and the 3 characters were S50, then the surcharge would be 50%, resulting in the new cost being $30.00 If there is one more character at the end of the ticket, then that character must be either an 'S' or a 'P'. If there is an extra character and it is not one of 'S' or 'P' then the ticket is invalid, an appropriate message is displayed to the screen and no cost is displayed to the screen. If the character is an 'S' then final cost of the ticket is reduced by 10%. If the character is a 'P' then the final cost of the ticket is reduced by 15% So an example of a valid ticket could be: 735MS05P There are many reasons why a user Ticket may be invalid, even if it is within the correct length range. Every effort should be made to give the exact reason why the ticket is invalid. For example, wrong length, doesn't start with digits, doesn't have a valid character in the right place, doesn't have digits in the right place. Part of your task is to list all the conditions that make a ticket invalid and write code to cover those cases. User input that should be accepted in both upper and lower case. That is the ticket input must be case insensitive. At first, this Task can seem quite complex. Here is a suggested strategy to break the problem down into smaller parts, to "eat the dragon in little bits". Start by checking the length of the user input. If the length is not between 4 and 8 inclusive, then the Ticket is invalid, nothing further needs to be done except to display the appropriate error message. If the length is correct, solve the problem assuming that the user enters a correct set of just digits, that is, three digits and a valid character. OOF Assignment Part A - due: 10:00 am Mon 27th Mar this is the first and final hand in date p. 9 of 11 Once you have this working correctly, solve the problem assuming the user correctly enters an optional group of a valid character and 2 digits. Then extend the program to solve the problem for the user entering a correct optional character at the end of the Ticket. Once these are working, then start working on testing for user input errors and build up your program bit by bit to detect these errors and display the appropriate error messages. Some sample runs of the program are shown below (user input is in bold): NOTE: the samples DO NOT cover all of reasons a ticket can be invalid, that is, they do not cover all test cases. Identifying and covering all of the cases is part of your Task. The samples do not always display appropriate invalid messages in most cases, your assignment submission will display the appropriate messages. > java Ticket Enter ticket >> 400F Ticket cost = $48.0 > java Ticket Enter ticket >> 50FF Not a valid ticket Third character must be a digit > java Ticket Enter ticket >> 230MS50 Ticket cost = $37.95 > java Ticket Enter ticket >> 400t Not a valid ticket Fourth character must be one of 'F', 'M' or 'B' > java Ticket Enter ticket >> 500FS4 Not a valid ticket Valid tickets cannot be length 6 > java Ticket Enter ticket >> 100BS10P Ticket cost = $9.35 Note that we haven't yet learnt how to control the number of decimal places that are displayed so this will not be an issue. please need help urgently thank you in advance :)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions