From 29021ae9b79d9be430b4a1969a2eefcfe5f8cb95 Mon Sep 17 00:00:00 2001 From: vibes-863 Date: Mon, 15 Apr 2024 19:46:24 +0800 Subject: [PATCH] Rename logger to LOGGER --- src/main/java/budgetbuddy/BudgetBuddy.java | 6 +-- .../java/budgetbuddy/account/Account.java | 10 ++-- .../budgetbuddy/account/AccountManager.java | 30 +++++------ .../java/budgetbuddy/storage/DataStorage.java | 52 +++++++++---------- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/main/java/budgetbuddy/BudgetBuddy.java b/src/main/java/budgetbuddy/BudgetBuddy.java index 1c6d6be23..667d1c186 100644 --- a/src/main/java/budgetbuddy/BudgetBuddy.java +++ b/src/main/java/budgetbuddy/BudgetBuddy.java @@ -39,7 +39,7 @@ public class BudgetBuddy { public static final String DELETE_ACC = "delete-acc"; public static final String EDIT_ACC = "edit-acc"; public static final String SEARCH = "search"; - public static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); + public static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); private final AccountManager accountManager; private final TransactionList transactions; @@ -57,7 +57,7 @@ public BudgetBuddy() { */ private static void setupLogger() { LogManager.getLogManager().reset(); - logger.setLevel(java.util.logging.Level.ALL); + LOGGER.setLevel(java.util.logging.Level.ALL); try { File logsDir = new File("logs"); if (!logsDir.exists()) { @@ -66,7 +66,7 @@ private static void setupLogger() { FileHandler fh = new FileHandler("logs/budgetBuggyLog.log"); fh.setFormatter(new SimpleFormatter()); fh.setLevel(Level.INFO); - logger.addHandler(fh); + LOGGER.addHandler(fh); } catch (IOException e) { UserInterface.printLoggerSetupError(); } diff --git a/src/main/java/budgetbuddy/account/Account.java b/src/main/java/budgetbuddy/account/Account.java index a8d2718b3..67a12832c 100644 --- a/src/main/java/budgetbuddy/account/Account.java +++ b/src/main/java/budgetbuddy/account/Account.java @@ -6,7 +6,7 @@ * Represents an account in the budget buddy system. */ public class Account { - public static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); + public static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); private final int accountNumber; private String name; private double balance; @@ -21,7 +21,7 @@ public Account(int accountNumber) { this.accountNumber = accountNumber; this.name = ""; this.balance = 0.00; - logger.info("Account created with default name and balance"); + LOGGER.info("Account created with default name and balance"); } /** @@ -37,7 +37,7 @@ public Account(int accountNumber, String name, double balance) { this.accountNumber = accountNumber; this.name = name; this.balance = balance; - logger.info("Account created with specified name and balance"); + LOGGER.info("Account created with specified name and balance"); } /** @@ -56,7 +56,7 @@ public double getBalance() { */ public void setBalance(double balance) { this.balance = balance; - logger.info("Account balance updated"); + LOGGER.info("Account balance updated"); } /** @@ -85,7 +85,7 @@ public String getName() { public void setName(String name) { assert name != null : "Name cannot be null"; this.name = name; - logger.info("Account name updated"); + LOGGER.info("Account name updated"); } /** diff --git a/src/main/java/budgetbuddy/account/AccountManager.java b/src/main/java/budgetbuddy/account/AccountManager.java index 9f65b79e3..826b457f0 100644 --- a/src/main/java/budgetbuddy/account/AccountManager.java +++ b/src/main/java/budgetbuddy/account/AccountManager.java @@ -19,7 +19,7 @@ */ public class AccountManager { public static final int INDEX_OFFSET = 1; - public static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); + public static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); private final DataStorage dataStorage = new DataStorage(); private final ArrayList accounts; private final ArrayList existingAccountNumbers; @@ -31,7 +31,7 @@ public class AccountManager { public AccountManager() { this.accounts = new ArrayList<>(); this.existingAccountNumbers = new ArrayList<>(); - logger.log(Level.INFO, "AccountManager created with empty account and account number lists"); + LOGGER.log(Level.INFO, "AccountManager created with empty account and account number lists"); } /** @@ -45,7 +45,7 @@ public AccountManager(ArrayList accounts, ArrayList existingAc assert existingAccountNumbers != null : "Existing account numbers list cannot be null"; this.accounts = accounts; this.existingAccountNumbers = existingAccountNumbers; - logger.log(Level.INFO, "AccountManager created with specified account and account number lists"); + LOGGER.log(Level.INFO, "AccountManager created with specified account and account number lists"); } /** @@ -59,7 +59,7 @@ public void addAccount(String name, double initialBalance) { int newAccountNumber = generateAccountNumber(); accounts.add(new Account(newAccountNumber, name, initialBalance)); existingAccountNumbers.add(newAccountNumber); - logger.log(Level.INFO, "Account added"); + LOGGER.log(Level.INFO, "Account added"); } /** @@ -76,13 +76,13 @@ public int generateAccountNumber() { for (int accountNumber : existingAccountNumbers) { if (accountNumber == fourDigitNumber) { noMatchFound = false; - logger.log(Level.WARNING, "Account number already exists. Generating new account number."); + LOGGER.log(Level.WARNING, "Account number already exists. Generating new account number."); break; } } } while (!noMatchFound); - logger.log(Level.INFO, "Account number generated"); + LOGGER.log(Level.INFO, "Account number generated"); return fourDigitNumber; } @@ -97,18 +97,18 @@ public int generateAccountNumber() { public void processAddAccount(String input) throws InvalidArgumentSyntaxException, NumberFormatException, EmptyArgumentException { assert input != null : "Input cannot be null"; - logger.log(Level.INFO, "Processing add account command"); + LOGGER.log(Level.INFO, "Processing add account command"); String[] arguments = {"/n/", "/$/"}; for (String argument : arguments) { if (!input.contains(argument)) { - logger.log(Level.WARNING, "Invalid add account syntax."); + LOGGER.log(Level.WARNING, "Invalid add account syntax."); throw new InvalidArgumentSyntaxException("Invalid add account syntax."); } } String[] parsedData = Parser.parseAddAccount(input); addAccount(parsedData[0], Double.parseDouble(parsedData[1])); UserInterface.printAddAccountMessage(getAccount(accounts.size() - INDEX_OFFSET).toString()); - logger.log(Level.INFO, "Account added successfully"); + LOGGER.log(Level.INFO, "Account added successfully"); } /** @@ -126,19 +126,19 @@ public void removeAccount(String input, TransactionList transactions) InvalidIndexException { assert input != null : "Input cannot be null"; assert transactions != null : "Transactions cannot be null"; - logger.log(Level.INFO, "Processing remove account command"); + LOGGER.log(Level.INFO, "Processing remove account command"); int accountNumber = Parser.parseRemoveAccount(input); Account accountRemoved = getAccountByAccountNumber(accountNumber); if (accounts.size() == 1) { UserInterface.printCannotDeleteLastAccountMessage(); - logger.log(Level.WARNING, "Cannot delete last account."); + LOGGER.log(Level.WARNING, "Cannot delete last account."); return; } accounts.remove(accountRemoved); existingAccountNumbers.remove(Integer.valueOf(accountNumber)); ArrayList transactionsRemoved = transactions.removeTransactionsByAccountNumber(accountNumber); UserInterface.printDeleteAccountMessage(accountRemoved.toString(), transactionsRemoved); - logger.log(Level.INFO, "Account removed successfully"); + LOGGER.log(Level.INFO, "Account removed successfully"); } /** @@ -166,7 +166,7 @@ public Account getAccountByAccountNumber(int accountNumber) { return account; } } - logger.log(Level.WARNING, "Account not found."); + LOGGER.log(Level.WARNING, "Account not found."); throw new IllegalArgumentException("Account not found."); } @@ -188,13 +188,13 @@ public ArrayList getAccounts() { */ public void processEditAccount(String input) throws EmptyArgumentException, IllegalArgumentException { assert input != null : "Input cannot be null"; - logger.log(Level.INFO, "Processing edit account command"); + LOGGER.log(Level.INFO, "Processing edit account command"); int accountNumber = Parser.parseEditAccount(input); Account account = getAccountByAccountNumber(accountNumber); String newName = UserInterface.getNewAccountName(account.toString()); account.setName(newName); UserInterface.printUpdatedAccount(account.toString()); - logger.log(Level.INFO, "Account edited successfully"); + LOGGER.log(Level.INFO, "Account edited successfully"); } /** diff --git a/src/main/java/budgetbuddy/storage/DataStorage.java b/src/main/java/budgetbuddy/storage/DataStorage.java index 9665b2b7a..6c4384a72 100644 --- a/src/main/java/budgetbuddy/storage/DataStorage.java +++ b/src/main/java/budgetbuddy/storage/DataStorage.java @@ -32,7 +32,7 @@ public class DataStorage { public static final String TRANSACTIONS_FILE_PATH = "./data/transactions.txt"; public static final String ACCOUNTS_FILE_PATH = "./data/accounts.txt"; public static final String FOLDER_PATH = "./data"; - public static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); + public static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); //@@author ShyamKrishna33 /** @@ -70,14 +70,14 @@ private static void createDataFolderIfNotExists() throws IOException { * @param accounts The list of accounts to save. */ public void saveAccounts(ArrayList accounts) { - logger.log(Level.INFO, "Saving accounts to file"); + LOGGER.log(Level.INFO, "Saving accounts to file"); try { File f = new File(ACCOUNTS_FILE_PATH); if (!f.exists()) { - logger.log(Level.WARNING, "File does not exist. Creating new file."); + LOGGER.log(Level.WARNING, "File does not exist. Creating new file."); createDataFolderIfNotExists(); if (!f.createNewFile()) { - logger.log(Level.SEVERE, "Failed to create file"); + LOGGER.log(Level.SEVERE, "Failed to create file"); throw new IOException("Failed to create file"); } } @@ -90,9 +90,9 @@ public void saveAccounts(ArrayList accounts) { fw.close(); } catch (IOException e) { System.out.println("Error saving accounts."); - logger.log(Level.SEVERE, "Error saving accounts"); + LOGGER.log(Level.SEVERE, "Error saving accounts"); } - logger.log(Level.INFO, "Accounts saved to file"); + LOGGER.log(Level.INFO, "Accounts saved to file"); } //@@author ShyamKrishna33 @@ -187,7 +187,7 @@ private Transaction parseDataToTransaction(String s, ArrayList existing */ public ArrayList readAccountFile(ArrayList existingAccountNumbers) throws IOException, FileCorruptedException { - logger.log(Level.INFO, "Reading accounts from file"); + LOGGER.log(Level.INFO, "Reading accounts from file"); File f = new File(ACCOUNTS_FILE_PATH); Scanner s = new Scanner(f); @@ -199,7 +199,7 @@ public ArrayList readAccountFile(ArrayList existingAccountNumb } accounts.add(processAccountLine(line, existingAccountNumbers)); } - logger.log(Level.INFO, "Accounts read from file"); + LOGGER.log(Level.INFO, "Accounts read from file"); return accounts; } @@ -213,18 +213,18 @@ public ArrayList readAccountFile(ArrayList existingAccountNumb */ private Account processAccountLine(String line, ArrayList existingAccountNumbers) throws FileCorruptedException { - logger.log(Level.INFO, "Processing account line"); + LOGGER.log(Level.INFO, "Processing account line"); String[] accountInfo = line.split(" ,"); validateAccountInfo(accountInfo, existingAccountNumbers); - logger.log(Level.INFO, "Account line processed"); + LOGGER.log(Level.INFO, "Account line processed"); int accountNumber = Integer.parseInt(accountInfo[0]); double balance = Double.parseDouble(accountInfo[2]); String accountName = accountInfo[1].trim(); existingAccountNumbers.add(accountNumber); - logger.log(Level.INFO, "Account added to existing account numbers list"); - logger.log(Level.INFO, "Account created"); + LOGGER.log(Level.INFO, "Account added to existing account numbers list"); + LOGGER.log(Level.INFO, "Account created"); return new Account(accountNumber, accountName, balance); } @@ -238,35 +238,35 @@ private Account processAccountLine(String line, ArrayList existingAccou private void validateAccountInfo(String[] accountInfo, ArrayList existingAccountNumbers) throws FileCorruptedException { if (accountInfo.length != 3) { - logger.log(Level.SEVERE, "Invalid account information format"); + LOGGER.log(Level.SEVERE, "Invalid account information format"); throw new FileCorruptedException("Invalid account information format"); } try { int accountNumber = Integer.parseInt(accountInfo[0]); if (accountNumber < 1000 || accountNumber > 9999) { - logger.log(Level.SEVERE, "Invalid account number"); + LOGGER.log(Level.SEVERE, "Invalid account number"); throw new FileCorruptedException("Invalid account number"); } if (existingAccountNumbers.contains(accountNumber)) { - logger.log(Level.SEVERE, "Duplicate account number"); + LOGGER.log(Level.SEVERE, "Duplicate account number"); throw new FileCorruptedException("Duplicate account number"); } } catch (NumberFormatException e) { - logger.log(Level.SEVERE, "Invalid type for account number"); + LOGGER.log(Level.SEVERE, "Invalid type for account number"); throw new FileCorruptedException("Invalid type for account number"); } try { double balance = Double.parseDouble(accountInfo[2]); } catch (NumberFormatException e) { - logger.log(Level.SEVERE, "Invalid type for account balance"); + LOGGER.log(Level.SEVERE, "Invalid type for account balance"); throw new FileCorruptedException("Invalid type for account balance"); } String accountName = accountInfo[1].trim(); if (accountName.isEmpty()) { - logger.log(Level.SEVERE, "Invalid account name"); + LOGGER.log(Level.SEVERE, "Invalid account name"); throw new FileCorruptedException("Invalid account name"); } } @@ -313,14 +313,14 @@ public ArrayList readTransactionFile(ArrayList existingAcc * @return The loaded AccountManager object. */ public AccountManager loadAccounts() { - logger.log(Level.INFO, "Loading accounts from file"); + LOGGER.log(Level.INFO, "Loading accounts from file"); try { File f = new File(ACCOUNTS_FILE_PATH); if (!f.exists()) { - logger.log(Level.WARNING, "File does not exist. Creating new file."); + LOGGER.log(Level.WARNING, "File does not exist. Creating new file."); createDataFolderIfNotExists(); if (!f.createNewFile()) { - logger.log(Level.SEVERE, "Failed to create file"); + LOGGER.log(Level.SEVERE, "Failed to create file"); throw new IOException("Failed to create file"); } return createNewAccountManager(); @@ -330,21 +330,21 @@ public AccountManager loadAccounts() { try { accounts = readAccountFile(existingAccountNumbers); } catch (FileCorruptedException e) { - logger.log(Level.SEVERE, "File corrupted"); + LOGGER.log(Level.SEVERE, "File corrupted"); UserInterface.printFileCorruptedError(); FileWriter fw = new FileWriter(ACCOUNTS_FILE_PATH, false); - logger.log(Level.WARNING, "Creating new account manager"); + LOGGER.log(Level.WARNING, "Creating new account manager"); return createNewAccountManager(); } if (accounts.isEmpty()) { - logger.log(Level.WARNING, "Creating new account manager"); + LOGGER.log(Level.WARNING, "Creating new account manager"); return createNewAccountManager(); } return new AccountManager(accounts, existingAccountNumbers); } catch (IOException e) { - logger.log(Level.SEVERE, "Error loading accounts"); + LOGGER.log(Level.SEVERE, "Error loading accounts"); UserInterface.printFileCorruptedError(); - logger.log(Level.WARNING, "Creating new account manager"); + LOGGER.log(Level.WARNING, "Creating new account manager"); return createNewAccountManager(); } }