Я немного реорганизовал ваш код (больше не должно быть множественного числа в имени класса, переименованных методах, закрытых полях, toString()
методе, удобном конструкторе, попробуйте ресурсы в аспекте), а также исправил аспект, используя target()
с привязкой аргументав аспекте:
Класс контактных данных:
package de.scrum_master.app;
import java.util.Scanner;
public class Contact {
private String name;
private String street;
private String city;
private String state;
private int zip;
private long phoneNumber;
private Scanner input;
public Contact() {}
public Contact(String name, String street, String city, String state, int zip, long phoneNumber) {
this.name = name;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "Contact[name=" + name + ", street=" + street + ", city=" + city + ", state=" + state +
", zip=" + zip + ", phoneNumber=" + phoneNumber + "]";
}
public void updateContact() {
input = new Scanner(System.in);
System.out.println("Please enter contact Name and Lastname: ");
name = input.nextLine();
System.out.println("\nPlease enter Street of contact: ");
street = input.nextLine();
System.out.println("\nPlease enter City of the contact: ");
city = input.nextLine();
System.out.println("\nPlease enter State of the contact: ");
state = input.nextLine();
System.out.println("\nPlease enter the Zipcode of the contact: ");
zip = input.nextInt();
System.out.println("\nPlease enter the contact Phone number (Ex 1115550000): ");
phoneNumber = input.nextLong();
System.out.println("Done! Contact updated.\n");
}
}
Класс фиктивного меню с основным методом:
package de.scrum_master.app;
public class Menu {
public void updateContact(Contact contact) {
contact.updateContact();
}
public static void main(String[] args) {
Menu menu = new Menu();
Contact contact = new Contact("Albert Einstein", "101 Main St", "Middle of Nowhere", "Utah", 12345, 11223344);
menu.updateContact(contact);
menu.updateContact(contact);
}
}
Как видите, я создаю исходный контактный объект, а затем обновляю его дважды, чтобы создать две строки вывода журнала.
Аспект ведения журнала:
package de.scrum_master.aspect;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import de.scrum_master.app.Contact;
import de.scrum_master.app.Menu;
public aspect UpdateContactAspect {
pointcut callUpdateContact(Contact contact) :
call(void updateContact()) && within(Menu) && target(contact);
before(Contact contact) : callUpdateContact(contact) {
try (PrintWriter writer = new PrintWriter(new FileWriter("UpdatesLog.txt", true))) {
writer.println("Record updated -> " + contact);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Журнал консоли для пробного запуска:
Please enter contact Name and Lastname:
John Doe
Please enter Street of contact:
321 Broadway
Please enter City of the contact:
New York City
Please enter State of the contact:
New York
Please enter the Zipcode of the contact:
54321
Please enter the contact Phone number (Ex 1115550000):
0123456789
Done! Contact updated.
Please enter contact Name and Lastname:
Donald Duck
Please enter Street of contact:
33 Wherever
Please enter City of the contact:
Quacktown
Please enter State of the contact:
Duckstate
Please enter the Zipcode of the contact:
88099
Please enter the contact Phone number (Ex 1115550000):
999333111
Done! Contact updated.
Содержимое файла журнала после пробного запуска:
Record updated -> Contact[name=Albert Einstein, street=101 Main St, city=Middle of Nowhere, state=Utah, zip=12345, phoneNumber=11223344]
Record updated -> Contact[name=John Doe, street=321 Broadway, city=New York City, state=New York, zip=54321, phoneNumber=123456789]
Есть и другие вещи, которыенужен рефакторинг, но я останавливаюсь здесь.