NioCodeV01
NioCodeV02
NioCodeV03
NioCodeV04
NioCodeV05
NioCodeV06
// Bad Example
let status;
if(user.isAuthenticated){
status = user.getUserStatus();
} else {
status = UserStatus.NotAuthenticated;
}
// Good Example
const status = user.isAuthenticated ?
user.getUserStatus() :
UserStatus.NotAuthenticated;
NioCodeM01
NioCodeM02
NioCodeM03
class SaleProcessor {
constructor(price, taxRate, discount) {
this.price = price;
this.taxRate = taxRate;
this.discount = discount;
}
// Calculate tax based on the price and tax rate
calculateTax() {
return this.price * this.taxRate / 100;
}
// Process discount based on the original price and discount rate
processDiscount() {
return this.price * this.discount / 100;
}
// Finalize sale by calculating the final price after tax and discount
finalizeSale() {
const taxAmount = this.calculateTax();
const discountAmount = this.processDiscount();
const finalPrice = this.price + taxAmount - discountAmount;
console.log(`The final sale price is: ${finalPrice}`);
return finalPrice;
}
}
// Example usage
const sale = new SaleProcessor(100, 10, 5); // price = 100, taxRate = 10%, discount = 5%
sale.finalizeSale(); // Outputs: The final sale price is: 105
NioCodeM04
find() or select() for database operations.NioCodeM05
NioCodeM06
NioCodeM07
// A function to compute the total price of items without modifying the input list
function computeTotal(items) {
return items.reduce((total, item) => total + item.price, 0);
}
// Example usage
const items = [
{ name: 'Apple', price: 1.2 },
{ name: 'Banana', price: 0.8 },
{ name: 'Orange', price: 1.5 },
];
const total = computeTotal(items);
console.log(`The total price is: ${total}`);
NioCodeM08
function processInput(input) {
try {
// Validate input
if (typeof input !== 'number') {
throw new TypeError('Input must be a number');
}
// Process input (as an example, let's square the input)
const result = input * input;
console.log(`Processed result: ${result}`);
return result;
} catch (error) {
console.error(`Error processing input: ${error.message}`);
// Return a default value or rethrow the error, depending on your error handling strategy
return null; // Example of returning a default value
}
}
// Example usage
processInput(10); // Should log: Processed result: 100
processInput('a'); // Should log: Error processing input: Input must be a number
NioCodeM09
public class AccountManager {
// Public method - accessible from outside the class
public void addAccount(String username, String password) {
// Logic to add a new account
logAccountCreation(username); // Internal use only
}
// Private method - restricted to access within the class itself
private void logAccountCreation(String username) {
System.out.println("Account created for: " + username);
}
}
// Example usage
public class Main {
public static void main(String[] args) {
AccountManager manager = new AccountManager();
manager.addAccount("john_doe", "securepassword123");
// manager.logAccountCreation("john_doe"); // This would result in a compile-time error
}
}
NioCodeM10
void.public class Operation {
// Method returns a boolean indicating the success of the operation
public boolean performOperation(int a, int b) {
try {
// Simulate an operation (e.g., writing to a file, processing data)
int result = a + b; // Example operation
System.out.println("Operation performed successfully. Result: " + result);
return true; // Indicate success
} catch (Exception e) {
System.err.println("Operation failed: " + e.getMessage());
return false; // Indicate failure
}
}
}
// Example usage
public class Main {
public static void main(String[] args) {
Operation operation = new Operation();
boolean isSuccess = operation.performOperation(5, 3);
if (isSuccess) {
System.out.println("Operation was successful.");
} else {
System.out.println("Operation failed.");
}
}
}