简体   繁体   中英

Need help printing Vehicle/Owner Data in Java program

I have a Project class. A Vehicle class. An Owner class. And a collection class. This project takes command line arguments but not to worry they are correct.

The collection class has 5 sorting methods which I don't need to worry about for now. I need help printing out an UNSORTED formatted list of owner vehicle information.

The Owner class is throwing me an error at line 80. The getVehicle() method. I'm not sure how this method collects the information! Could someone enlighten me?

 package project5;

/**
 * Provides the properties and methods for an owner.
 */
public class Owner {
    private static final String ROW = "%-12s%-12s%-49s%-17s%-41s";
    private static final String ADDRESS = "%s %s, %s  %s";

// Owner properties
    private String address;
    private String city;
    private String firstName;
    private String lastName;
    private String phone;
    private String state;
    Vehicle[] vehicle;

    private String zip;

/**
 * Creates an object instance of the Owner class.
 * @param ownerData The data representing the owner being stored.
 */
protected Owner(String[] ownerData) {
    lastName = ownerData[0];
    firstName = ownerData[1];
    address = ownerData[2];
    city = ownerData[3];
    state = ownerData[4];
    zip = ownerData[5];
    phone = ownerData[6];
    //vehicle = ownerData[7];
}





/**
 * Returns the city of the owner.
 * @return the city of the owner.
 */
public String getCity() {
    return city;
}

/**
 * Returns a formatted String containing the header for an owner report.
 * @return a formatted String containing the header for an owner report.
 */
public String getHeader() {
    return String.format("%-12s%-12s%-49s%-17s%-12s%-16s%-6s%-7s\n%-12s%-12s%-49s%-17s%-12s%-16s%-6s%-7s", 
            "Last Name", "First Name", "Address", "Phone", "Make", "Model", "Year", "Mileage", 
            "---------", "----------", "-------", "-----", "----", "-----", "----", "-------");
}

/**
 * Returns the last name of the owner.
 * @return the last name of the owner.
 */
public String getLastName() {
    return lastName;
}

/**
 * Returns the state of the owner.
 * @return the state of the owner.
 */
public String getState() {
    return state;
}

/**
 * Returns the vehicle of the owner.
 * @return the vehicle of the owner.
 */
public Vehicle getVehicle() {

 return vehicle; //This method won't return  
}

/**
 * Returns a formatted String containing information about this owner.
 * @return a formatted String containing information about this owner.
 */
@Override
public String toString() {
    return String.format(ROW, lastName, firstName, String.format(ADDRESS, address, city, state, zip), phone, vehicle.toString());
}

}

This is the Vehicle class. Not throwing any errors.

package project5;

/**
 * Provides the properties and methods for a vehicle.
 */
public class Vehicle {
    private static final String ROW = "%-12s%-16s%-6d%7d";    

// Vehicle properties
private String make;
private int mileage;
private String model;
private int year;

/**
 * Creates an object instance of the Vehicle class.
 * @param vehicleData The data representing the vehicle being stored.
 */
public Vehicle(String[] vehicleData) {
    make = vehicleData[7];
    model = vehicleData[8];
    year = Integer.parseInt(vehicleData[9]);
    mileage = Integer.parseInt(vehicleData[10]);

}

/**
 * Returns the mileage of the vehicle.
 * @return the mileage of the vehicle.
 */
public int getMileage() {
    return mileage;
}

/**
 * Returns a formatted String containing information about this vehicle.
 * @return a formatted String containing information about this vehicle.
 */
@Override
public String toString() {
    return String.format(ROW, make, model, year, mileage);
}

/**
 * Returns the year of the vehicle.
 * @return the year of the vehicle.
 */
public int getYear() {
    return year;
}

} This is my unused collection class. When run it should display unsorted information. (Disregarding the sorting methods)

package project5;

/**
 * Provides the properties and methods for a collection of owners.
 */
public class Collection {
// Sorting descriptions
public static final int CITY_STATE = 0;
public static final int LAST_NAME = 1;
public static final int MILEAGE = 2;
public static final int UNSORTED = 3;
public static final int YEAR = 4;

private int numOwners;

private Owner[] owners;
private String[] sortTypes = new String[] {"Sorted by City and State", "Sorted by Last Name", "Sorted by Mileage", "Unsorted", "Sorted by Year"};

/**
 * Creates an object instance of the Collection class and initializes the owner collection.
 * @param maxSize The maximum number of owners stored in this collection.
 */
public Collection(int maxSize) {
    owners = new Owner[maxSize];
}

/**
 * Adds a owner to the collection.
 * @param owner The owner to add.
 */
public void add(Owner owner) {
    owners[numOwners++] = owner;
}

/**
 * Displays information about the owners and the vehicles they own.
 */
public void displayOwners() {
    displayOwners(sortTypes[UNSORTED]);
}

/**
 * Displays information about the owners and the vehicles they own.
 * 
 * @param sortType The description of the sort performed on the owner data.
 */
private void displayOwners(String sortType) {
    System.out.format("\nOwner Information (%s):\n\n",sortType);
    System.out.println(owners[0].getHeader());
    for (Owner owner : owners) {
        System.out.println(owner.toString());
    }
}

/**
 * Returns the total number of owners stored in this collection.
 * @return the total number of owners stored in this collection.
 */
public int getTotal() {
    return owners.length;
}

/**
 * Determines which sorting method to call based on the specified sort type.
 *
 * Note: Once the data is sorted, this method displays the owner information.
 *
 * @param sortType The type of sort to perform.
 */
public void sortData(int sortType) {
    switch(sortType) {
        case 0: // City and State
            sortByOwnerCityState();
            break;
        case 1: // Last Name
            sortByOwnerLastName();
            break;
        case 2: // Mileage
            sortByVehicleMileage();
            break;
        case 3: // Unsorted
            break;
        case 4: // Year
            sortByVehicleYear();
            break;
        default:
            System.out.println("Error:  Illegal Sort Type Selected");
    }
    displayOwners(sortTypes[sortType]);
}

/**
 * Sorts the owner data by State, and then by City using the Bubble Sort algorithm.
 */
private void sortByOwnerCityState() {
    /* False Data Generator
        for ( traversal loop)
            for (comparision
                if (a[x].state compareTo(a[x+1]state)>0)
                    swap(x,x+1);

    else if (a[x].state.compareTo(a[x+1]state)==0) &&
            (a[x].city.CompareTo(a[x+1]state)>0)
                swap(x,x+1);
    */
}

/**
 * Sorts the owner data by Last Name using the Bubble Sort algorithm.
 */
private void sortByOwnerLastName() {

}

/**
 * Sorts the owner data by vehicle Mileage using the Bubble Sort algorithm.
 */
private void sortByVehicleMileage() {

}

/**
 * Sorts the owner data by vehicle Year using the Bubble Sort algorithm.
 *
 * Note:  The data must be sorted in descending order (newest to oldest).
 */
private void sortByVehicleYear() {

}

/**
 * Swaps values between the two specified locations in an array.
 * @param pos1 The position of the first value to swap.
 * @param pos2 The position of the second value to swap.
 */ 
private void swap(int pos1, int pos2) {

}
}

And my Project class

package project5;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
  Manages owner information.
 */
public class Project5 {
    private static final int MAX_OWNERS = 50;

    private Collection owners;

public static void main(String[] args) {
    System.out.println("Ima Java Programmer\nProject 5");

    // Creates an object of the Project5 class, calls the execute method, and
    // passes the filename of the input file provided in the command-line arguments
    new Project5().execute(args[0]);
}

/**
 * Creates an object instance of the Project5 class and initializes the owner collection.
 */
public Project5() {
    owners = new Collection(MAX_OWNERS);
}

/**
 * Controls the operation of the program.
 * @param filename The path and name of the input data file.
 */
private void execute(String filename) {
    readFile(filename);
    owners.displayOwners();
    owners.sortData(Collection.LAST_NAME);
    owners.sortData(Collection.MILEAGE);
    owners.sortData(Collection.YEAR);
    owners.sortData(Collection.CITY_STATE);
}

/**
 * Reads data from the specified input file into the program.
 * @param filename The path and name of the input data file.
 * @return false if an error occurred while reading the file; otherwise, true.
 */
private void readFile(String filename) {
    String line;

    try {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        while((line = br.readLine()) != null) {
            owners.add(new Owner(line.split(",")));
        }
    }
    catch(FileNotFoundException fnfe) {
        System.out.format("The program could not find the file: %s.\n", filename);
    }
    catch(IOException ioe) {
        System.out.format("An error occurred while reading the file: %s.\n", filename);
    }
    catch(Exception ex) {
        System.out.format("An unexpected error occurred.  Error information: %s.\n", ex.getMessage());
    }
}

}

public Vehicle getVehicle() {
  return vehicle;
}

vehicle is an array. The method's signature should be:

public Vehicle[] getVehicle()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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