car park pay system


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: car park pay system

  1. #1
    Join Date
    Aug 2005
    Posts
    11

    car park pay system

    hello all i need help asap i need this sorted by today if possible, thanks. i am new to this so sorry if i have made any mistakes posting this up

    i have this code below :-

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    //<applet code=CarPark2.class width=400 height=150></applet>
    public class CarPark2 extends Applet implements ActionListener {


    private int balance;
    private int time;
    private Button tenP, twentyP, fiftyP, onePound, twoPounds,
    less1, oneTwo, twoThree, threeFour, overFour;

    private Button change;
    TextField message = new TextField(20);
    public void putCoin(int money)
    {
    if ( money == 10 |money == 20 |money == 50 |money == 100);
    balance = balance + money;
    }

    public int cancel()
    {
    int refund = balance;
    balance = 0;
    return refund;
    }

    public String getTicket(){
    String s = "change";
    return s;

    int time = 0;
    if (balance < 20)
    {
    time = 0;
    }
    else if (balance > 160)
    {
    balance -= 160;
    time = 240;
    }
    else
    {
    int timeSegments = balance / 10;
    time = timeSegments * 15;
    balance = (balance % 10);
    }

    }


    public void init() {
    setLayout(new BorderLayout());

    tenP = new Button("10p"); ;tenP.setEnabled(true);
    add(tenP);
    tenP.addActionListener(this);
    twentyP = new Button("20p"); twentyP.setEnabled(true);
    add(twentyP);
    twentyP.addActionListener(this);
    fiftyP = new Button("50p"); fiftyP.setEnabled(true);
    add(fiftyP);
    fiftyP.addActionListener(this);
    onePound = new Button("1"); onePound.setEnabled(true);
    add(onePound);
    onePound.addActionListener(this);
    twoPounds = new Button("2"); twoPounds.setEnabled(true);
    add(twoPounds);
    twoPounds.addActionListener(this);
    less1 = new Button("Up to 1 hr");
    add(tenP);
    tenP.addActionListener(this);
    oneTwo = new Button("1 to 2 hr");
    add(tenP);
    tenP.addActionListener(this);
    twoThree = new Button("2 to 3 hr");
    add(tenP);
    tenP.addActionListener(this);
    threeFour = new Button("3 to 4 hr");
    add(tenP);
    tenP.addActionListener(this);
    overFour = new Button("Over 4 hr");
    add(tenP);
    tenP.addActionListener(this);


    message = new TextField(20);

    Panel leftSide = new Panel();
    leftSide.setLayout(new GridLayout(5,1));
    leftSide.add(tenP);
    leftSide.add(twentyP);
    leftSide.add(fiftyP);
    leftSide.add(onePound);
    leftSide.add(twoPounds);
    add("West", leftSide);

    Panel rightSide = new Panel();
    rightSide.setLayout(new GridLayout(5, 1));
    rightSide.add(less1);
    rightSide.add(oneTwo);
    rightSide.add(twoThree);
    rightSide.add(threeFour);
    rightSide.add(overFour);
    add("East", rightSide);

    Panel middle = new Panel();
    middle.setLayout(new FlowLayout());
    middle.add(message);
    add("Center", middle);
    }
    public void actionPerformed (ActionEvent event) {
    balance = balance +1
    }

    }


    The program should:
    1.allow the customer to select the number of hours car parking to be purchased
    2.display the amount payable
    3.accept payment from the customer until the full payment is received. A suitable message should be displayed if the payment is correct. If the payment is too much the change due should be displayed.

    i really need help with this plz.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I'm on it, am i right in assuming the price for parking is:
    0-1: 10p
    1-2: 20p
    2-3: 50p
    3-4: 1
    4+ : 2

    ?
    eschew obfuscation

  3. #3
    Join Date
    Aug 2005
    Posts
    11
    hello yes u are correct those are the correct prices and figures

  4. #4
    Join Date
    Aug 2005
    Posts
    11
    sorry my mistake th figures are

    Hours Amount payable
    0-1 1.00
    1-2 2.00
    2-3 3.50
    3-4 5.00
    4+ 6.00

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here it is, seems to be bugfree now...

    ...and this is the way I code it

    It can be run as an application also, it has a main() method so you can
    run the applet(frame) from the console. If you only want to hand in an
    applet then delete the main() method. Note, it runs fine as an applet
    even with the main() method there.

    I often use this hybrid technique during applet development, it saves me
    from the browser caching and I dont need a browser's java console to see
    what bugs out..

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.util.*;
    import java.text.*;
    
    //<applet code=CarPark2.class width=400 height=150></applet>
    
    public class CarPark2 extends Applet implements ActionListener {
    
      static DecimalFormat df=new DecimalFormat("0.00");
      // hashtable for mapping the pay buttons' text to the payment
      static Hashtable priceTab=new Hashtable();
      static {
        priceTab.put("10p",new Double(0.10d));
        priceTab.put("20p",new Double(0.20d));
        priceTab.put("50p",new Double(0.50d));
        priceTab.put("1",new Double(1.0d));
        priceTab.put("2",new Double(2.0d));
      }
      // hashtable for mapping the time buttons' text to the price
      static Hashtable timePriceTab=new Hashtable();
      static {
        timePriceTab.put("Up to 1 hr",new Double(1.0d));
        timePriceTab.put("1 to 2 hr",new Double(2.0d));
        timePriceTab.put("2 to 3 hr",new Double(3.5d));
        timePriceTab.put("3 to 4 hr",new Double(5.0d));
        timePriceTab.put("Over 4 hr",new Double(6.0d));
      }
    
      private double balance;
      private double priceToPay;
    
      private Button tenP, twentyP, fiftyP, onePound, twoPounds,
          less1, oneTwo, twoThree, threeFour, overFour;
    
      private Button cancelBtn;
      private TextArea message = null;
      private Panel leftSide = new Panel();
      private Panel rightSide = new Panel();
    
    
    
      public void init() {
        setLayout(new BorderLayout());
    
        tenP = new Button("10p"); ;
        twentyP = new Button("20p");
        fiftyP = new Button("50p");
        onePound = new Button("1");
        twoPounds = new Button("2");
        less1 = new Button("Up to 1 hr");
        oneTwo = new Button("1 to 2 hr");
        twoThree = new Button("2 to 3 hr");
        threeFour = new Button("3 to 4 hr");
        overFour = new Button("Over 4 hr");
    
        message = new TextArea();
        message.setEditable(false);
    
    
        leftSide.setLayout(new GridLayout(5, 1));
        leftSide.add(tenP);
        leftSide.add(twentyP);
        leftSide.add(fiftyP);
        leftSide.add(onePound);
        leftSide.add(twoPounds);
        add("West", leftSide);
    
        rightSide.setLayout(new GridLayout(5, 1));
        rightSide.add(less1);
        rightSide.add(oneTwo);
        rightSide.add(twoThree);
        rightSide.add(threeFour);
        rightSide.add(overFour);
        add("East", rightSide);
    
        Panel middle = new Panel();
        middle.setLayout(new GridLayout());
        middle.add(message);
        add("Center", middle);
    
        cancelBtn=new Button("Cancel");
        add("South",cancelBtn);
    
        hookUpButtons(this);
        resetMachine(false);
      }
      /**
       * Finds all buttons within a container (and its subcontainers)
       * and hooks them up to the actionlistener
       */
      private void hookUpButtons (Container cont) {
        Component [] cmp=cont.getComponents();
        for (int i=0; i<cmp.length; i++) {
          if (cmp[i] instanceof Button) {
            ((Button)cmp[i]).addActionListener(this); // hook up
          } else if (cmp[i] instanceof Container) {
            hookUpButtons((Container)cmp[i]); // recursion to subcontainer
          }
        }
      }
      /**
       * Enable/disable all buttons w.in a container
       */
      private void enableButtons (Container cont, boolean enabled) {
        Component [] cmp=cont.getComponents();
        for (int i=0; i<cmp.length; i++) {
          if (cmp[i] instanceof Button) {
            ((Button)cmp[i]).setEnabled(enabled);
          }
        }
      }
      /**
       * Initialize all payment
       */
      private void resetMachine (boolean isCancel) {
        enableButtons(leftSide,false);
        enableButtons(rightSide,true);
        cancelBtn.setEnabled(false);
        if (isCancel) {
          if (balance > 0) {
            message.append("Cancelled, returned amount: " + df.format(balance));
          }
          else {
            message.append("Cancelled");
          }
        } else {
          message.setText("");
        }
        balance=0.0d;
      }
      /**
       * Handle button clicks
       */
      public void actionPerformed(ActionEvent ae) {
        cancelBtn.setEnabled(true);
        String cmd=ae.getActionCommand();
        if (timePriceTab.containsKey(cmd)) { // a time button
          handleTimeSelection(cmd);
        } else if (priceTab.containsKey(cmd)) { // a payment button
          handlePayment(cmd);
        } else if (cmd.equals("Cancel")) {
          resetMachine(true);
        }
      }
    
      /**
       * Process a payment button click
       */
      private void handlePayment(String cmd) {
        double payment=((Double)priceTab.get(cmd)).doubleValue();
        balance += payment;
        message.append("paid: "+df.format(payment)+
                       ", total: "+df.format(balance));
        double rest=priceToPay-balance;
        if (rest < 0.01d) { // the double precision bugger....
          message.append("\r\n\r\nThank you, Here is your ticket...\r\n");
          if (rest < 0) {
            message.append("Refund: "+df.format(balance-priceToPay)+"\r\n");
          }
          enableButtons(leftSide,false);
          enableButtons(rightSide,true);
          balance=0.0d;
          cancelBtn.setEnabled(false);
        } else {
          message.append(", payment left: "+df.format(priceToPay-balance)+"\r\n");
        }
      }
      /**
       * Process a parking time button click
       */
      private void handleTimeSelection(String cmd) {
        enableButtons(leftSide,true); // lock-unlock appropriate buttons
        enableButtons(rightSide,false);
        priceToPay=((Double)timePriceTab.get(cmd)).doubleValue();
        message.setText("");
        message.append("Selected parking time: "+cmd+"\r\n");
        message.append("Price is: "+df.format(priceToPay)+"\r\n");
      }
      /**
       * main method for running applet standalone (as an application)
       * NOTE: is not invoked when applet runs in a browser.
       */
      public static void main(String[] args) {
        Frame f=new Frame("CarPark");
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        f.setLayout(new GridLayout());
        CarPark2 cp = new CarPark2();
        f.add(cp);
        f.setBounds(20,20,500,300);
        cp.init();
        f.setVisible(true);
      }
    
    }
    eschew obfuscation

  6. #6
    Join Date
    Aug 2005
    Posts
    11
    thank u for all your help, i will run the code and test it thanks once again

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Pls notice that the php (or whatever) manages to embed blanks in the code
    scroller window, in a rather random fashion, like:
    priceToPay=((Double)timePriceTab.get(cmd)).doubleV alue();
    eschew obfuscation

  8. #8
    Join Date
    Aug 2005
    Posts
    11
    just a qucik question, im trying to get this to run in edit plus and its not running, no buttons are working when clicked on

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    What is edit plus, it doesn't sound like its compliant with java standards;
    This works on the web, on my console, and in JBuilder.

    Note: the pay buttons dont work until you have selected parking time.

    Try: compile it and open a console window, go to the folder where the
    class file is and do:

    Code:
    java CarPark2
    Last edited by sjalle; 08-19-2005 at 01:39 PM.
    eschew obfuscation

  10. #10
    Join Date
    Aug 2005
    Posts
    11
    Edit plus is a basic java program that i use to view applets

  11. #11
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Ok, I can assure you that it doesn't do the job right. There is nothing "exotic" or
    out of the ordinary in the code I posted.

    Try the approach I posted above.

    Why can't everybody just use JBuilder ...
    eschew obfuscation

  12. #12
    Join Date
    Aug 2005
    Posts
    11
    is it possible if u can send me screen shots of the actual program in action plz, i cannot afford jbuilder i am a very poor student. thank you for all the help u have given me

  13. #13
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    eschew obfuscation

  14. #14
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here are screenshots

    These are web shots...
    Attached Images Attached Images
    eschew obfuscation

  15. #15
    Join Date
    Aug 2005
    Posts
    11
    thank you once again, u cannot begin too understand how much u have helped me out, i salute you.

    thank you

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. Replies: 7
    Last Post: 04-13-2005, 07:09 PM
  3. API For shutting, starting win NT,9x OSes
    By Saiful in forum VB Classic
    Replies: 6
    Last Post: 10-15-2000, 03:18 PM
  4. Replies: 0
    Last Post: 08-01-2000, 07:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center