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

标签 java arrays class methods

对于类(class),我应该编写一个程序来模拟警察向停放时间过长的汽车开出 parking 罚单。我打算创建四个类:ParkedCar、ParkingMeter、ParkingTicket 和 PoliceOfficer。我只是停留在主要方法上,因为我必须制作一个由两辆车组成的数组,并制作另一个由两个 parking 计时器组成的数组,并使一辆车成为违规车辆,另一辆车成为非违规车辆。我怎样才能做到这一点?

停放汽车类

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

parking 计时器类

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

parking 票类别

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

最佳答案

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

关于java - 如何使一个类方法与数组中的另一个类方法一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41949498/

相关文章:

java - 如何在 JavaFX 3D 中制作 vector 图形样式线?

JavaScript 关联数组比较

python - 使用cython来早期类型化类属性

java - Scala 的类和对象的问题

java - 使用Python反序列化Java org.apache.kafka.common.serialization序列化对象

java - 从 ISO **数字** 货币代码中查找 CurrencyUnit

java - 显示错误未找到主类

javascript - 在 AngularJS 应用程序中本地保存数组数据,以便在两个应用程序之间共享

javascript - 如何让对象在数组中找到自己的索引?

python - 子类化 Tkinter.Text 以创建自定义小部件