对于课程,我想制作一个程序来模拟警察向停放时间过长的汽车发放停车罚单。我想创建四个类,ParkedCar,ParkingMeter,ParkingTicket和PoliceOfficer。我只是坚持使用主要方法,因为我必须制作一个由两辆车组成的阵列并制作两个ParkingMeter的另一个阵列,并使一辆车违规,另一辆不违规。我怎么能这样做?
ParkedCar Class
// a class of type ParkedCar
public class ParkedCar {
// the class fields
private String make;
private String model;
private String color;
private String licenseNumber;
private int minutesParked;
// the class constructor
public ParkedCar(String mk, String mdel, String col, String lic, int minParked) {
// assign the constrictor fields to the class fields
make = mk;
model = mdel;
color = col;
licenseNumber = lic;
minutesParked = minParked;
}
// the copy constructor
public ParkedCar copy(ParkedCar car2) {
ParkedCar copyObject = new ParkedCar(make, model, color, licenseNumber, minutesParked);
return copyObject;
}
// getter method for make of a car
public String getMake() {
return make;
}
// setter method for make of a car
public void setMake(String make) {
this.make = make;
}
// getter method for model of a car
public String getModel() {
return model;
}
// setter method for model of a car
public void setModel(String model) {
this.model = model;
}
// getter method for color of a car
public String getColor() {
return color;
}
// setter method for a color of a car
public void setColor(String color) {
this.color = color;
}
// getter method for a car licence number
public String getLicenseNumber() {
return licenseNumber;
}
// setter method for a car licence number
public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}
// getter method for minutes parked
public int getMinutesParked() {
return minutesParked;
}
// setter method for minutes parked
public void setMinutesParked(int minutesParked) {
this.minutesParked = minutesParked;
}
}
ParkingMeter Class
// a class of type ParkingMeter
public class ParkingMeter {
// the class fields
private int minutesPurchased;
// the class constructor
public ParkingMeter(int numMinPurchased) {
// assign the constrictor fields to the class fields
this.minutesPurchased = numMinPurchased;
}
// getter method for minutes purchased
public int getMinutesPurchased() {
return minutesPurchased;
}
// setter method for minutes purchased
public void setMinutesPurchased(int minutesPurchased) {
this.minutesPurchased = minutesPurchased;
}
}
警察官员班级
// a class of type PoliceOfficer
public class PoliceOfficer {
// the class fields
private String name;
private String badgeNumber;
// the class constructor
public PoliceOfficer(String officeName, String badgeNumber) {
// assign the constrictor fields to the class fields
name = officeName;
this.badgeNumber = badgeNumber;
}
// the copy constructor
public PoliceOfficer copy(PoliceOfficer officer) {
PoliceOfficer copyObject = new PoliceOfficer(name, badgeNumber);
return copyObject;
}
// the method patrol looks at the number of minutes a car has been parked and the
// number of minutes purchased
public ParkingTicket patrol(ParkedCar car, ParkingMeter meter) {
ParkingTicket ticket = null;
// Calculate the total number of minutes parked over minutes
// purchased
int illegalMinutes = car.getMinutesParked()
- meter.getMinutesPurchased();
// if illegalMinutes, give ticket
if (illegalMinutes > 0) {
// yes, it is illegally parked.
ticket = new ParkingTicket(car, this, illegalMinutes);
}
return ticket;
}
// a getter method to get name of officer
public String getName() {
return name;
}
// a setter method to set name of officer
public void setName(String name) {
this.name = name;
}
// a getter method to get officer badge number
public String getBadgeNumber() {
return badgeNumber;
}
// a setter method to set officer badge number
public void setBadgeNumber(String badgeNumber) {
this.badgeNumber = badgeNumber;
}
}
ParkingTicket Class
// a class of type ParkingTicket
public class ParkingTicket {
// the class fields
private ParkedCar car;
private PoliceOfficer officer;
private double fine;
private int minutes;
public final double BASE_FINE = 25.0;
public final double HOURLY_FINE = 10.0;
// the class constructor
public ParkingTicket(ParkedCar aCar, PoliceOfficer anOfficer, int meterMins) {
// assign the constrictor fields to the class fields
car = aCar;
officer = anOfficer;
minutes = meterMins;
}
// a copy constructor
public ParkingTicket copy(ParkingTicket ticket) {
ParkingTicket copyObject = new ParkingTicket(car, officer, minutes);
return copyObject;
}
// The method calculateFine calculates the amount of a parking fine
public void calculateFine() {
double hours = minutes / 60.0;
int hoursAsInt = (int) hours;
if ((hours - hoursAsInt) > 0) {
hoursAsInt++;
}
// Assign the base fine.
fine = BASE_FINE;
// Add the additional hourly fines.
fine += (hoursAsInt * HOURLY_FINE);
}
// getter method to get a car
public ParkedCar getCar() {
return car;
}
// setter method to set a car
public void setCar(ParkedCar car) {
this.car = car;
}
// getter method to get officer
public PoliceOfficer getOfficer() {
return officer;
}
// setter method to set officer
public void setOfficer(PoliceOfficer officer) {
this.officer = officer;
}
// getter method to get fine
public double getFine() {
return fine;
}
// setter method to set fine
public void setFine(double fine) {
this.fine = fine;
}
// getter method to get minutes
public int getMinutes() {
return minutes;
}
// setter method to set minutes
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public String toString() {
return "ParkingTicket [car=" + car + ", officer=" + officer
+ ", fine=" + fine + ", minutes=" + minutes
+ ", BASE_FINE=" + BASE_FINE + ", HOURLY_FINE="
+ HOURLY_FINE + "]";
}
}
主要方法
// a class of type PatrolSimulation
public class PatrolSimulation {
// the main method
public static void main(String[] args) {
// an array of 2 Car objects, with various minutesParked values
ParkedCar[] car = new ParkedCar[2];
car[0] = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 100);
car[1] = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 30);
// an array of 2 ParkingMeter objects, with minutes so that
// the first Car object is in violation, while the second is not
ParkingMeter[] meter = new ParkingMeter[2];
meter[0] = new ParkingMeter(30);
meter[1] = new ParkingMeter(40);
// an array of 2 ParkingTicket objects
ParkingTicket[] ticket = new ParkingTicket[2];
// a PoliceOfficer object
PoliceOfficer officer = new PoliceOfficer("Sargent Cody", "007");
}
}
主要方法伪代码
// Create an array of 2 Car objects, with various minutesParked values
// Create an array of 2 ParkingMeter objects, with minutes so that
// the first Car object is in violation, while the second is not
// Create an array of 2 ParkingTicket objects
// Create a PoliceOfficer object. Give the officer a name and badge
// number
// Have the officer patrol each of the Car and ParkingMeter object
// combinations (index i for the array of Car objects should be
// matched with index i for the array of ParkingMeter objects, which
// should be matched with index i of the array of ParkingTicket
// objects)
// After the PoliceOfficer has patrolled the cars and parking
// meters, walk over the array of ParkingTickets and invoke the
// toString method if a ticket has been issued, otherwise indicate
// that a ticket has not been issued
答案 0 :(得分:1)
您看起来正确的数组,但也许可以使用内联初始化进行改进。您只需要使用for
循环迭代这些数组:
public class PatrolSimulation {
public static void main(String[] args) {
ParkedCar[] cars = new ParkedCar[] {
new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 100),
new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 30)
};
ParkingMeter[] meters = new ParkingMeter[] {
new ParkingMeter(30),
new ParkingMeter(40)
};
ParkingTicket[] tickets = new ParkingTicket[cars.length];
PoliceOfficer officer = new PoliceOfficer("Sargent Cody", "007");
for (int i = 0; i < cars.length; i++) {
tickets[i] = officer.patrol(cars[i], meters[i]);
}
for (ParkingTicket ticket : tickets) {
if (ticket != null) {
ticket.calculateFine();
System.out.println(ticket.toString());
} else {
System.out.println("No ticket issued.");
}
}
}
}