|
| 1 | +import java.io.*; |
| 2 | +import java.net.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class MotivationalQuote { |
| 6 | + |
| 7 | + private static final String QUOTE_FILE = "quotes.txt"; |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + Scanner scanner = new Scanner(System.in); |
| 11 | + System.out.println("Choose an option:"); |
| 12 | + System.out.println("1. Show random local quote"); |
| 13 | + System.out.println("2. Fetch quote from API"); |
| 14 | + System.out.print("Enter choice: "); |
| 15 | + |
| 16 | + int choice = scanner.nextInt(); |
| 17 | + scanner.close(); |
| 18 | + |
| 19 | + switch (choice) { |
| 20 | + case 1: |
| 21 | + showRandomLocalQuote(); |
| 22 | + break; |
| 23 | + case 2: |
| 24 | + fetchQuoteFromAPI(); |
| 25 | + break; |
| 26 | + default: |
| 27 | + System.out.println("Invalid choice!"); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Option 1: Read from local file |
| 32 | + private static void showRandomLocalQuote() { |
| 33 | + List<String> quotes = new ArrayList<>(); |
| 34 | + try (BufferedReader br = new BufferedReader(new FileReader(QUOTE_FILE))) { |
| 35 | + String line; |
| 36 | + while ((line = br.readLine()) != null) { |
| 37 | + if (!line.trim().isEmpty()) { |
| 38 | + quotes.add(line.trim()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (quotes.isEmpty()) { |
| 43 | + System.out.println("No quotes found in file!"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + Random random = new Random(); |
| 48 | + int index = random.nextInt(quotes.size()); |
| 49 | + System.out.println("\n💬 Random Quote:\n" + quotes.get(index)); |
| 50 | + |
| 51 | + } catch (IOException e) { |
| 52 | + System.out.println("Error reading quotes file: " + e.getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Option 2: Fetch from public API |
| 57 | + private static void fetchQuoteFromAPI() { |
| 58 | + try { |
| 59 | + URL url = new URL("https://api.quotable.io/random"); |
| 60 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 61 | + conn.setRequestMethod("GET"); |
| 62 | + |
| 63 | + if (conn.getResponseCode() == 200) { |
| 64 | + BufferedReader in = new BufferedReader( |
| 65 | + new InputStreamReader(conn.getInputStream())); |
| 66 | + StringBuilder response = new StringBuilder(); |
| 67 | + String line; |
| 68 | + |
| 69 | + while ((line = in.readLine()) != null) { |
| 70 | + response.append(line); |
| 71 | + } |
| 72 | + in.close(); |
| 73 | + |
| 74 | + // Simple extraction of the "content" field |
| 75 | + String json = response.toString(); |
| 76 | + String quote = json.split("\"content\":\"")[1].split("\",")[0]; |
| 77 | + System.out.println("\n🌐 Fetched Quote:\n" + quote); |
| 78 | + } else { |
| 79 | + System.out.println("Failed to fetch quote from API."); |
| 80 | + } |
| 81 | + |
| 82 | + } catch (Exception e) { |
| 83 | + System.out.println("Error fetching quote: " + e.getMessage()); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments