• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taxi Company program  RSS feed

 
nadeem muhammad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i have an assignment that i have to submit till 1st july so kindly help me

theses are the requirment:


Taxi Company
Develop an application to manage a taxi company.
All the classes must belong to package taxi. Use the class MainClass as an example.
R1. Agenzie e Taxi
A taxi company is represented by the class TaxiCompany.
Taxis are represented by the class Taxi, that provides a constructor accepting the unique id of the taxi.
The method toString() from class Taxi returns the identifier of the taxi.
The method addTaxi (String id) from class TaxiCompany allows adding a new item to the taxi list of a company; if the identifier is already present, it raises an InvalidTaxiName exception.
When a new taxi is added to a company, it is inserted at the end of the available taxis queue.
The method getAvailable() returns the free taxis queue.
R2. Places and Passengers
The class Place represents a real address and provides a constructor accepting as arguments the actual address and the relative district (or quarter) name. (E.b.: "Corso Duca Abruzzi 24", "crocetta").
The method toString() returns the address of the place.
The class Passenger represents a customer of the taxi company, it provides a constructor accepting as argument the place where the customer is located currently, i.e. where he/she should be picked up by a taxi
The method getPlace() returns the place where the passenger is currently located.
R3. Taxi Management
Taxi management will be handled with a First-In-First-Out strategy by means of a queue containing the available taxis.
The method callTaxi(Passenger p) assigns the first taxi in the available queue to the passenger passed as argument and returns the taxi itself; if no taxi is available then the call is lost.
The method getLostTrips() returns the total number of lost calls.
The class Taxi provides the methods for initiating and terminating a taxi trip:

a taxi, once assigned to a passenger, can initiate its trip by means of method beginTrip(Place dest), that accepts the destination as argument, while the departure place is represented by the current passenger position.

the trip terminates with the method terminateTrip() that assigns to the passenger the destination place and puts the taxi at the end of the available taxis queue.

R4. Trips
The class Trip represents a trip completed by a taxi. The method toString() returns a string containing the departure and arrival addresses, separated by a comma (',').
The method getTrips(String id) of the class TaxiCompany returns a list containing all the trips completed by the taxi identified by the argument in chronological order; if the identifier is not present in the list of taxis working for the company then it raises an InvalidTaxiName exception.
R5. Statistics
Class TaxiCompany provides two methods used to retrieve statistics:

the method statsTaxi() returns the list of taxis (through the interface InfoI) sorted by decreasing number of completed trips (those for which the terminateTrip() method has been invoked); in case of tie use the alphabetic order of the id.

the method statsDistricts() returns the list of districts that have been destination of at least a trip (through the interface InfoI), sorted by decreasing number of trips having that district as the destination; in case of ties use the alphabetic order of the district.











i am stucked in the begintrip() and terminate () methods so kindly help me

thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

i am stucked in the begintrip() and terminate() methods

In which way are you stuck? What, exactly, does the code do now, and in which way, exactly, is that different from what you were expecting it to do? What prevents you from adding the missing pieces?
 
nadeem muhammad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the beginTrip(Place p )
must start the trip as well call this method and the argument that it take is the destination address and departure place is represented by the current passenger position so i tried
Passenger p = new Passenger(current.getPlace());

but when i run the test file it show me the null pointer error

and if write the above code then there is another error. the destination place is coming but there should be the current address
i dont know what to do
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Start by posting the exact error messages, and also tell us in which lines of code they occur. A NullPointerException (NPE) in particular occurs because some object reference is null, and your code is not checking for it - which object reference is that?

2) What is the "test file" you mention? I see no file use in any of the code. In fact, I see no input whatsoever being done - how is this code run? There should be a class with a "main" method somewhere that actually causes the above code to do something - post that too.

3) Line 26 of the Passenger class as well as line 21 of the Place class are incorrect. Before casting, you need to check whether the object about to be casted is of the appropriate object type; instead you're checking whether it is of the "Taxi" type - looks like a copy/paste mistake.

4) The while loop in TaxiCOmpany.callTaxis looks wrong - there's a return statement in the body of the loop, which ensures that the loop is terminated at the end of the first iteration; probably not what you want. It's not clear to me what the purpose of the loop is, so I can't speculate what should happen instead.

5) The Trip.toString method will always throw a NPE, because t is never assigned anything.
 
nadeem muhammad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 ) taxi.InvalidTaxiName
Assigned Taxi1
Taxi1: left from Via Roma 10, destination Corso Francia 105
Taxi1: arrived at Via Roma 10
Assigned Taxi2
Taxi2: left from Via Roma 10, destination Corso Duca Abruzzi 24
Taxi2: arrived at Via Roma 10
Call lost
Exception in thread "main" java.lang.NullPointerException
at Example.main(Example.java:68)
this is the error that i get if i make changes in the Taxi class

by changing the
public void beginTrip(Place dest) {
this.dest = dest;
}

public void terminateTrip(){

return;
}
2 ) We I have a test file that i can check with to solve the problem but i not a good programmer that is why i can not find the problem.
3)the mistake that you are refering to i think it is correct because i copied the code from one of the solution that been done by professor and it is correct.
4) About while you are correct i have to take out the return statement.
5) can you tell me why Trip.toString method will always throw NPE and how to solve it

Can you kindly read above the requirment that i have copied because those are the requirment that we have to fill to make the correction.

There is a file with main class that i didn't copied here but i will do it here


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In Java, exceptions are designed to help you find the issue. You just need to be able to read it and interpret it.

nadeem muhammad wrote:
Exception in thread "main" java.lang.NullPointerException
at Example.main(Example.java:68)
this is the error that i get if i make changes in the Taxi class


From the stack trace that you provided, you have an issue near line 68, of the Example.java file. From this information, you should be able to modify the program to isolate it a bit more. Show us the area around line 68, and a guess at what you think it is, and we can give you a hint in the right direction.

Henry
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3)the mistake that you are referring to i think it is correct because i copied the code from one of the solution that been done by professor and it is correct.

I assure you, it is absolutely, positively, not correct. Those codes will throw exceptions every time they are run. It's possible that they don't throw exceptions in this case because they are never run - but they're exceptions waiting to happen.

5) can you tell me why Trip.toString method will always throw NPE and how to solve it

Because "t" is initialized to "null" (which is the default value for object references), and no other value is ever assigned to it. (No other value can ever be assigned to it, because it is declared private, and no setter method for it exists.) And since toString tries to dereference "t", it will throw a NPE.
 
nadeem muhammad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void beginTrip(Place dest) {
this.dest = dest;
}

public void terminateTrip(){

return;

This.dest = dest; this line produce the NPE i dont know how to solve it even though if i can make it correct there is another problem that is when the begintrip method is called in the program it take read the destinantion address in the parameter and the current location is the passenger current location which i have to take so how to do it

and the second one is terminate which terminate the trip so when this method id called it should terminate the trip and show the current location of the passenger but my program show me the destination address where the passenger want to go but how can he go when it is taxi is terminated so it must show the current address
i am not been able to solve it
 
nadeem muhammad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt wrote much in the terminatetrip method because i dont know what to write
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This.dest = dest; this line produce the NPE

No, that line can't throw a NPE. But all these lines will:
t.beginTrip(...)

every time they're called if "t" is null. Your code checks whether t is null before each such call, but then the code proceeds anyway. You can't invoke methods on a null object reference (that will result in a NPE), so you need to think about structuring the code differently.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!