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.
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);
}
}
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
Bookmarks