// Zorah Fung, CSE 142 // This program reads an address book and asks the user for a domain name to spam. // If an email with the domain name is found in the address book, the user is asked // whether the person's name and email should be added to the "spam list" import java.util.*; // For Scanner import java.io.*; // For Files /* Each line of the file is guaranteed to start with the last name and then the first name followed by an arbitrary number of tokens .... Ex: Fung Zorah CSE 446 Paul G. Allen Center zorahtheexplorer@gmail.com zorahf@cs.washington.edu */ public class Spammer { public static void main(String[] args) throws FileNotFoundException { Scanner fileScan = new Scanner(new File("address_book.txt")); Scanner console = new Scanner(System.in); PrintStream spamList = new PrintStream(new File("spam_list.txt")); System.out.print("Email domain to spam? "); String domain = console.nextLine(); while (fileScan.hasNextLine()) { // Read a single contact String line = fileScan.nextLine(); readContact(line, domain, console, spamList); } } // Processing a contact on the given line, looking for an email with the given domain. // If an email with the domain is found, the user is asked whether they want the first name, // last name and email printed to the given PrintStream public static void readContact(String line, String domain, Scanner console, PrintStream spamList) { Scanner tokens = new Scanner(line); // Each line guaranteed to start with String lastName = tokens.next(); String firstName = tokens.next(); while (tokens.hasNext()) { String token = tokens.next(); if (token.endsWith(domain)) { // Found an email! System.out.print("Would you like to spam " + token +"? "); String response = console.nextLine(); if (response.toLowerCase().startsWith("y")) { spamList.println(firstName + " " + lastName + " <" + token + ">"); } } } } }