java BufferedReader and BufferedWriter

  Kiến thức lập trình

So I have this code that I’ve been trying to add java BufferedReader and BufferedWriter to it and have a text file but it’s not working.
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.LinkedList;

// Define the Food class

class Food {

private String name;

private String countryOfOrigin;

private String code;

private String additionalInfo;



// Constructor

public Food(String name, String countryOfOrigin, String code, String additionalInfo) {

    this.name = name;

    this.countryOfOrigin = countryOfOrigin;

    this.code = code;

    this.additionalInfo = additionalInfo;

}



// Getter methods

public String getName() {

    return name;

}



public String getCountryOfOrigin() {

    return countryOfOrigin;

}



public String getCode() {

    return code;

}



public String getAdditionalInfo() {

    return additionalInfo;

}

}

// Define the Administrator class

class Administrator {

private LinkedList<Food> foodCatalog;

private LinkedList<FoodRequest> foodRequests;



// Constructor

public Administrator() {

    foodCatalog = new LinkedList<>();

    foodRequests = new LinkedList<>();

}



// Method to add new food to the catalog

public void addFood(Food food) {

    foodCatalog.add(food);

    System.out.println("Food added successfully.");

}



// Method to delete information of a food item

public void deleteFood(String name) {

    for (Food food : foodCatalog) {

        if (food.getName().equalsIgnoreCase(name)) {

            foodCatalog.remove(food);

            System.out.println("Food deleted successfully.");

            return;

        }

    }

    System.out.println("Food with name " + name + " not found.");

}



// Method to search for a particular food variety by name

public Food searchFood(String name) {

    for (Food food : foodCatalog) {

        if (food.getName().equalsIgnoreCase(name)) {

            return food;

        }

    }

    return null;

}



// Method to check the number of food varieties

public int checkVarieties() {

    return foodCatalog.size();

}



// Method to generate reports based on different criteria

public void generateReports() {

    // Implement report generation logic

    System.out.println("Generating reports...");

}



// Method to review food requests from users

public void reviewFoodRequests() {

    System.out.println("Food Requests:");

    for (FoodRequest request : foodRequests) {

        System.out.println("Name: " + request.getName());

        System.out.println("Country of Origin: " + request.getCountryOfOrigin());

        System.out.println("Additional Info: " + request.getAdditionalInfo());

        System.out.println("Feedback: " + request.getFeedback());

        System.out.println();

    }

}



// Method to give feedback on user's requests

public void giveFeedback(String name, String feedback) {

    for (FoodRequest request : foodRequests) {

        if (request.getName().equalsIgnoreCase(name)) {

            request.setFeedback(feedback);

            System.out.println("Feedback given successfully.");

            return;

        }

    }

    System.out.println("Food request with name " + name + " not found.");

}



// Method to add a food request from the user

public void addFoodRequest(FoodRequest request) {

    foodRequests.add(request);

    System.out.println("Food request submitted successfully.");

}



// Method to access food catalog

public LinkedList<Food> getFoodCatalog() {

    return foodCatalog;

}

}

// Define the User class

class User {

// Method to inquire about specific food including information

public void inquireAboutFood(String name, Administrator admin) {

    Food food = admin.searchFood(name);

    if (food != null) {

        System.out.println("Food Name: " + food.getName());

        System.out.println("Country of Origin: " + food.getCountryOfOrigin());

        System.out.println("Code: " + food.getCode());

        System.out.println("Additional Info: " + food.getAdditionalInfo());

    } else {

        System.out.println("Food not found.");

    }

}



// Method to request for a new food item

public void requestNewFood(BufferedReader reader, Administrator admin) throws IOException {

    System.out.println("Enter food name:");

    String name = reader.readLine();

    System.out.println("Enter country of origin:");

    String countryOfOrigin = reader.readLine();

    System.out.println("Enter additional information:");

    String additionalInfo = reader.readLine();

    FoodRequest request = new FoodRequest(name, countryOfOrigin, additionalInfo);

    admin.addFoodRequest(request);

}



// Method to view all available food items

public void viewAllFoodItems(Administrator admin) {

    System.out.println("--------------------------------------------------");

    System.out.printf("%-20s | %-20s | %-20s | %-20s%n", "Name", "Country of Origin", "Code", "Additional Info");

    System.out.println("--------------------------------------------------");

    for (Food food : admin.getFoodCatalog()) {

        System.out.printf("%-20s | %-20s | %-20s | %-20s%n", food.getName(), food.getCountryOfOrigin(), food.getCode(), food.getAdditionalInfo());

    }

    System.out.println("--------------------------------------------------");

}



// Method to raise a complaint about non-availability of food

public void complain() {

    System.out.println("Complaint raised successfully.");

}

}

// Define the FoodRequest class

class FoodRequest {

private String name;

private String countryOfOrigin;

private String additionalInfo;

private String feedback;



// Constructor

public FoodRequest(String name, String countryOfOrigin, String additionalInfo) {

    this.name = name;

    this.countryOfOrigin = countryOfOrigin;

    this.additionalInfo = additionalInfo;

    this.feedback = "Pending";

}



// Getter and setter methods

public String getName() {

    return name;

}



public String getCountryOfOrigin() {

    return countryOfOrigin;

}



public String getAdditionalInfo() {

    return additionalInfo;

}



public String getFeedback() {

    return feedback;

}



public void setFeedback(String feedback) {

    this.feedback = feedback;

}

}

// Define the Main class

public class JavaApplication31 {

public static void main(String[] args) {

    Administrator admin = new Administrator();

    User user = new User();

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    
    



    try {

        while (true) {

            System.out.println("Please select the user:");

            System.out.println("1. Administrator");

            System.out.println("2. User");

            System.out.println("3. Exit");



            int choice = Integer.parseInt(reader.readLine());



            switch (choice) {

                case 1: // Administrator

                    adminMenu(reader, admin);

                    break;

                case 2: // User

                    userMenu(reader, user, admin);

                    break;

                case 3: // Exit

                    System.out.println("Exiting the program...");

                    return;

                default:

                    System.out.println("Invalid choice. Please try again.");

            }

        }

    } catch (IOException | NumberFormatException e) {

        System.out.println("Error reading input. Exiting...");

        e.printStackTrace();

    }

}



// Method to display and handle administrator menu

private static void adminMenu(BufferedReader reader, Administrator admin) throws IOException {

    while (true) {

        System.out.println("Administrator Menu:");

        System.out.println("1. Add new food and its information");

        System.out.println("2. Delete the information of the food");

        System.out.println("3. Search for a particular food variety by name");

        System.out.println("4. Check the number of food varieties");

        System.out.println("5. Generate the reports of the food");

        System.out.println("6. Review food requests from users");

        System.out.println("7. Give feedback on user's requests");



        int choice = Integer.parseInt(reader.readLine());



        switch (choice) {

            case 1: // Add new food

                System.out.println("Enter food name:");

                String name = reader.readLine();

                System.out.println("Enter country of origin:");

                String countryOfOrigin = reader.readLine();

                System.out.println("Enter additional information:");

                String additionalInfo = reader.readLine();

                Food newFood = new Food(name, countryOfOrigin, "", additionalInfo);

                admin.addFood(newFood);

                break;

            case 2: // Delete food

                System.out.println("Enter name of the food to delete:");

                String nameToDelete = reader.readLine();

                admin.deleteFood(nameToDelete);

                break;

            case 3: // Search food

                System.out.println("Enter name of the food to search:");

                String nameToSearch = reader.readLine();

                Food searchedFood = admin.searchFood(nameToSearch);

                if (searchedFood != null) {

                    System.out.println("Food found:");

                    System.out.println("Name: " + searchedFood.getName());

                    System.out.println("Country of Origin: " + searchedFood.getCountryOfOrigin());

                    System.out.println("Code: " + searchedFood.getCode());

                    System.out.println("Additional Info: " + searchedFood.getAdditionalInfo());

                } else {

                    System.out.println("Food not found.");

                }

                break;

            case 4: // Check varieties

                System.out.println("Number of food varieties: " + admin.checkVarieties());

                break;

            case 5: // Generate reports

                admin.generateReports();

                break;

            case 6: // Review food requests

                admin.reviewFoodRequests();

                break;

            case 7: // Give feedback on user's requests

                System.out.println("Enter name of the food request to give feedback:");

                String requestName = reader.readLine();

                System.out.println("Enter feedback:");

                String feedback = reader.readLine();

                admin.giveFeedback(requestName, feedback);

                break;

            default:

                System.out.println("Invalid choice. Please try again.");

        }



        System.out.println("Do you want to continue as Administrator? (yes/no)");

        String continueChoice = reader.readLine();

        if (!continueChoice.equalsIgnoreCase("yes")) {

            break;

        }

    }

}



// Method to display and handle user menu

private static void userMenu(BufferedReader reader, User user, Administrator admin) throws IOException {

    while (true) {

        System.out.println("User Menu:");

        System.out.println("1. Inquire about the specific food including the information");

        System.out.println("2. Request for a new item");

        System.out.println("3. View all available food items");

        System.out.println("4. Complain if any");



        int choice = Integer.parseInt(reader.readLine());



        switch (choice) {

            case 1: // Inquire about food

                System.out.println("Enter name of the food to inquire:");

                String nameToInquire = reader.readLine();

                user.inquireAboutFood(nameToInquire, admin);

                break;

            case 2: // Request new food

                user.requestNewFood(reader, admin);

                break;

            case 3: // View all available food items

                user.viewAllFoodItems(admin);

                break;

            case 4: // Raise a complaint

                user.complain();

                break;

            default:

                System.out.println("Invalid choice. Please try again.");

        }



        System.out.println("Do you want to continue as User? (yes/no)");

        String continueChoice = reader.readLine();

        if (!continueChoice.equalsIgnoreCase("yes")) {

            break;

        }

    }

}

}

I tried adding foodList.txt in correct directory and i expected that when I run the program then I can see the information in foodList.txt

New contributor

Zahra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT