Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Desperate Beginner would Love ur HELP...
|
Posted: Aug 24, 2002 3:39 PM
|
|
The code for this can be downloaded at: http://www.quantumhyperspace.com/SourceBank/Ferry.java ***************************************
/* Ferry.java * @author: Charles Bell * * Design & implement a class ferry which models a cross-channel ferry. * The class should model the following state: * * currentDrive - which can be neutral, forward or reverse. * * bowDoors - which can be open or close. * * Methods are: * * closeDoors - this closes the bow doors if they are open, otherwise * it displays and error message. * * openDoors - this opens the bow doors if they are closed provided the * current drive is in neutralotherwise it displays an error message. * * engageDrive - this requires an integer argument (-1 reverse and 1 forward); * if the bow doors are clsed it will engage the selected drive, * otherwise an error message is displayed; * * disengageDrive - if neither reverse or forward is engage it will be * disengaged, otherwise an error message will be displayed. */
import java.awt.*; import java.awt.event.*; import javax.swing.*;
/** Models a cross-channel ferry. */ public class Ferry extends JFrame implements ActionListener{ final int NEUTRAL = 0; final int FORWARD = 1; final int REVERSE = -1; private int currentDrive = NEUTRAL; final int OPEN = 0; final int CLOSED = 1; private int bowDoors = CLOSED;
private JLabel driveStatusLabel; private JLabel bowdoorStatusLabel;
public static void main(String[] args){ Ferry ferry = new Ferry(); ferry.init(); } public void init(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel ferryStatusPanel = new JPanel(); driveStatusLabel = new JLabel("Neutral"); bowdoorStatusLabel = new JLabel("Closed"); ferryStatusPanel.add(new JLabel("Drive Status: ")); ferryStatusPanel.add(driveStatusLabel); ferryStatusPanel.add(new JLabel("Bow Door Status: ")); ferryStatusPanel.add(bowdoorStatusLabel); JPanel bowDoorControlPanel = new JPanel(); JButton openButton = new JButton("Open Bow Doors"); JButton closeButton = new JButton("Close Bow Doors"); JButton exitButton = new JButton("Exit"); bowDoorControlPanel.add(openButton); bowDoorControlPanel.add(closeButton); bowDoorControlPanel.add(exitButton); openButton.addActionListener(this); closeButton.addActionListener(this); exitButton.addActionListener(this); JPanel driveControlPanel = new JPanel(); JButton reverseButton = new JButton("Reverse"); JButton neutralButton = new JButton("Neutral"); JButton forwardButton = new JButton("Forward"); driveControlPanel.add(reverseButton); driveControlPanel.add(neutralButton); driveControlPanel.add(forwardButton); reverseButton.addActionListener(this); neutralButton.addActionListener(this); forwardButton.addActionListener(this); getContentPane().add(ferryStatusPanel, BorderLayout.NORTH); getContentPane().add(bowDoorControlPanel, BorderLayout.CENTER); getContentPane().add(driveControlPanel, BorderLayout.SOUTH); pack(); setLocationRelativeTo(null); show(); } /** Closes the bow doors if they are open, otherwise it displays an error message. */ public void closeDoors(){ if (bowDoors == OPEN){ bowDoors = CLOSED; }else{ JOptionPane.showMessageDialog(this, "The Bow Doors are already closed.", "Error", JOptionPane.ERROR_MESSAGE); } }
/** Opens the bow doors if they are closed provided the current drive is in neutral. * Otherwise it displays an error message. */ public void openDoors(){ if ((bowDoors == CLOSED) && (currentDrive == NEUTRAL)){ bowDoors = OPEN; }else{ if (bowDoors == OPEN) { JOptionPane.showMessageDialog(this, "The Bow Doors are already open.", "Error", JOptionPane.ERROR_MESSAGE); } else if (currentDrive != NEUTRAL) { JOptionPane.showMessageDialog(this, "The Cuurent Drive must be in neutral to open the doors.", "Error", JOptionPane.ERROR_MESSAGE); } } }
/** Requires an integer argument (-1 reverse and 1 forward); * If the bow doors are closed it will engage the selected drive, * otherwise an error message is displayed; */ public void engageDrive(int request){ if (bowDoors == CLOSED){ if (request == -1) currentDrive = REVERSE; if (request == 1) currentDrive = FORWARD; }else{ JOptionPane.showMessageDialog(this, "The Bow Doors must be closed to engage the drive.", "Error", JOptionPane.ERROR_MESSAGE); } } /** If neither reverse or forward is engage it will be disengaged, otherwise an error * message will be displayed. */ public void disengageDrive(){ if (currentDrive == REVERSE) { currentDrive =NEUTRAL; }else if (currentDrive == FORWARD) { currentDrive =NEUTRAL; }else{ JOptionPane.showMessageDialog(this, "The Drive was already disengaged.", "Error", JOptionPane.ERROR_MESSAGE); } } public void updateStatus(){ if (currentDrive == REVERSE) driveStatusLabel.setText("Reverse"); if (currentDrive == FORWARD) driveStatusLabel.setText("Forward"); if (currentDrive == NEUTRAL) driveStatusLabel.setText("Neutral"); if (bowDoors == OPEN) bowdoorStatusLabel.setText("Open"); if (bowDoors == CLOSED) bowdoorStatusLabel.setText("Closed"); } public void actionPerformed(ActionEvent actionEvent){ String actionCommand = actionEvent.getActionCommand(); if (actionCommand.compareTo("Open Bow Doors") == 0){ openDoors(); }else if (actionCommand.compareTo("Close Bow Doors") == 0){ closeDoors(); }else if (actionCommand.compareTo("Forward") == 0){ engageDrive(FORWARD); }else if (actionCommand.compareTo("Reverse") == 0){ engageDrive(REVERSE); }else if (actionCommand.compareTo("Neutral") == 0){ disengageDrive(); }else if (actionCommand.compareTo("Exit") == 0){ System.exit(0); } updateStatus(); } }
|
|