Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename logger to LOGGER #167

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()) {
Expand All @@ -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();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/budgetbuddy/account/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

/**
Expand All @@ -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");
}

/**
Expand All @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/budgetbuddy/account/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Account> accounts;
private final ArrayList<Integer> existingAccountNumbers;
Expand All @@ -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");
}

/**
Expand All @@ -45,7 +45,7 @@ public AccountManager(ArrayList<Account> accounts, ArrayList<Integer> 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");
}

/**
Expand All @@ -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");
}

/**
Expand All @@ -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;
}

Expand All @@ -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");
}

/**
Expand All @@ -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<Transaction> transactionsRemoved = transactions.removeTransactionsByAccountNumber(accountNumber);
UserInterface.printDeleteAccountMessage(accountRemoved.toString(), transactionsRemoved);
logger.log(Level.INFO, "Account removed successfully");
LOGGER.log(Level.INFO, "Account removed successfully");
}

/**
Expand Down Expand Up @@ -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.");
}

Expand All @@ -188,13 +188,13 @@ public ArrayList<Account> 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");
}

/**
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/budgetbuddy/storage/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down Expand Up @@ -70,14 +70,14 @@ private static void createDataFolderIfNotExists() throws IOException {
* @param accounts The list of accounts to save.
*/
public void saveAccounts(ArrayList<Account> 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");
}
}
Expand All @@ -90,9 +90,9 @@ public void saveAccounts(ArrayList<Account> 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
Expand Down Expand Up @@ -187,7 +187,7 @@ private Transaction parseDataToTransaction(String s, ArrayList<Integer> existing
*/
public ArrayList<Account> readAccountFile(ArrayList<Integer> 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);

Expand All @@ -199,7 +199,7 @@ public ArrayList<Account> readAccountFile(ArrayList<Integer> existingAccountNumb
}
accounts.add(processAccountLine(line, existingAccountNumbers));
}
logger.log(Level.INFO, "Accounts read from file");
LOGGER.log(Level.INFO, "Accounts read from file");
return accounts;
}

Expand All @@ -213,18 +213,18 @@ public ArrayList<Account> readAccountFile(ArrayList<Integer> existingAccountNumb
*/
private Account processAccountLine(String line, ArrayList<Integer> 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);
}

Expand All @@ -238,35 +238,35 @@ private Account processAccountLine(String line, ArrayList<Integer> existingAccou
private void validateAccountInfo(String[] accountInfo, ArrayList<Integer> 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");
}
}
Expand Down Expand Up @@ -313,14 +313,14 @@ public ArrayList<Transaction> readTransactionFile(ArrayList<Integer> 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();
Expand All @@ -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();
}
}
Expand Down
Loading