繁体   English   中英

如何使一个类方法与数组中的另一个类方法一起使用?

[英]How can I make a class method work with another class method in an array?

对于课程,我想制作一个程序来模拟警察向停放时间过长的汽车发出停车罚单。 我假设创建四个类,ParkedCar、ParkingMeter、ParkingTicket 和 PoliceOfficer。 我只是停留在主要方法上,因为我必须制作一个包含两辆车的数组并制作另一个包含两个停车表的数组,并使一辆车违规,另一辆车不违规。 我怎样才能做到这一点?

停放汽车类

// 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;
}
}

停车收费表类

// 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;
}
}

停车票类

// 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");

}
}

Main方法伪代码

// 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  

您所拥有的数组看起来是正确的,但也许可以通过内联初始化进行改进。 您只需要使用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.");
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM