2017-01-31 15 views
1

對於班級,我想製作一個程序來模擬警察向停放太久的汽車發放停車票。我想創建四個班,ParkedCar,ParkingMeter,ParkingTicket和PoliceOfficer。我只是停留在主要方法上,因爲我必須製造兩輛汽車的陣列,並且製造另一個兩個ParkingMeter陣列,並且使一輛車成爲違規車輛,另一輛是非違規車輛。我怎樣才能做到這一點?我怎樣才能使類方法與數組中的另一個類方法一起工作?

ParkedCar類

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

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

// 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 + "]"; 
} 
} 

Main方法

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

我不確定我完全理解你卡在什麼上。是「有巡警......」嗎? – 4castle

+0

我被困在主要的方法,我不得不爲兩個車對象做一個數組,我必須做兩個ParkingMeter對象的數組。我只是想知道如何將陣列中的兩個汽車物體拿下來,並讓一個汽車物體非法停放,並讓另一個汽車物體合法停放? –

回答

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."); 
      } 
     } 
    } 
} 
+0

謝謝,我以爲我是由於某種原因使數組不正確。 –

相關問題