我有一个Project类。车辆类。所有者类。还有一个集合类。该项目采用命令行参数,但不要担心它们是正确的。
集合类有5种排序方法,我现在不需要担心。我需要帮助打印出一份UNSORTED格式的车主信息列表。
Owner类在第80行给我一个错误.getVehicle()方法。我不确定这种方法如何收集信息!有人可以启发我吗?
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());
}
}
这是Vehicle类。没有丢失任何错误。
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;
}
} 这是我未使用的集合类。运行时,它应显示未排序的信息。 (无视排序方法)
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) {
}
}
我的Project类
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());
}
}
}
答案 0 :(得分:1)
public Vehicle getVehicle() {
return vehicle;
}
vehicle
是一个数组。该方法的签名应为:
public Vehicle[] getVehicle()