内部类toString()

时间:2018-02-07 19:49:38

标签: java tostring

似乎无法让toString()工作,如何格式化,当合法停车时我无法fine成为0.00。这是我第一次在程序中使用多个类,我在这里有点迷失。我认为我在访问toString()方法时遇到了问题,因为它在内部类中,但我不知道另一种方法。

/**Homework #####
 * Author: Thomas Woodward
 * Project Purpose: 
 * Input:  
 * Desired Output:  
 * Variables and Classes: 
 * Formulas:  
 * Main algorithm: 
 * January 00, 2018
 **/

package parkingticketsimulator;


public class PoliceOfficer
{
    private String name;
    private String badge;
    private boolean expired;
    private ParkedCar car;
    private ParkingMeter meter;

    //---------- Constructor
    public PoliceOfficer(String n, String b)
    {
        name = n;
        badge = b;        
    }

    //---------- Empty Constructor
    public PoliceOfficer()
    {
        name = "";
        badge = "";        
    }

    //---------- Copy Constructor
    public PoliceOfficer(PoliceOfficer object2)
    {
        name = object2.name;
        badge = object2.badge;
    }

    public boolean checkMeter()
    {
        if (meter.minutesPurchased <= car.minutesParked)
        {
            expired = true;
            ParkingTicket ticket = new ParkingTicket(car, meter, this);
            System.out.println(ticket.toString());
            return expired;
        } else {
            expired = false;
            return expired;
        }
    }


    //---------- Mutators
    public void setName(String n)
    {
        name = n;
    }

    public void setBadge(String b)
    {
        badge = b;
    }

    //---------- Accessors
    public String getName()
    {
            return name;
    }

    public String getBadge()
    {
        return badge;
    }


    /**/
    /**INNER CLASS PARKINGTICKET**/
    /**/

    public class ParkingTicket
    {
        public ParkedCar car;
        public ParkingMeter meter;
        public PoliceOfficer officer;
        private double fine;

        //---------- Constructor
        public ParkingTicket(ParkedCar cr, ParkingMeter mtr, PoliceOfficer     ofr)
        {
            car = new ParkedCar(cr);
            meter = new ParkingMeter(mtr);
            officer = new PoliceOfficer(ofr);
            calculateFine();
        }

        private void calculateFine()
        {
            if (car.minutesParked - meter.minutesPurchased > 60) 
            {
                fine = ((car.minutesParked / 60) * 10.00) + 25.00;
            } else {
                fine = 25.00;
            }
        }

        @Override
        public String toString()
        {
            String str = String.format("\n\nPARKING TICKET"
                                            + "============\n"
                                            + "CAR\n"
                                            + "Make: %s\n"
                                            + "Model: %s\n"
                                            + "Color: %s\n"
                                            + "License Plate: %s\n"
                                            + "============\n"
                                            + "FINE: $%,.2f\n"
                                            + "-----------------------------    ----\n"
                                            + "Issuing Officer: %s\n"
                                            + "Badge #%s", 
                                            car.make,
                                            car.model, 
                                            car.color,
                                            car.license, 
                                            fine, 
                                            officer.name, 
                                            officer.badge);
            return str;        
        }
    }
}

还有其他课程,但我不是他们需要解决这个问题。不过我可能错了。

0 个答案:

没有答案