无法到达的语句是:thisCustomer = findCustomer(theCustomerID);
. 我不知道为什么。thisCustomer
是Customer
object的一个属性,findCustomer()
是下面列出的一个方法,theCustomerID
是这个语句所在方法的参数。问题出在rentOutApt(int theAptID, int theCustomerID, int theMonthsToRent) //3. 此代码位于底部。
import java.util.ArrayList;
import java.util.Iterator; // If you choose to use one.
/**
* .AptRentalAgency.
* Controller class.
*
* Name:
* Comment:
*
* For Dr. Nikhil Srinivasan's MIST 4600 Exam02 on 20140321.
*
* @author Dr. Nikhil Srinivasan
* @version 20140317
*
*/
public class AptRentalAgency
{
// instance variables
private int currentDate;
// These match the Customer objects:
private int customerID;
private String customerName;
private double customerBalance;
private int customerAptID; // 0 if he or she does not currently have a apartment.
private int customerCreditRating;
// These match the Apartment objects:
private int aptID;
private String aptName;
private String aptAddress;
private int aptSize;
private double aptRent;
// ****** The following are set when the apt is rented:
private int aptCustomerID; // 0 if not rented.
private double aptDeposit; // 0 if not rented
private int aptMonthsRented; // 0 if not rented.
private int aptRentalDate; // 0 if not rented.
// There are a number of important nonfields that are to be computed
// in their accessor (get) methods as needed:
private double aptDepositMultiplier;
private double aptTotalCost;
// These are for the current Customer and current Apt objects:
Customer thisCustomer;
Apartment thisApt;
// These are the needed ArrayLists:
ArrayList<Customer> customers;
ArrayList<Apartment> apts;
// ReadFile objects:
private ReadFile aptReader; // Declare a ReadFile object to read from the AptData.txt file.
private ReadFile customerReader; // Declare a ReadFile object to read from the customerData.txt file.
/**
* AptRentalAgency Constructor
*/
public AptRentalAgency()
{
// Set the currentDate:
currentDate = 20140321;
// Create the ArrayLists:
customers = new ArrayList<Customer>();
apts = new ArrayList<Apartment>();
// Read in the Apt objects and Customer objects. Load them into the ArrayLists.
readApts();
readCustomers();
}
/**
* .readApts.
* Reads in the Apt objects.
*/
private void readApts()
{
aptReader = new ReadFile("AptData.txt");
aptReader.setSeparator(",");
// Read and Load the Data:
for(aptReader.readInputLine(); !aptReader.eof(); aptReader.readInputLine())
{
// Load the data into fields
aptID = aptReader.getIntField(1);
aptName = aptReader.getStringField(2);
aptAddress = aptReader.getStringField(3);
aptSize = aptReader.getIntField(4);
aptRent = aptReader.getDoubleField(5);
aptCustomerID = aptReader.getIntField(6);
aptDeposit = aptReader.getDoubleField(7);
aptMonthsRented = aptReader.getIntField(8);
aptRentalDate = aptReader.getIntField(9);
// Construct thisApt
thisApt = new Apartment(aptID, aptName, aptAddress, aptSize, aptRent,
aptCustomerID, aptDeposit, aptMonthsRented, aptRentalDate);
// Add thisApt to the apts ArrayList.
apts.add(thisApt);
}
// End of Loop
System.out.println("\nAll apts read from the file and added to the ArrayList.\n");
}
/**
* .readCustomers.
* Reads in the Customer objects.
*/
private void readCustomers()
{
customerReader = new ReadFile("CustomerData.txt");
customerReader.setSeparator(",");
// Read and Load the Data:
for(customerReader.readInputLine(); !customerReader.eof(); customerReader.readInputLine())
{
// Load the data into fields
customerID = customerReader.getIntField(1);
customerName = customerReader.getStringField(2);
customerBalance = customerReader.getDoubleField(3);
customerAptID = customerReader.getIntField(4);
customerCreditRating = customerReader.getIntField(5);
// Construct thisCustomer
thisCustomer = new Customer(customerID, customerName, customerBalance, customerAptID, customerCreditRating);
// Add thisCustomer to the customers ArrayList.
customers.add(thisCustomer);
}
// End of Loop
System.out.println("\nAll customers read from the file and added to the ArrayList.\n");
}
/**
* .printAllCustomers.
*/
public void printAllCustomers()
{
// Print a header, then the list.
System.out.println("\nCurrent Customer List on " + currentDate + "\n");
System.out.println("CustID Name Balance Apt ID CreditRating");
System.out.println("------ ----------------- ------- ------- ------------");
for(Customer theCustomer: customers)
{
theCustomer.printInfo();
}
System.out.println("\nEnd of Customer List. \n");
}
/**
* .printAllApts.
*/
public void printAllApts()
{
// Print a header, then the list.
System.out.println("\nCurrent Apt List on " + currentDate + "\n");
System.out.println("AptID Apt Name Address Size Rent RenterID Deposit MonthsRented RentalDate");
System.out.println("----- ------------------- -------------------------- ---- ------- -------- ------- ------------ ----------");
for(Apartment theApt: apts)
{
theApt.printInfo();
}
System.out.println("\nEnd of Apt List. \n");
}
// ****************** Do your work after this point ********************************
/**
* .find a customer.
*/
public Customer findCustomer(int theCustomerID)
{
for(Customer theCustomer: customers)
{
if (theCustomer.getCustomerID() == theCustomerID)
{
return theCustomer;
}
}
return null;
}
/**
* .find Apartment.
*/
public Apartment findApartment(int theAptID)
{
for(Apartment theApt: apts)
{
if (theApt.getAptID() == theAptID)
{
return theApt;
}
}
return null;
}
/**
* .customerAddMoney.
* Part a
*/
public void customerAddMoney(int theCustomerID, double theAmountToAdd)
{
// Find the customer, If the Customer does not exist print a message and return(exit the method)
thisCustomer = findCustomer(theCustomerID);
// Check the amount provided to see if it is less than zero. If it is less than zero then print a message and return(exit the method)
if( thisCustomer == null)
{
System.out.println("Customer " + theCustomerID + " does not exist. Returning!");
return;
}
// Update the customer balance by adding the amount to the existing customer balance.
double customerBalance = thisCustomer.getCustomerBalance() + theAmountToAdd;
thisCustomer.setCustomerBalance(customerBalance);
}
/**
* .checkOutApt.
* Parts b and c.
*/
public void rentOutApt(int theAptID, int theCustomerID, int theMonthsToRent)
{
// PART B:
// 1 Find the specific theAptID Apartment object and load it into thisApt.
// If null, print a message that the apt does not exist and return.
thisApt = findApartment(theAptID);
// 2 Verify that thisApt Apartment is available (not rented out to a customer).
// If not, print a message that the apt is already rented out and return.
if(thisApt.getAptRentalDate() == 0)
{
System.out.println("Apartment " + theAptID + " is available. Returning!");
return;
}
else
{
System.out.println("Apartment " + theAptID + " is rented out. Returning!");
return;
}
// 3 Find the specific theCustomerID object and load it into thisCustomer.
// If null, print a message that the customer does not exist and return.
thisCustomer = findCustomer(theCustomerID);
if(thisCustomer == null)
{
System.out.println("Customer " + theCustomerID + " does not exist. Returning!");
return;
}
// 4 Verify that thisCustomer Customer does not already have an apartment.
// If he or she does, print a message that the customer already has an apartment
// and return.
if(thisCustomer.getCustomerAptID() != 0)
{
System.out.println("*** Customer is already living in: " + customerAptID);
return;
}
// 5 Verify that the thisCustomer Customer has enough money to rent the apt.
// The initial payment from a customer to rent an apartmentis the sum of the calculated
// deposit based on customer credit rating (the getAptDepositAmount method)
// and the first month's rent.
//
// You will need to get the theCustomer’s balance and compare it to the sum of
// thisApt (getAptRent and getAptDepositAmount methods).
double customerBalance = thisApt.getAptDepositAmount(thisCustomer.getCustomerCreditRating()) + thisApt.getAptRent();
if(customerBalance > thisCustomer.getCustomerBalance())
{
System.out.println();
System.out.println("The customer does not have enough money.");
System.out.println("The customer is " + customerBalance + " short.");
System.out.println();
return;
}
// PART C:
// 6 Now check out theAptID Apt object to thisCustomer Customer object.
// In thisCustomer, set customerAptID to theAptID.
thisCustomer.setCustomerAptID(theAptID);
// In thisApt, set
// * aptCustomerID to theCustomerID
thisApt.setAptCustomerID(theCustomerID);
// In thisApt, set
// * aptDeposit to the deposit amount you find after using the
// getAptDepositAmount(int custCreditRating) method in Apartment Class
thisApt.setAptDeposit(thisApt.getAptDepositAmount(thisCustomer.getCustomerCreditRating()));
// set * aptMonthsRented theMonthsToRent
thisApt.setAptMonthsRented(theMonthsToRent);
// * aptRentalDate to currentDate
thisApt.setAptRentalDate(currentDate);
}
}