Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Having troublegetting buttons to workand time to display (http://www.go4expert.com/forums/troublegetting-buttons-workand-time-t10387/)

Ken 5May2008 02:27

Having troublegetting buttons to workand time to display
 
Here what i have to do first off

Code:

Octagon Boat Hire is a busy boat hire company on the shores of Lake Octagon.  The company hires out pedal boats by the hour, and rowing boats for a flat rate for up to four hours.  The hire charges are:

        pedal boats - 4 per hour
        rowing boats flat rate of 10 for up to 4 hours, with a late charge of 5 per hour after 4 hours

Each boat has a unique ID number.  The pedal boats come in different colours (white, red and green) while the rowing boats are named after local landmarks.  When a customer hires a boat, they tell the clerk what type of boat they would like (pedal boat or rowing boat).  The clerk tells them the ID number of an available boat meeting their requirements, its colour or name as appropriate, and notes the customer's name in the boat's record, along with the time.  Octagon Boat Hire is a laid-back company, so the time is recorded to the next nearest hour (in the 24-hour clock).  If a customer takes out a boat at 1:15 pm, the time 14 (representing 2 pm) is recorded.

When a customer brings their boat back, they tell the clerk the boat ID number.  He inputs this, plus the time of return to the nearest hour, and tells the customer the amount due.

The clerk often has to perform administrative tasks, such as adding new boats when they are purchased, listing all the boats, or all the boats of a given type, and listing all the boats currently on hire.

Task
Your task is to implement, in Java, an object-oriented system for Octagon Boat Hire. 

Your system should contain, at minimum, a Boat, PedalBoat, RowingBoat, and BoatHireCompany class and exploit the key object-oriented concepts encapsulation, inheritance and polymorphism.

You should initially develop and test each class using an appropriate text-based driver (main) class, which does not need to have user interaction (choice or input via Scanner).  In order to be able to fulfil the above scenario, the classes you develop should be able to:

Boat, PedalBoat,  RowingBoat classes:
o        construct a rowing boat
o        construct a pedal boat
o        mark a boat as hired, storing the customer name and time
o        return a boat's details as a String, including customer name and time of hire if it currently hired out
o        calculate the amount due on a hired boat, given the time it was brought back
o        make a boat that has been brought back available for rehire
BoatHireCompany class:
o        construct a company which holds a list of boats
o        add a new boat to the list of boats owned by the company
o        display the details of all boats owned by the company
o        display the details of all pedal boats
o        display the details of all rowing boats
o        find an available pedal boat
o        find an available rowing boat
o        display the details of all the boats currently hired out, including the name of the customer who has hired each boat, and the time it was hired
o        find a boat given its id number

I have done the code with is diplayed below
Boat.java
Code:

public abstract class Boat
{
 
    protected String name;
    protected int id;               
    protected String type;               
    protected boolean hire;
   
        public Boat(int boatID, String boatName, String boatType)
    {
    name = boatName; 
    id = boatID;
    type = boatType;
    hire = false;
    }
}

Pedal Boat.java
[code]
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public abstract class PedalBoat extends Boat
{
Calendar cal = new GregorianCalendar();

protected String colour;
protected double cost = 4.00;
protected int status;
protected String stat;
protected int timeHired;


public PedalBoat(int id, String boatName, String boatType, String c, int theStatus, int Time)
{
super(id, boatName, boatType);
colour = c;
status = theStatus;
timeHired = Time;

super.hire = false;

if(theStatus==0)
{
stat="Unavailable";
}
else
{
stat="Available";
}

}


public int getstat()
{
return status;
}

public String toString()
{
String s = "\nBoat ID:" + id + "\nAvaliablitiy: " + stat + "\nName:" + name + "\nTime" + timeHired + "\nRate Per hour" + cost;
return s;
}
}
[\code]

Rowingboat.java
[code]
public abstract class RowingBoat extends Boat
{
protected String colour;
protected double cost= 4.00;
protected int status;
protected String stat;
protected int timeHired;


public RowingBoat(int id, String boatName, String boatType, String c, int theStatus, int Time)
{
super(id, boatName, boatType);
colour = c;
status = theStatus;
timeHired = Time;
super.hire = false;

if(theStatus==0)
{
stat="Unavailable";
}
else
{
stat="Available";
}

}
public int getstat()
{
return status;
}

public String toString()
{
String s = "\nBoat ID:" + id + "\nAvaliablitiy: " + stat + "\nName: " + name + "\nTime " + timeHired + "\nRate Per hour " + cost;
return s;
}
}

[\code]

BoatHireCompany.java
Code:

import java.util.*;
import javax.swing.*;

public abstract class BoatHireCompany
{

        protected ArrayList <Boat> Boat;
        protected int numBoat;
        protected int maxBoat;
       
        protected ArrayList <PedalBoat> PedalBoat;
        protected int numPedalBoat;
        protected int maxPedalBoat;
       
        protected ArrayList <RowingBoat> RowingBoat;
        protected int numRowingBoat;
        protected int maxRowingBoat;
       
        private String BoatHireCompanyName = "Octgon Boat Company";
       
                public BoatHireCompany (String name, int num)
                {
               
                        BoatHireCompanyName = name;
                       
                        numBoat = 0;
                        maxBoat = num;
                        numPedalBoat = 0;
                        maxPedalBoat = num;
                        numRowingBoat = 0;
                        maxRowingBoat = num;
                       
                        Boat = new ArrayList <Boat> (maxBoat);
                        PedalBoat = new ArrayList <PedalBoat> (maxPedalBoat);
                        RowingBoat = new ArrayList <RowingBoat> (maxRowingBoat);
                  }
                       
                public boolean addBoat(Boat newBoat)
                        {
                        if (numBoat == maxBoat)
                                  {
                                  return false;
                                  }
                                 
                        else   
                                  {
                                  Boat.add(newBoat);
                                  numBoat++;
                                  return true;
                                  }
                        }
                     
                public boolean addPedalBoat (PedalBoat newPedalBoat)
                    {
                        if (numPedalBoat == maxPedalBoat)
                                  {
                                  return false;
                                  }
                               
                        else     
                                  {
                                  PedalBoat.add(newPedalBoat);
                                  numPedalBoat++;
                                  return true;
                                  }
                        }
                       
                public boolean addRowingBoat (RowingBoat newRowingBoat)
                      {
                        if (numRowingBoat == maxRowingBoat)
                                  {
                                  return false;
                                  }
                       
                        else
                                  {
                                  RowingBoat.add(newRowingBoat);
                                  numRowingBoat++;
                                  return true;
                                  }
                        }
                     
                public void displayBoatDetails(JTextArea displayArea) 
                      {
               
                        System.out.println("\nCompany Name : " + BoatHireCompanyName);
               
                          for (int i = 0; i <numBoat; i++)
                                  {
                                  System.out.println("\nCompany NameDetails : " + Boat.get(i).toString());
                                  }
                        }
                               
                public void displayPedalBoatDetails(JTextArea displayArea)
                      {
                        for (int i = 0; i <numPedalBoat; i++)
                                {
                                  System.out.println("\nPedal Boat Details : " + PedalBoat.get(i).toString());
                                  }
                        }     
                     
                public void displayRowingBoatDetails(JTextArea displayArea) {
                        for (int i = 0; i <numRowingBoat; i++)
                                  {
                                  System.out.println("\nRowing Boat Details : " + RowingBoat.get(i).toString());
                                  }
                        }             
                     
                public Boat findBoat(String boatID)
                        {
                        for (int i = 0; i < numBoat; i++)
                                          {
                                          Boat BID = Boat.get(i);
                                          return BID;
                                          }
                                  return null;
                        }
               
    void addPedalBoat(JTextArea displayArea) {}
    void addRowingBoat(JTextArea displayArea) {}
    void findBoat(JTextArea displayArea) {}
}

Hire.java
Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Hire extends JDialog implements ActionListener
{

        protected JButton ok;
        protected JButton cancel;
        protected JTextField bid;
        protected JTextField name;
        protected JTextField time;
        protected BoatHireCompany Boat;
       
        public Hire(String sTitle, JFrame owner) {
                super (owner, sTitle, true);
                setLayout(new FlowLayout());
                setSize(350,200);
                add (new JLabel("Enter Boat ID:"));
                bid = new JTextField(20);
                add (bid);
                add (new JLabel("Enter Customer Name:"));
                name = new JTextField(20);
                add (name);
                add (new JLabel("Enter Current Time:"));
                time = new JTextField(20);
                add (time);
                ok = new JButton("Hire Boat!");
                ok.addActionListener(this);
                add(ok);
                cancel = new JButton("Cancel");
                cancel.addActionListener(this);       
                add(cancel);
        }

        public BoatHireCompany getBoat()
        {
                return Boat;
        }
        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == ok) {
                    Boat =  new BoatHireCompany("Carl",10) {};
                }
                else if (e.getSource() == cancel) {
                    // data entry cancelled
                    Boat = null;
                }
                dispose();
        }
}

Main.java
Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener
{
  protected JButton displayBoats;
  protected JButton displayPBoats;
  protected JButton displayRBoats;
  protected JButton addpBoats;
  protected JButton addrBoats;
  protected JButton hireboat;
  protected JButton boatbk;
  protected JButton hire;
  protected JTextArea displayArea;
  protected BoatHireCompany octagon;
  protected BoatHireCompany pBoat;
  protected BoatHireCompany rBoat;
 
public Main()
  {       
  pBoat=new BoatHireCompany("Octagon Boat Hire Company",5) {};
  rBoat=new BoatHireCompany("Octagon Boat Hire Company",5) {};
  octagon= new BoatHireCompany("Octagon Boat Hire Company",5) {}; 
           
  PedalBoat B1= new PedalBoat(123,"Forbidden One","Pedal Boat","White",1,0){};       
  PedalBoat B2= new PedalBoat(623,"Dragon","Pedal Boat","Green",0,14){};
  RowingBoat B3= new RowingBoat (764,"Death","Rowing Boat","Yellow",1,0){};
  RowingBoat B4= new RowingBoat(344,"Sword","Rowing Boat","Aqua blue",0,15){};

  octagon.addBoat( B1);
  octagon.addBoat( B2);   
  octagon.addBoat( B3);
  octagon.addBoat( B4);
 
  pBoat.addBoat( B1);
  pBoat.addBoat( B2);

  rBoat.addBoat( B3);
  rBoat.addBoat( B4);

  // set up frame
  setTitle("Octagon Boat Hire Company");
  setBounds(0, 0, 1200, 250);
  getContentPane().setLayout(new BorderLayout());
       
  // add buttons and actionListeners
  displayBoats = new JButton("Display all boats");
  displayBoats.addActionListener(this);

  displayPBoats = new JButton("Display all pedal boats");
  displayPBoats.addActionListener(this);

  displayRBoats = new JButton("Display all rowing boats");
  displayRBoats.addActionListener(this);
       
  addpBoats = new JButton("Add new pedal boat");
  addpBoats.addActionListener(this);
       
  addrBoats = new JButton("Add new rowing boat");
  addrBoats.addActionListener(this);
       
  hireboat = new JButton("List of Hire Boat");
  hireboat.addActionListener(this);

  boatbk = new JButton("Return boat");
  boatbk.addActionListener(this);

  hire = new JButton("Hire boat");
 
  hire.addActionListener(this);

  JPanel p = new JPanel();
  p.add(displayBoats) ;
  p.add(displayPBoats);
  p.add(displayRBoats);
  p.add(addpBoats);
  p.add(addrBoats);
  p.add(hireboat);
  p.add(boatbk);
       
  getContentPane().add("South", p);
  JPanel northPanel = new JPanel();
  displayArea = new JTextArea("This is the display area    " +"\nDetails of boats will be displayed here      " +"\nPlease make your choice\n\n\n\n\n\n"    );
       
  JScrollPane scrollPane;
  scrollPane = new JScrollPane(displayArea);
  northPanel.add(scrollPane);
  getContentPane().add("Center", northPanel);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  String name;
  name = JOptionPane.showInputDialog("What is your name? ");                 
  JOptionPane.showMessageDialog(null, "Welcome: " + name);
 }

public void actionPerformed(ActionEvent e) 
 {
  if (e.getSource() == displayBoats)
  {
    octagon.displayBoatDetails(displayArea);
  }

  else if (e.getSource() == displayPBoats)       
  {
    pBoat.displayPedalBoatDetails(displayArea);
  }

  else if (e.getSource() == displayRBoats)       
  {
    rBoat.displayRowingBoatDetails(displayArea);
  }

  else if (e.getSource() == addpBoats)       
  {
    octagon.addPedalBoat(displayArea);
  }

  else if (e.getSource() == addrBoats)       
  {
    octagon.addRowingBoat(displayArea);
  }

 else if (e.getSource()==hire)
  {
  displayArea.setText("boat hired");
  }
 }

public static void main(String []args)
 {
               
Main window = new Main();
window.setVisible(true);

 }

}

Problem is that the button wont work and i dont know how to get the time in 12 hours i.e Timehired 1.00pm? If an y one can help me get my button and the time running i would be so grateful thank you so much. If someone can tell me how to allow the user to input time is so so frustating see I am not that good at gui's thanks again.

Ken 5May2008 05:27

Re: Having troublegetting buttons to workand time to display
 
Here the Boat Comany class if anyone can help me with this problem i would be everso grateful and hopefully return the favour

Code:

import java.util.*;
import javax.swing.*;

public abstract class BoatHireCompany
{

        protected ArrayList <Boat> Boat;
        protected int numBoat;
        protected int maxBoat;
       
        protected ArrayList <PedalBoat> PedalBoat;
        protected int numPedalBoat;
        protected int maxPedalBoat;
       
        protected ArrayList <RowingBoat> RowingBoat;
        protected int numRowingBoat;
        protected int maxRowingBoat;
       
        private String BoatHireCompanyName = "Octgon Boat Company";
       
                public BoatHireCompany (String name, int num)
                {
               
                        BoatHireCompanyName = name;
                       
                        numBoat = 0;
                        maxBoat = num;
                        numPedalBoat = 0;
                        maxPedalBoat = num;
                        numRowingBoat = 0;
                        maxRowingBoat = num;
                       
                        Boat = new ArrayList <Boat> (maxBoat);
                        PedalBoat = new ArrayList <PedalBoat> (maxPedalBoat);
                        RowingBoat = new ArrayList <RowingBoat> (maxRowingBoat);
                  }
                       
                public boolean addBoat(Boat newBoat)
                        {
                        if (numBoat == maxBoat)
                                  {
                                  return false;
                                  }
                                 
                        else   
                                  {
                                  Boat.add(newBoat);
                                  numBoat++;
                                  return true;
                                  }
                        }
                     
                public boolean addPedalBoat (PedalBoat newPedalBoat)
                    {
                        if (numPedalBoat == maxPedalBoat)
                                  {
                                  return false;
                                  }
                               
                        else     
                                  {
                                  PedalBoat.add(newPedalBoat);
                                  numPedalBoat++;
                                  return true;
                                  }
                        }
                       
                public boolean addRowingBoat (RowingBoat newRowingBoat)
                      {
                        if (numRowingBoat == maxRowingBoat)
                                  {
                                  return false;
                                  }
                       
                        else
                                  {
                                  RowingBoat.add(newRowingBoat);
                                  numRowingBoat++;
                                  return true;
                                  }
                        }
                     
                public void displayBoatDetails(JTextArea displayArea) 
                      {
               
                        System.out.println("\nCompany Name : " + BoatHireCompanyName);
               
                          for (int i = 0; i <numBoat; i++)
                                  {
                                  System.out.println("\nCompany NameDetails : " + Boat.get(i).toString());
                                  }
                        }
                               
                public void displayPedalBoatDetails(JTextArea displayArea)
                      {
                        for (int i = 0; i <numPedalBoat; i++)
                                {
                                  System.out.println("\nPedal Boat Details : " + PedalBoat.get(i).toString());
                                  }
                        }     
                     
                public void displayRowingBoatDetails(JTextArea displayArea) {
                        for (int i = 0; i <numRowingBoat; i++)
                                  {
                                  System.out.println("\nRowing Boat Details : " + RowingBoat.get(i).toString());
                                  }
                        }             
                     
                public Boat findBoat(String boatID)
                        {
                        for (int i = 0; i < numBoat; i++)
                                          {
                                          Boat BID = Boat.get(i);
                                          return BID;
                                          }
                                  return null;
                        }
               
    void addPedalBoat(JTextArea displayArea) {}
    void addRowingBoat(JTextArea displayArea) {}
    void findBoat(JTextArea displayArea) {}
}



All times are GMT +5.5. The time now is 14:27.