import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CarSale extends Applet implements ActionListener {
private TextArea txaCars, txaSelected;
private TextField txfSelItem;
private Label lblCarNo;
private Label lblTitle;
private TextField txffirstname;
private Label lblfirstname;
private TextField txflastname;
private Label lbllastname;
private TextField txfaddress;
private Label lbladdress;
private TextField txfpostcode;
private Label lblpostcode;
private Label lblemail;
private TextField txfemail;
private Label lblphone;
private TextField txfphone;
private Label lblTotal;
private Color colTotal = Color.LIGHT_GRAY;
private float total;
private Button btnGetItem, btnClearItems, btnClearInfo, btnClearAll, btnSubmit;
private final int MAXCARS = 100; // maximum size of arrays
private Car[] cars; // cars on offer
private Car[] pickedCars; // cars selected
private int nbrOfCars = 0; // number of cars on offer
private int nbrCarsPicked = 0; // number of cars picked so far
private int nbrToPick = 0; // number of cars chosen
public void init(){
setSize(600,510);
setLayout(null);
setBackground(Color.GREEN);
createGUI();
cars = new Car[MAXCARS];
pickedCars = new Car[MAXCARS];
makeCar();
displayCars();
}
private void displayCars() {
txaCars.setText("");
for(int i = 0; i < nbrOfCars; i++){
txaCars.append("" + i+ ": ");
txaCars.append(cars[i].toString() + "\n");
}
}
private void displaySelectedCars() {
txaSelected.setText("");
for(int i = 0; i < nbrCarsPicked; i++){
txaSelected.append(pickedCars[i] + "\n");
}
}
private void createGUI() {
lblTitle=new Label("Auto Trader Car Sales");
lblTitle.setBounds(10,10,585,20);
lblTitle.setBackground(Color.yellow);
add(lblTitle);
lblfirstname = new Label("First Name");
lblfirstname.setBounds(10,40,90,20);;
add(lblfirstname);
txffirstname= new TextField("");
txffirstname.setBounds(110,80,150,20);
add(txffirstname);
lbllastname = new Label("Last Name");
lbllastname.setBounds(10,60,100,20);
add(lbllastname);
txflastname= new TextField ("");
txflastname.setBounds(110,100,150,20);
add(txflastname);
lbladdress = new Label ("Address");
lbladdress.setBounds(10,80,100,20);
add (lbladdress);
txfaddress=new TextField("");
txfaddress.setBounds(110,120,150,20);
add(txfaddress);
lblpostcode =new Label("Post Code");
lblpostcode.setBounds(10,100,100,20);
add(lblpostcode);
txfpostcode=new TextField("");
txfpostcode.setBounds(110,60,150,20);
add(txfpostcode);
lblemail=new Label("Email");
lblemail.setBounds(10,120,100,20);
add(lblemail);
txfemail=new TextField("");
txfemail.setBounds(110,40,150,20);
add(txfemail);
lblphone=new Label("Phone");
lblphone.setBounds(10,140,100,20);
add(lblphone);
txfphone=new TextField("");
txfphone.setBounds(110,140,150,20);
add(txfphone);
btnClearInfo = new Button("Clear Information");
btnClearInfo.setBounds(130,165,110,24);
btnClearInfo.addActionListener(this);
add(btnClearInfo);
lblTitle=new Label("Cars For Sale");
lblTitle.setBounds(10,200,585,20);
lblTitle.setBackground(Color.yellow);
add(lblTitle);
lblTitle=new Label("Shopping Basket");
lblTitle.setBounds(10,345,585,20);
lblTitle.setBackground(Color.yellow);
add(lblTitle);
txaCars = new TextArea("", 10,10,TextArea.SCROLLBARS_BOTH);
txaCars.setBounds(10,225,585,85);
add(txaCars);
txaSelected = new TextArea("", 10,10,TextArea.SCROLLBARS_BOTH);
txaSelected.setBounds(10,370,585,85);
add(txaSelected);
lblCarNo = new Label("Car Number ?");
lblCarNo.setBounds(56,315,80,30);
add(lblCarNo);
txfSelItem = new TextField("");
txfSelItem.setBounds(140,320,40,20);
txfSelItem.addActionListener(this);
add(txfSelItem);
btnGetItem = new Button("Get Car");
btnGetItem.setBounds(200,317,110,24);
btnGetItem.addActionListener(this);
add(btnGetItem);
btnClearItems = new Button("Clear Basket");
btnClearItems.setBounds(10,460,110,24);
btnClearItems.addActionListener(this);
add(btnClearItems);
btnClearAll = new Button("Clear All");
btnClearAll.setBounds(140,460,110,24);
btnClearAll.addActionListener(this);
add(btnClearAll);
btnSubmit = new Button("Submit Order");
btnSubmit.setBounds(480,480,110,24);
btnSubmit.addActionListener(this);
add(btnSubmit);
lblTotal = new Label("£0.00");
lblTotal.setBounds(460,457,130,20);
lblTotal.setBackground(colTotal);
add(this.lblTotal);
}
public void actionPerformed(ActionEvent e) {
// Get item
if(e.getSource() == btnGetItem || e.getSource() == txfSelItem){
try {
nbrToPick = Integer.parseInt(txfSelItem.getText());
}
catch(Exception ex){
txfSelItem.setText("");
return;
}
// See if item exists foe item no entered and there is
// room in picked array
if(nbrToPick >= 0 && nbrToPick < nbrOfCars
&& nbrCarsPicked < MAXCARS){
pickedCars[nbrCarsPicked] = cars[nbrToPick];
nbrCarsPicked++;
displaySelectedCars();
total = total + cars [nbrToPick].getValue();
displayTotal();
}
txfSelItem.setText("");
}
if(e.getSource() == btnClearItems){
txfSelItem.setText("");
txaSelected.setText("");
for(int i = 0; i < nbrCarsPicked ; i++)
pickedCars[i] = null;
nbrCarsPicked = 0;
System.out.println("EMPTY SHOPPING BASKET");
}
if(e.getSource() == btnClearInfo){
txffirstname.setText("");
txflastname.setText("");
txfaddress.setText("");
txfpostcode.setText("");
txfemail.setText("");
txfphone.setText("");
System.out.println("CLEAR CUSTOMER INFORMATION");
}
if(e.getSource() == btnClearAll){
txfSelItem.setText("");
txaSelected.setText("");
txffirstname.setText("");
txflastname.setText("");
txfaddress.setText("");
txfpostcode.setText("");
txfemail.setText("");
txfphone.setText("");
System.out.println("RESET ALL");
}
if(e.getSource() == btnSubmit){
txfSelItem.setText("");
txaSelected.setText("");
txffirstname.setText("");
txflastname.setText("");
txfaddress.setText("");
txfpostcode.setText("");
txfemail.setText("");
txfphone.setText("");
System.out.println("ORDER HAS BEEN SUBMITTED + RESET");
}
}
private void displayTotal() {
this.lblTotal.setText("£" + this.total);
}
private void makeCar() {
cars[nbrOfCars] = new Car("Ferrari F430", 120000.00f);
nbrOfCars++;
cars[nbrOfCars] = new Car("BMW M3", 15000.00f);
nbrOfCars++;
cars[nbrOfCars] = new Car("Benz Classic", 250000.00f);
nbrOfCars++;
cars[nbrOfCars] = new Car("Skyline GT", 30000.00f);
nbrOfCars++;
}
}