如何使用其他类的方法

时间:2022-11-19 10:36:59

I am having trouble with using methods from other classes. I will post specific code below:

我在使用其他类的方法时遇到了麻烦。我将在下面发布具体代码:

import java.util.ArrayList;

/**
 * Train models a train containing a car class and a seat class.
 * 
 * @author Nicholas Howes, Carleton University
 * @version Oct 9th, 2017
 */

public class Train
{
    /** The cars in this train. */
    private ArrayList<Car> cars;

    /** 
     * Constructs an empty train; i.e., one that has no cars.
     */
    public Train()
    {
        cars = new ArrayList<Car>(0);
    }

    /**
     * Adds the specified car to this train.
     * @param car The car object being appended to the end of the list.
     */
    public void addCar(Car car)
    {
        cars.add(car);
    }

    /**
     * Returns this trains's list of cars. This method is intended for 
     * testing purposes only, and should not be called by other objects,
     * as it may be removed from the final version of this class.
     * 
     * @return A list of the cars in the train
     */
    public ArrayList<Car> cars()
    {
        return cars;
    }    

    /**
     * Attempts to issue a ticket for a business class seat or an
     * economy class seat, as specified by the method's argument.
     * It will attempt to book a seat in the first car of the appropriate
     * type, but if a seat is not available it will attempt to book a seat
     * in the second car of the appropriate type, and so on. 
     * A request to issue a ticket in a business-class car will never result
     * in a seat being reserved in an economy-class car, and vice-versa. 
     * Returns true if successful, false otherwise.
     */
    public boolean issueTicket(boolean businessClassSeat)
    {
        for(int i = 0; i < cars.size(); i++) {
            if(businessClassSeat == (isBusinessClass())) { 
                cars(i).bookNextSeat();
                if(bookNextSeat == true) {
                         i = cars.size();
                         return true;
                }
            } 
        }
        return false;
        }

    /**
     * Cancels the ticket for the specified seat in the specified car.
     * Returns true if successful, false otherwise.
     */
    public boolean cancelTicket(int carId, int seatNo)
    {
        return false;
    }
   }

/**
 * Car models a car in a passenger train.
 * 
 * @author Nicholas Howes, Carleton University
 * @version 1.0 September 29, 2017
 */

public class Car
{
    /** This car's identifier. */
    private int id;

    /**
     * true == this car is a business-class car,
     * false == this car is an economy-class car.
     */
    private boolean businessClass;

    /** The cost of a business class seat, in dollars. */
    public static final double BUSINESS_SEAT_COST = 125.0;

    /** The cost of an economy class seat, in dollars. */
    public static final double ECONOMY_SEAT_COST = 50.0;    

    /** The number of seats in a business class car. */
    public static final int BUSINESS_SEATS = 30;   

    /** The number of seats in an economy class car. */
    public static final int ECONOMY_SEATS = 40;   

    /** The list of this car's seats. */
    private Seat[] seats;

    /**
     * Constructs a new Car object with the specified id.
     * If parameter isBusinessClass is true, the car is a business-class
     * car. If parameter isBusinessClass is false, the car is an
     * economy-class car.
     * @param carId The car id # for the new car object.
     * @param isBusinessClass The boolean value corresponding to the class 
     * of the car.
     */
    public Car(int carId, boolean isBusinessClass)
    {
        id = carId;
        businessClass = isBusinessClass;
        int Count = 1;
        if (businessClass == true) {
            seats = new Seat[BUSINESS_SEATS];
            for(int i = 0; i < seats.length; i++) {
                seats[i] = new Seat(Count, BUSINESS_SEAT_COST);
                Count++;
            }
        } else {
            seats = new Seat[ECONOMY_SEATS];
            for(int i = 0; i < seats.length; i++) {
                seats[i] = new Seat(Count, ECONOMY_SEAT_COST);
                Count++;
            }
        }
    }

    /**
     * Returns this car's list of seats. This method is intended for 
     * testing purposes only, and should not be called by other objects,
     * as it may be removed from the final version of this class.
     * 
     * @return The seats in this car, an array of Seat objects.
     */
    public Seat[] seats()
    {
        return seats;
    }

    /** 
     * Returns true if this is a business-class car,
     * false if this is an economy-class car.
     * @return true The boolean value returned if the car is business class.
     * @return false The boolean value returned if the car is economy class.
     */
    public boolean isBusinessClass()
    {
        if(businessClass == true) {
            return true;
        }
        return false;
    }

    /**
     * Returns the id of this car.
     * @return id The value returned is the id # of the car.
     */
    public int id()
    {
        return id;
    }

    /**
     * This method is called when the specified seat has been booked,
     * to print a ticket for that seat.
     * 
     * @param seatNo The integer identifier of the seat.
     */
    private void printTicket(int seatNo)
    {
        System.out.println("Car ID: " + id);
        System.out.println("Seat number: " + seatNo);
        System.out.println("Price: ");
        if (businessClass) {
            System.out.println(BUSINESS_SEAT_COST);
        } else {
            System.out.println(ECONOMY_SEAT_COST);
        }
    }   

    /**
     * Attempts to book a seat. If successful, this method prints a 
     * ticket and returns true.
     * If no seats are available, this method returns false.
     * @return true The return value when a new seat is booked.
     * @return false The return value when no seats are available.
     */
    public boolean bookNextSeat()
    {
        // After booking an available seat, print the ticket by calling
        // private method printTicket(); e.g.,
        // printTicket(seats[i].number());
        for(int i = 0; i < seats.length; i++) {
            if(seats[i].isBooked() == false) {
            seats[i].book();
            printTicket(seats[i].number());
            return true;
            }
        }
        return false;
    }

    /** 
     * Cancels the booking for the specified seat, which must be between
     * 1 and the maximum number of seats in the car.
     * If the seat number is valid and if the seat has been reserved, this
     * method cancels the booking for that seat and returns true. 
     * If the seat number is not valid, this method returns false. 
     * If the seat number is valid, but the seat has not been reserved, 
     * this method returns false.
     * @param seatNo The seat number to cancel if it is booked and valid.
     */
    public boolean cancelSeat(int seatNo)
    {
        if(seatNo < 1 || seatNo > seats.length) {
            return false;
        }
        if(seats[seatNo-1].isBooked() == false) {
            return false;
        }
        seats[seatNo-1].cancelBooking();
        return true;
    }    
}

/**
 * Seat models a seat in a car in a passeger train.
 *
 * @author D.L. Bailey, SCE
 * @version 1.00 January 28, 2007
 */
public class Seat
{
    private int number;     // the seat's number
    private boolean booked; // has this seat has been reserved?
    private double price;   // the cost of a ticket for this seat, in dollars

    /**
     * Constructs a new Seat with the specified seat number and
     * ticket price.
     */
    public Seat(int seatNo, double cost)
    {
        booked = false;
        number = seatNo;
        price = cost;
    }

    /**
     * Returns the cost of purchasing a ticket for this Seat.
     */
    public double price()
    {
        return price;
    }

    /**
     * Returns this seat's number.
     */
    public int number()
    {
        return number;
    }

    /**
     * Returns true if someone has purchased a ticket
     * for this Seat.
     */
    public boolean isBooked()
    {
        return booked;
    }

    /**
     * If this seat is available, books it and returns true.
     * If the seat is not available, returns false.
     */
    public boolean book() 
    {
        if (!booked) {
            booked = true;
            return true;
        }
        return false;
    }

    /**
     * If this seat is booked, cancels the booking and returns true.
     * If the seat was not booked, returns false.
     */
    public boolean cancelBooking() 
    {
        if (booked) {
            booked = false;
            return true;
        }
        return false;
    }
}

The problem I am having is with the method boolean issueTicket in class Train. I understand I can use the methods from my Car class to search all cars of the specified type (the parameter passed in issueTicket) either being business or economy (true or false) and then using the method bookNextSeat to book the next empty seat then exit the for loop if it returns true by setting i equal to the arraylist size. What am I doing wrong with all of these method uses from class car?

我遇到的问题是使用类Train中的方法boolean issueTicket。我知道我可以使用我的Car类中的方法来搜索指定类型的所有汽车(在issueTicket中传递的参数),无论是商业还是经济(真或假),然后使用方法bookNextSeat预订下一个空座位然后退出如果通过设置i等于arraylist大小返回true,则为for循环。我从班车中使用所有这些方法做错了什么?

1 个解决方案

#1


1  

First of all, statement i = cars.size(); is required at all, you can use break; instead to break out of the loop. I would suggest take a method scope variable boolean status = false and update value of status only when bookNext returns true and call break and return the status variable instead of hardcoded true false.

首先,声明i = cars.size();是必需的,你可以使用休息;而是摆脱循环。我建议采用方法范围变量boolean status = false并仅在bookNext返回true并调用break时更新status的值并返回状态变量而不是hardcoded true false。

Since return statement is only within the scope of loop method won’t be able to return anything, in fact you will get missing return statement error.

由于return语句只在loop方法范围内,因此无法返回任何内容,实际上你会得到缺少的return语句错误。

#1


1  

First of all, statement i = cars.size(); is required at all, you can use break; instead to break out of the loop. I would suggest take a method scope variable boolean status = false and update value of status only when bookNext returns true and call break and return the status variable instead of hardcoded true false.

首先,声明i = cars.size();是必需的,你可以使用休息;而是摆脱循环。我建议采用方法范围变量boolean status = false并仅在bookNext返回true并调用break时更新status的值并返回状态变量而不是hardcoded true false。

Since return statement is only within the scope of loop method won’t be able to return anything, in fact you will get missing return statement error.

由于return语句只在loop方法范围内,因此无法返回任何内容,实际上你会得到缺少的return语句错误。