Page 155 - CA_Blue( J )_Class10
P. 155

Type of coaches              Amount
                                            First_AC                            700
                                            Second_AC                           500
                                            Third_AC                            250
                                            sleeper                             None
                            void display()    :       to display all  details of a customer  such as name, coach,  total amount and  mobile
                                                     number.
                        Write a main() method to create an object of the class and call the above methods.          [2018]

                   Ans.  import java.util.*;
                      class RailwayTicket
                      {
                          String name,coach;
                          long mobno;
                          int amt,totalamt;
                          void accept()
                          {
                              Scanner sc= new Scanner(System.in);
                              System.out.print("Enter Name: ");
                              name = sc.next();
                              System.out.print("Enter Coach: ");
                              coach = sc.next();
                              System.out.print("Mobile number: ");
                              mobno = sc.nextLong();
                              System.out.print("Amount: ");
                              amt = sc.nextInt();
                          }
                          void update()
                          {
                              if(coach.equalsIgnoreCase("First_AC"))
                                  totalamt = amt + 700;
                              else if(coach.equalsIgnoreCase("Second_AC"))
                                  totalamt = amt + 500;
                              else if(coach.equalsIgnoreCase("Third_AC"))
                                  totalamt = amt + 250;
                          }
                          void display()
                          {
                              System.out.println("Name"+"\t"+"Coach"+"\t"+"Total Amount"+"\t"+"Mobile"
                       +"number");
                              System.out.println(name+"\t"+coach+"\t"+totalamt+"\t\t"+mobno);
                          }
                          public static void main()
                          {
                              RailwayTicket obj = new RailwayTicket();
                              obj.accept();
                              obj.update();
                              obj.display();
                          }
                      }














                                                                                                                       153
                                                                                           Conditional Constructs in Java  153
   150   151   152   153   154   155   156   157   158   159   160