Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class BusFares extends Applet implements ActionListener {
  6.     //declare global variables
  7.     Label p1, p2, p3, p4;
  8.     TextField age, tType, zone;
  9.     Button order;
  10.     String economy, premium;
  11.     boolean student, pensioner;
  12.     double tPrice;
  13.     int z;
  14.  
  15.     public void init() {
  16.         //instantiate variables
  17.         p1 = new Label("Enter your age, ticket type (economy or premium) and desired zone (1, 2 or 3).");
  18.         p2 = new Label("Age:");
  19.         p3 = new Label("Type:");
  20.         p4 = new Label("Zone:");
  21.         age = new TextField(10);
  22.         tType = new TextField(10);
  23.         zone = new TextField(10);
  24.         order = new Button("Order Ticket");
  25.        
  26.         //add elements in applet
  27.         add (p1);
  28.         add (p2);
  29.         add (age);
  30.         add (p3);
  31.         add (tType);
  32.         add (p4);
  33.         add (zone);
  34.         add (order);
  35.        
  36.         //add ActionListener to the button 'order'
  37.         order.addActionListener(this);
  38.         age.addActionListener(this);
  39.         tType.addActionListener(this);
  40.         zone.addActionListener(this);
  41.     }
  42.  
  43.     public void actionPerformed(ActionEvent e) {
  44.         //instantiate variables
  45.         student = Integer.parseInt(age.getText()) < 18;
  46.         pensioner = Integer.parseInt(age.getText()) >= 64;
  47.         z = Integer.parseInt(zone.getText());
  48.         economy = tType.getText().trim();
  49.         premium = tType.getText().trim();
  50.        
  51.         //setting price (tPrice) according to ticket type (tType) - economy or premium
  52.         if (economy.equals("economy")) {
  53.             switch (z) {
  54.                 case 1: tPrice = 4;
  55.                 break;
  56.                 case 2: tPrice = 6;
  57.                 break;
  58.                 case 3: tPrice = 7;
  59.                 break;
  60.                 default: showStatus("Economy: zone fail");
  61.             }
  62.         }
  63.         else if (premium.equals("premium")) {
  64.             switch (z) {
  65.                 case 1: tPrice = 6;
  66.                 break;
  67.                 case 2: tPrice = 7;
  68.                 break;
  69.                 case 3: tPrice = 7;
  70.                 break;
  71.                 default: showStatus("Premium: zone fail");
  72.             }
  73.         }
  74.         else showStatus("tType fail");
  75.  
  76.         //determining discount based on age - $5 cap after discount
  77.         if (student || pensioner){
  78.             tPrice = tPrice*0.8;
  79.             if (tPrice > 5) tPrice = 5;
  80.         }
  81.         else showStatus("Not student/pensioner");
  82.         repaint();
  83.     }
  84.  
  85.     public void paint (Graphics g) {
  86.         g.drawString("Your ticket fee is $" + tPrice + ".", 20,100);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement