Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Movie ticket calculation

In a multiplex theater, there is a discount scheme announced where one gets a 10% discount on the total cost of tickets when there is a bulk booking of more than 20 tickets, and a discount of 2% on the total cost of tickets if a special coupon card is submitted. Develop a program to find the total cost as per the scheme. The cost of the king class ticket is Rs.75 and queen class is Rs.150. Refreshments can also be opted by paying an additional of Rs. 50 per member.

Hint: k-king and q-queen and You have to book minimum of 5 tickets and maximum of 40 at a time. If fails display "Minimum of 5 and Maximum of 40 Tickets". If circle is given a value other than 'k' or 'q' the output should be "Invalid Input".

The ticket cost should be printed exactly to two decimal places.

Sample Input 1:
Enter the no of ticket:35
Do you want refreshment:y
Do you have coupon code:y
Enter the circle:k

Sample Output 1:
Ticket cost:4065.25

What I have tried:

Java
import java.util.*;

public class CinemaTicket {
    public static void main(String[] args) {
       int t,refe;
        double rate,sum,sum1,sum2,sum3;
        String cou,ref,circle;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the no of ticket:");
        t = s.nextInt();
        if (t<5 && t> 40) 
        {
            System.out.println("Minimum of 5 and Maximum of 40 tickets");
        }

        System.out.println("Do you want refreshment:");
        ref = s.next();
        System.out.println("Do you have a coupon code:");
        co = s.next();
        System.out.println("Enter the circle:");
        circle = s.next();
        if (circle.equals("k")) {
            cost = t * 75;
        } else if (circle.equals("q")) {
            total = no * 150;
        } else {
            System.out.println("Invalid Input");
        }
        if (t> 20) {
            sum = ((0.1) * cost);
            sum1 = cost- sum;
            if (cou.equals("y")) {
                sum2 = ((0.2) * cost);
                sum3 = sum1 - sum2;
                if (ref.equals("y")) {
                    refe = t* 150;
                    rate = sum3 + refe;
                } else {
                    rate = sum3;
                }
            } else {
                rate = sum1;
            }
        } else {
            rate = cost;
        }
        System.out.printf("Ticket cost %.2f:",+rate);
    }
}
Posted
Updated 23-Jun-20 14:44pm
v2
Comments
Richard MacCutchan 27-Jan-20 6:33am    
What is the question?
Yaswanth Kummar 27-Jan-20 7:11am    
My output is different from the expected output ...
If we give coupon =n
It is not executing ref and directly printing the result
Richard MacCutchan 27-Jan-20 8:02am    
So are we supposed to guess what the expected output is?
Richard MacCutchan 27-Jan-20 8:08am    
What I can say is that the above code will not even compile. Look at the line follpwing } else if (circle.equals("q")) {; you have two variables that are not defined anywhere. And with all those different variables it is little wonder the answer does not come out right. you should start with the number of tickets and apply the cost based on the seats selected. That gives the total gross cost. Then apply any discounts based on the coupons. And finally apply any extras (refreshments etc.). That should give the final net cost.
Patrice T 27-Jan-20 6:36am    
And you have a question or a problem ?

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Quote:
...getting wrong output

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

[Update]
Quote:
My output is different from the expected output ...
If we give coupon =n
It is not executing ref and directly printing the result

It is because it is what you told your code to do !
The ref code do not execute if coupon="n" because the ref code" is inside the coupon="y" code.
 
Share this answer
 
v2
import java.util.*;
public class CinemaTicket
{
    public static void main(String args[])
    {
        int no;
        double cost,total=0;
        String ref,co,cir,cl;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the no of ticket:");
        no=sc.nextInt();
        System.out.println("Do you want refreshment:");
        ref=sc.next();
        System.out.println("Do you have coupon code:");
        co=sc.next();
        System.out.println("Enter the circle:");
        cir=sc.next();
        if(no<5 || no>40)
        {
            System.out.println("Minimum of 5 and Maximum of 40 Tickets");
            return;
        }
        if(cir.charAt(0) == 'k')
            cost=75*no;
        else if(cir.charAt(0) == 'q')
            cost=150*no;
        else
        {
         System.out.println("Invalid Input");
         return;
        }
        total=cost;
        if(no>20)
            cost= cost - ((0.1)*cost);
        total=cost;
        if(co.charAt(0)== 'y')
            total= cost - ((0.02)*cost);
        if(ref.charAt(0)== 'y')
            total += (no*50);
        System.out.format("Ticket cost:%.2f",total);
    }
}
 
Share this answer
 
import java.util.Scanner;
import java.text.DecimalFormat;

public class CinemaTicket {

public static void main(String[] args) {

int noTicket;
double total = 0,cost;
String ref, co , circle;
Scanner s = new Scanner(System.in);
System.out.println("Enter the no of ticket:");
noTicket = s.nextInt();
if (noTicket < 5 || noTicket > 40) {
System.out.println("Minimum of 5 and Maximum of 40 tickets");
System.exit(0);
}
System.out.println("Do you want refreshment:");
ref = s.next();
System.out.println("Do you have coupon code:");
co = s.next();
System.out.println("Enter the circle:");
circle = s.next();
if(circle.charAt(0) == 'k')
cost=75*noTicket;
else if(circle.charAt(0) == 'q')
cost=150*noTicket;
else
{
System.out.println("Invalid Input");
return;
}
total=cost;
if(noTicket>20)
cost= cost - ((0.1)*cost);
total=cost;
if(co.charAt(0)== 'y')
total= cost - ((0.02)*cost);
if(ref.charAt(0)== 'y')
total += (noTicket*50);
System.out.format("Ticket cost:%.2f",total);
}
}
 
Share this answer
 
Comments
CHill60 24-Jun-20 0:14am    
An unexplained, unformatted code dump is not a good solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900