2014-10-21 2 views
-4

도달 할 수없는 문은 thisCustomer = findCustomer(theCustomerID);입니다. 나는 이유를 알 수 없다. thisCustomerCustomer 개체의 특성이며 findCustomer()은 아래 나열된 메서드이며 theCustomerID은이 문이있는 메서드의 매개 변수입니다. 문제는 rentOutApt (intAptID, int theCustomerID, int theMonthsToRent) // 3입니다. 이 코드는 맨 아래쪽에 있습니다.이것이 도달 할 수없는 진술 인 이유는 무엇입니까? Java

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); 


    } 

} 
+3

코드에 오류가 있습니다. 문 앞에서 이미 메서드를 닫고 있습니다. –

+0

메서드에있는 것처럼 보이지 않습니다. –

+1

중괄호를 신중하게 일치시킵니다. – rossum

답변

2

라인 237-246은 다음과 같습니다이 보장되는

if(thisApt.getAptRentalDate() == 0) 
{ 
    System.out.println("Apartment " + theAptID + " is available. Returning!"); 
    return; 
} 
else 
{ 
    System.out.println("Apartment " + theAptID + " is rented out. Returning!"); 
    return; 
} 

반환 할 수 있습니다. 출력은 thisApt.getAptRentalDate()의 결과에 따라 다르지만 두 조건이 모두 반환됩니다.

그 이후의 모든 것은 도달 할 수없는 코드입니다. thisCustomer = findCustomer(theCustomerID);은 250 행에 있기 때문에 해당 메소드의 다른 모든 코드뿐만 아니라 도달 할 수 없습니다.