Click to See Complete Forum and Search --> : Java Program


fortune2k
April 18th, 2008, 11:16 AM
hi, i am undertaking a project for my school and well i am not very knowledgable in the world of java andwell this is what i have to do :


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



this is what i have done so far :

BoatHireCompany Class:

package assignment;
import java.util.Scanner;

public class BoatHireCompany {

private RowingBoat [] theRowingBoats;
private PedalBoat [] thePedalBoats;
private int numRowingBoats;
private int numPedalBoats;
private int maxRowingBoats;
private int maxPedalBoats;
private String BoatHireCompanyName;
private RowingBoat [] notime;


public BoatHireCompany (String name, int num)
{
BoatHireCompanyName = name;
maxRowingBoats = num;
numRowingBoats = 0;
maxPedalBoats = num;
numPedalBoats = 0;
theRowingBoats = new RowingBoat[maxRowingBoats];
thePedalBoats = new PedalBoat [maxPedalBoats];
notime = new RowingBoat[maxRowingBoats];
}

public boolean addRowingBoat( RowingBoat newRowingBoat )
{

// add another Boat to the BoatHireCompany if the BoatHireCompany is not full
if ( numRowingBoats == maxRowingBoats )
{
// the BoatHireCompany is full Boat cannot be added
return false;
}
else
{

int stat;
stat=RowingBoat.status;
if(stat==0)
{
theRowingBoats[numRowingBoats] = newRowingBoat;
numRowingBoats++;

}
else
{
notime[numRowingBoats] = newRowingBoat;

}
return true;


}
}
public boolean addPedalBoat( PedalBoat newPedalBoat )
{
// add another Boat to the BoatHireCompany if the BoatHireCompany is not full
if ( numPedalBoats == maxPedalBoats )
{
// the BoatHireCompany is full Boat cannot be added
return false;
}
else
{
// add Boat to the BoatHireCompany
thePedalBoats[numPedalBoats] = newPedalBoat;
numPedalBoats++;
return true;
}
}


public void outputBoatHireCompanyDetails()
{
System.out.println("Company: " + BoatHireCompanyName );
System.out.println("");
//output each Boat in turn

System.out.println("The Rowing Boats:");
for( int i=0; i < numRowingBoats; ++i)
{
System.out.println(theRowingBoats[i].toString());

}
for( int i=0; i < numRowingBoats; ++i)
{
System.out.println(notime[i].toString());

}





System.out.println("\n");
System.out.println("The Pedal Boats:");
for( int i=0; i < numPedalBoats; ++i)
{
System.out.println(thePedalBoats[i].toString());
}

Scanner kybd = new Scanner(System.in);

System.out.println("MENU");
System.out.println("1. Hire a boat");
System.out.println("2. Return a boat");
System.out.println("Please enter your choice:");
int choice = kybd.nextInt();
switch (choice) {
case 1:


}
}
}


Boat Class

package assignment;

public class Boat {
public int ID;


public Boat (int a)
{
ID = a;

}

public String toString() {
String s = "ID:" + ID ;
return s;
}
}





Rowing Boat Class:

package assignment;

public class RowingBoat extends Boat
{


public String Name;
public static int status;
public String stat;

public RowingBoat(int a, String Name, int theStatus)
{
super(a);



this.Name=Name;
status=theStatus;
ID=a;

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

}

}
public int getstat() {
return status;
}

public String toString() {
String s = "ID:" + ID + " Avaliablitiy: " + stat + " Name:" + Name ;
return s;
}
}



Testing Class

package assignment;


public class Test {


// Driver class to test the BoatHireCompany class
public static void main(String[] args) {


BoatHireCompany BoatHire = new BoatHireCompany("BoatHire",15);
BoatHire.addRowingBoat(new RowingBoat (1,"Tedship",1));
BoatHire.addRowingBoat(new RowingBoat (2,"Daveship",0));

BoatHire.outputBoatHireCompanyDetails();
}
}





Well my problem is that i cannont change the boats from availabe to unavaile and vise-versa..... soo for example you have a boat which is availble which a integer is set to 0 a person comes alone hires out a boat then i need to change the nterger to 1 to show that it is not available and then link that to the person which is hiring out the boat#


Thankyou

keang
April 18th, 2008, 12:00 PM
soo for example you have a boat which is availble which a integer is set to 0 a person comes alone hires out a boat then i need to change the nterger to 1 to show that it is not availableIf you have to represent a 2 state situation eg On - Off or Available - Not Available etc it's normal to use a boolean which can be true or false rather than an int. So in this case have a boolean variable called something like available or isAvailable and toggle it between true and false.

To change the value of a variable you would normally use a setXXX() method and to query it's value you'd use a getXXX() method (although with boolean values it's common to use isXXX() instead). So you need to add the getter and setter methods for the 'available' variable and call them from your boat company class.

With regard to your class heirarchy I would have thought that the 'is available' code would have been part of the abstract boat class afterall all of the types of boats your boat company has will require this code.

fortune2k
April 18th, 2008, 02:18 PM
Ive changed the availabilty bit and put it into boat so it covers both the 2 boat classes which extends boats...........

i have this in the test class


BoatHire.addRowingBoat(new RowingBoat (1001,"Tedship",1));
BoatHire.addRowingBoat(new RowingBoat (1002,"Daveship",0));

the 1 and 0 in this constructor is determing if the boat is availible or unavailable and i have a string which is set to this value which out puts 1 0 = "unavailible", 1 = "unavailable" this data is converted to a 1d string array and then printed out using


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




public String toString() {
String s = "ID:" + ID + " Avaliablitiy: " + stat + " Name:" + Name ;
return s;



the program then prints this info out by using


for( int i=0; i < numRowingBoats; ++i)
{
System.out.println(theRowingBoats[i].toString());

}


output is this :

run:
Company: BoatHire

The Rowing Boats:
ID:1002 Avaliablitiy: Unavailable Name:Daveship
ID:1001 Avaliablitiy: Available Name:Tedship


this is now in a 1d array what im trying to do is have like a boathire mehtod in BoatHireCompany class where the user will input a few person details and the boat id this will then look up the boat by id then change the availability of the boat to unavailable because the status int is changed from 1 to 0....

when the boat is hired the person details will be linkd to it the data will be things such as customer name and time.... i just dont know how to edit this array ect to implement this can you help me .

i just dont know how to perfom a lookup for the id ect ect because each cell of the arry hold data like this
"ID:1001 Avaliablitiy: Available Name:Tedship"

dlorde
April 19th, 2008, 08:16 AM
If you want to keep track of which boats are hired, and by whom, why not keep a list of current hirings? This means adding an entry, with the boat or boat number and person details, every time a boat is hired. You'll probably want to be able to retrieve an entry by boat number (i.e. who has hired boat n), so using a keyed collection would be good. You could use a HashMap (http://java.sun.com/javase/6/docs/api/java/util/HashMap.html) to store the person details with the boat number as the key.

I'd probably consider having two lists - a list of boats available for hire, and a list of boats currently on hire (this would be a list mapping boat to person). When a boat was hired, I'd remove it from the available list, and add it to the on hire list. When a boat was returned, I'd remove it from the on hire list and add it back to the available list. This way I could always see how many boats were on hire, and which ones, and how many boats were available, and which ones. [You might still want to keep a separate list of all the boats owned by the boat-hirer, but it wouldn't be essential for the hiring process].

Imagination is more important than knowledge...
A. Einstein