1 Replies - 12000 Views - Last Post: 22 January 2010 - 10:56 PM Rate Topic: -----

#1 moman1981  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 17-December 09

ticket simulator

Posted 22 January 2010 - 10:34 PM

Ticket Simulator

For this assignment you will design a set of classes that work together to simulate a police officer issuing a parking ticket. You should design the following classes:

The ParkedCar Class: This class should simulate a parked car. The class's responsibilities are as follows:

- To know the car's make, model, color, license number, and the number of minutes that the car has been parked.

The ParkingMeter Class: This class should simulate a parking meter. The class's only responsibility is as follows:

- To know the number of minutes of parking time that has been purchased.

The ParkingTicket Class: This class should simulate a parking ticket. The class's responsibilities are as follows:

- To report the make, model, color, and license number of the illegally parked car

- To report the amount of the fine, which is $25 for the first hour or part of an hour that the car is illegally parked, plus $10 for every additional hour or part hour that the car is illegally parked, plus $l0 for every additional hour or part of an hour that the car is illegally parked

- To report the name and badge number of the police officer issuing the ticket

The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. The class's responsibilities are as follows:

- To know the police officer's name and badge number

- To examine a ParkedCar object and a ParkingMeter object, and determine whether the car's time has expired

- To issue a parking ticket (generate a ParkingTicket object) if the car's time has expired

Write a program that demonstrates how these classes collaborate.




Here is what I have so far. Please help. I am not getting an output. I think I might have a NULL somewhere that I cannot find. Any help would be greatly appreciated,

DEMO


public class Demo
{
public static void main (String[ ] args)
{
// Create a ParkedCar object, passing arguments to the constructor.
ParkedCar myParkedCar = new ParkedCar("Chevrolet", "Lumina APV", "White", "556 TNT", 48);

// Create a ParkingMeter object, passing arguments to the constructor.
ParkingMeter myParkingMeter = new ParkingMeter(48);

// Create a ParkedCar object.
//ParkedCar secondParkedCar = new ParkedCar("Chevrolet", "Lumina APV", "White", "556 TNT", 48);

// Create a ParkingMeter object.
//ParkingMeter secondParkingMeter = new ParkingMeter(60);

// Create a PoliceOfficer object.
PoliceOfficer myPoliceOfficer = new PoliceOfficer("John Doe", 1337, myParkedCar, myParkingMeter);

// Create a PoliceOfficer object.
//PoliceOfficer secondPoliceOfficer = new PoliceOfficer("John Doe", 1337, secondParkedCar, secondParkingMeter);

//
myPoliceOfficer.inspectParkedCar();

ParkingTicket myParkingTicket = new ParkingTicket(myPoliceOfficer.fine, myParkedCar, myParkingMeter);
myParkingTicket.toString();

myParkedCar.toString();
//
//secondPoliceOfficer.checkMeter(secondParkedCar, secondParkingMeter);
}//end main
}//end class




parked car class

public class ParkedCar
{
private String make; // Car's make
private String model; // Car's model
private String color; // Car's color
private String licenseNumber; // Car's alphanumeric license number
public int minutes; // Minutes parked

/**
This constructor initializes the make, model, color, licenseNumber, and minutes fields.
@param pcmake The parked car's make.
@param pcmodel The parked car's model.
@param pccolor The parked car's color.
@param pclnumber The parked car's license plate number.
@param pcminutes The number of minutes that the car has been parked.
*/

public ParkedCar(String pcmake, String pcmodel, String pccolor, String pclnumber, int pcminutes)
{
make = pcmake;
model = pcmodel;
color = pccolor;
licenseNumber = pclnumber;
minutes = pcminutes;
}

/** The set method sets a value for each field.
@param pcmake The parked car's make.
@param pcmodel The parked car's model.
@param pccolor The parked car's color.
@param pclnumber The parked car's license plate number.
@param pcminutes The number of minutes that the car has been parked.
*/

public void set(String pcmake, String pcmodel, String pccolor, String pclnumber, int pcminutes)
{
make = pcmake;
model = pcmodel;
color = pccolor;
licenseNumber = pclnumber;
minutes = pcminutes;
}

/** The copy constructor initializes the object
as a copy of another ParkedCar object.
@param object2 The object to copy.
*/

public ParkedCar(ParkedCar object2)
{
make = object2.make;
model = object2.model;
color = object2.color;
licenseNumber = object2.licenseNumber;
minutes = object2.minutes;
}

/**
toString method
@return A string containing the parked car information.
*/

public String toString()
{
// Create a string representing the object.
String str = "Parked car's make.....................: " + make +
"\nParked car's model..................: " + model +
"\nParked car's color..................: " + color +
"\nParked car's license plate number...: " + licenseNumber +
"\nMinutes that the car has been parked: " + minutes;

// Return the string.
return str;
}
}




parking meter class


public class ParkingMeter
{
public int minutesPurchased; // The number of minutes of parking time that has been purchased

/**
This constructor initializes the minutesPurchased field.
@param Minutes of parking time purchased.
*/

public ParkingMeter(int pmmpurchased)
{
minutesPurchased = pmmpurchased;
}

/**
The set method sets a value for each field.
@param Minutes of parking time purchased.
*/

public void set(int pmmpurchased)
{
minutesPurchased = pmmpurchased;
}

/** The copy constructor initializes the object
as a copy of another ParkingMeter object.
@param object2 The object to copy.
*/

public ParkingMeter(ParkingMeter object2)
{
minutesPurchased = object2.minutesPurchased;
}

/**
getMinutesPurchased method
@return The number of minutes of parking time that has been purchased.
*/

public int getMintuesPurchased()
{
return minutesPurchased;
}
}




parking ticket class


public class ParkingTicket
{
private double fine; // The amount of the fine.
private ParkedCar parkedcar; //
private ParkingMeter parkingmeter; //
private PoliceOfficer policeofficer; //

/**
This constructor initializes the parkedcar, parkingmeter, and policeofficer fields.
@param parkedcar
@param parkingmeter
@param policeofficer
*/

public ParkingTicket(double fine, ParkedCar pcar, ParkingMeter pmeter)
{
// Create a new ParkedCar object, passing
// pcar as an argument to the copy constructor.
parkedcar = new ParkedCar(pcar);

// Create a new ParkingMeter object, passing
// pmeter as an argument to the copy constructor.
parkingmeter = new ParkingMeter(pmeter);

// Create a new PoliceOfficer object, passing
// pofficer as an argument to the copy constructor.
//policeofficer = new PoliceOfficer(pofficer);

this.fine = fine;
}



/**
toString method
@return A string containing the parking ticket information.
*/

public String toString()
{
// Create a string representing the object.
String str = "Illegally parked car info: " + parkedcar +
"\nParking meter info:" + parkingmeter +
"\nAmount of the fine...: " + fine;
//"\nPolice officer info: " + policeofficer;

// Return the string.
return str;
}
}




police officer


public class PoliceOfficer
{
private String name; // Name
private int badgeNumber; // Badge number
private ParkedCar parkedcar; // Initialize "parkedcar" object to be copied
private ParkingMeter parkingmeter; // Initialize "parkingmeter" object to be copied
private ParkingTicket parkingticket; //
public double fine = 0; //

/**
This constructor initializes the name and badgeNumber fields.
@param poname The police officer's name.
@param pobnumber The police officer's badge number.
*/

public PoliceOfficer(String poname, int pobnumber, ParkedCar pcar, ParkingMeter pmeter)
{
name = poname;
badgeNumber = pobnumber;

// Create a new ParkedCar object, passing
// pcar as an argument to the copy constructor.
parkedcar = new ParkedCar(pcar);

// Create a new ParkingMeter object, passing
// pmeter as an argument to the copy constructor.
parkingmeter = new ParkingMeter(pmeter);
}

/** The set method sets a value for each field.
@param poname The police officer's name.
@param pobnumber The police officer's badge number.
*/

public void set(String poname, int pobnumber)
{
name = poname;
badgeNumber = pobnumber;
}

/** The copy constructor initializes the object
as a copy of another PoliceOfficer object.
@param object2 The object to copy.
*/

public PoliceOfficer(PoliceOfficer object2)
{
name = object2.name;
badgeNumber = object2.badgeNumber;


// Create a new ParkedCar object, passing
// pcar as an argument to the copy constructor.
//parkedcar = new ParkedCar(pcar);

//Create a new ParkedCar object.
//parkedcar = new ParkedCar(pcar);

//Create a ParkingMeter object.
//parkingmeter = new ParkingMeter(pmeter);
}

public ParkedCar getParkedCar()
{
// Return a copy of the ParkedCar object.
return new ParkedCar(parkedcar);
}

public ParkingMeter getParkingMeter()
{
// Return a copy of the ParkingMeter object.
return new ParkingMeter(parkingmeter);
}

public void inspectParkedCar()
{
// Determine whether the car is illegally parked.
if (parkedcar.minutes > parkingmeter.minutesPurchased)
// Determine the amount of the fine.
if (parkedcar.minutes - parkingmeter.minutesPurchased <= 60)
fine = 25;
else
fine = 25 + (10 * ((parkedcar.minutes - parkingmeter.minutesPurchased) / 60));

// Create a ParkingTicket object, passing
// 3 objects as arguments to the constructor.
parkingticket = new ParkingTicket(fine, parkedcar, parkingmeter);
}
}




thanks again for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: ticket simulator

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: ticket simulator

Posted 22 January 2010 - 10:56 PM

in order to print to the console, you must use:
System.out.println("STRING");



the toString method you use only returns a string object, so now all you have to do is:
System.out.println(myParkedCar);



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1