当前位置: 代码迷 >> java >> Java 方法的传递
  详细解决方案

Java 方法的传递

热度:63   发布时间:2023-08-04 09:11:26.0

嗨,我的教授对我的代码说了这些
请帮帮我我没有得到他想让我用我的代码做什么

// The following code (mark by "-")can be replace by getEntryDetails(AddressBookEntry entry)
            // you can just pass "addent" to the method 
            // always try to reuse as much methods/codes as possible

请帮帮我我没有得到他想让我用我的代码做什么
这是一个包含 3 个课程的地址簿,这是我真正遇到问题的课程之一

import java.util.Scanner;
public class AddressBookApp {
    private static Scanner dataReader;
    private static AddressBook book;
        // TODO Address Book App

    public static void main(String[] args) {            
        book = new AddressBook(10);
        dataReader = new Scanner(System.in);

        boolean lContinue = true;
        while (lContinue) {
            switch (Character.toUpperCase(menu())) {
            case '1': addBookEntry(); break;                
            case '2': deleteEntry(); break;
            case '3': viewAllEntries(); break;
            case '4': editEntry(); break;
            case '5': searchEntryByName(); break; 
            case '6': searchEntryByRecord(); break;
            case 'X':
                lContinue = false;
                break;
            default:
                System.out.println("\nInvalid Menu option");
            }            
        }
        System.out.println("\nEnd of program...");
    }

    public static char menu() {
        char choice;
        System.out.println("\nAddressBook Menu");
        System.out.println("1. Add Entry");        
        System.out.println("2. Delete Entry");
        System.out.println("3. View all Entries");
        System.out.println("4. Update an Entry");
        System.out.println("5. Search Entry By Name");
        System.out.println("6. Search Entry By Record Number");
        System.out.println("X. Exit Program");
        System.out.print("\nSelect Menu Option: ");
        choice = dataReader.nextLine().charAt(0);
        return choice;
    }

    public static AddressBookEntry getEntryDetails(AddressBookEntry entry) {
        if( entry == null ) {
            entry = new AddressBookEntry();
        }
        System.out.print("\nName     : "); entry.setName(dataReader.nextLine());
        System.out.print("Address  : "); entry.setAddress(dataReader.nextLine());
        System.out.print("Phone No.: "); entry.setTelNo(dataReader.nextLine());
        System.out.print("Email    : "); entry.setEmailAdd(dataReader.nextLine());
        return entry;
    }

    public static void addBookEntry() {
        AddressBookEntry entry = getEntryDetails(null);
        if( entry != null ) {
            book.addAddressBookEntry(entry);
        }
    }

    public static void editEntry() {
        Scanner datainput = new Scanner(System.in);
        System.out.println("Enter record number: ");
        int recnumb = datainput.nextInt();

        AddressBookEntry addent = new AddressBookEntry();
        addent = book.findAddressBookEntryByRecordNo(recnumb);

        // The following code (mark by "-")can be replace by getEntryDetails(AddressBookEntry entry)
        // you can just pass "addent" to the method 
        // always try to reuse as much methods/codes as possible

        getEntryDetails(AddressBookEntry)
        - System.out.println("Name: " + addent.getName());

        - System.out.println("Edit Name: ");
        - String name = datainput.next();
        - addent.setName(name);

        - System.out.println("Edit Address: ");
        - String address = datainput.next();
        - addent.setAddress(address);

        - System.out.println("Edit EmailAdd: ");
        - String emailAdd = datainput.next();
        - addent.setEmailAdd(emailAdd);

        - System.out.println("Edit TelNo: ");
        - String telNo = datainput.next();
        - addent.setTelNo(telNo);

        displayEntry(addent, recnumb);

        // TODO: edit a single record entry
        //System.out.println("\nUnder construction....");
    }

    public static void searchEntryByRecord() {

        try {
            Scanner datainput = new Scanner(System.in);
            System.out.println("Enter record number: ");
            int recnumb = datainput.nextInt();

            AddressBookEntry addent = new AddressBookEntry();
            addent = book.findAddressBookEntryByRecordNo(recnumb);

            System.out.println("Name: " + addent.getName());
            System.out.println("Address:" + addent.getAddress());
        } catch (Exception NullPointerException) {
            // TODO Auto-generated catch block
            return;
        }


        // TODO: search an entry using its record no.
        // display "record not found" if such record does not exist.
        // Display all its entry. 
        // Hint: use the method "findAddressBookEntryByRecordNo()" 
        //       from the AddressBook class
        //System.out.println("\nUnder construction....");
    }


    public static void deleteEntry() {
        Scanner datainput = new Scanner(System.in);

        System.out.println("Enter record number to delete: ");
        int recnumb = datainput.nextInt();

        if (book.deleteAddressBookEntry(recnumb)) {
            System.out.println("Deleted successfully.");
        } else {
            System.out.println("Record not found");
        }

            }

        // TODO: delete an entry using its record no.
        // display "record not found" if such record does not exist.         
        // Hint: use the method "deleteAddressBookEntry()" 
        //       from the AddressBook class


    // display a single record
    public static void displayEntry(AddressBookEntry entry, int recNo) {        
        System.out.println("\nRecord No. " + recNo);
        System.out.println("Name     : " + entry.getName());
        System.out.println("Address  : " + entry.getAddress());
        System.out.println("Phone No.: " + entry.getTelNo());
        System.out.println("Email    : " + entry.getEmailAdd());
    }

    // Search all entries containing name search criteria
    public static void searchEntryByName() {       
        System.out.print("\nSearch[Name]: "); 
        // ensure no extraneous space and search criteria all in lowercase 
        String name = dataReader.nextLine().trim().toLowerCase();   

        // get a reference to the Addressbook list
        AddressBookEntry[] list = book.getAllEntries();             
        for( int i = 0; i < list.length; i++ ) {   
            // compare search criteria with every entry 
            if(list[i]!=null && list[i].getName().toLowerCase().contains(name)) {
                displayEntry(list[i],i+1); 
            }
        }
        System.out.println("No more records follow...");
    }

    public static void viewAllEntries() {
        int validRecords = 0;

        // get a reference to the Addressbook list
        AddressBookEntry[] list = book.getAllEntries();
        if( list.length == 0) {
            System.out.println("\nList empty...");
        }

        for( int i = 0; i < list.length; i++ ) {
            if( list[i] != null ) {
                displayEntry(list[i],++validRecords);                
            }    
        }
        System.out.println("No more entries to follow...");
    }
}

删除所有以“-”开头的行并改用它:

 getEntryDetails( addent );
  相关解决方案