├── codsoft.java ├── currency converter.java └── student management.java /codsoft.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class BankAccount { 4 | private double balance; 5 | 6 | public BankAccount(double initialBalance) { 7 | this.balance = initialBalance; 8 | } 9 | 10 | public double getBalance() { 11 | return balance; 12 | } 13 | 14 | public void deposit(double amount) { 15 | if (amount > 0) { 16 | balance += amount; 17 | System.out.println("Deposit successful. New balance: $" + balance); 18 | } else { 19 | System.out.println("Invalid deposit amount."); 20 | } 21 | } 22 | 23 | public void withdraw(double amount) { 24 | if (amount > 0 && amount <= balance) { 25 | balance -= amount; 26 | System.out.println("Withdrawal successful. New balance: $" + balance); 27 | } else { 28 | System.out.println("Invalid withdrawal amount or insufficient funds."); 29 | } 30 | } 31 | } 32 | 33 | class ATM { 34 | private BankAccount bankAccount; 35 | 36 | public ATM(BankAccount bankAccount) { 37 | this.bankAccount = bankAccount; 38 | } 39 | 40 | public void displayMenu() { 41 | System.out.println("1. Withdraw"); 42 | System.out.println("2. Deposit"); 43 | System.out.println("3. Check Balance"); 44 | System.out.println("4. Exit"); 45 | } 46 | 47 | public void processOption(int option) { 48 | Scanner scanner = new Scanner(System.in); 49 | 50 | switch (option) { 51 | case 1: 52 | System.out.print("Enter withdrawal amount: $"); 53 | double withdrawAmount = scanner.nextDouble(); 54 | bankAccount.withdraw(withdrawAmount); 55 | break; 56 | case 2: 57 | System.out.print("Enter deposit amount: $"); 58 | double depositAmount = scanner.nextDouble(); 59 | bankAccount.deposit(depositAmount); 60 | break; 61 | case 3: 62 | System.out.println("Current balance: $" + bankAccount.getBalance()); 63 | break; 64 | case 4: 65 | System.out.println("Exiting ATM. Thank you!"); 66 | System.exit(0); 67 | break; 68 | default: 69 | System.out.println("Invalid option. Please try again."); 70 | } 71 | } 72 | } 73 | 74 | public class Main { 75 | public static void main(String[] args) { 76 | BankAccount userAccount = new BankAccount(1000.0); // Initial balance of $1000 77 | ATM atm = new ATM(userAccount); 78 | 79 | while (true) { 80 | atm.displayMenu(); 81 | 82 | Scanner scanner = new Scanner(System.in); 83 | System.out.print("Enter your choice: "); 84 | int option = scanner.nextInt(); 85 | 86 | atm.processOption(option); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /currency converter.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | import java.net.HttpURLConnection; 5 | import java.net.URL; 6 | 7 | public class CurrencyConverter { 8 | private static final String API_KEY = "YOUR_EXCHANGE_RATE_API_KEY"; 9 | private static final String API_URL = "https://open.er-api.com/v6/latest/"; 10 | 11 | public static void main(String[] args) { 12 | try { 13 | // Get the exchange rates 14 | String baseCurrency = getUserInput("Enter the base currency code (e.g., USD): "); 15 | String targetCurrency = getUserInput("Enter the target currency code (e.g., EUR): "); 16 | double amount = Double.parseDouble(getUserInput("Enter the amount to convert: ")); 17 | 18 | String exchangeRatesJson = getExchangeRates(baseCurrency); 19 | double conversionRate = extractConversionRate(exchangeRatesJson, targetCurrency); 20 | 21 | // Perform the conversion 22 | double convertedAmount = amount * conversionRate; 23 | 24 | // Display the result 25 | System.out.printf("%.2f %s is equal to %.2f %s%n", 26 | amount, baseCurrency, convertedAmount, targetCurrency); 27 | 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | private static String getUserInput(String prompt) throws IOException { 34 | System.out.print(prompt); 35 | BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 36 | return reader.readLine(); 37 | } 38 | 39 | private static String getExchangeRates(String baseCurrency) throws IOException { 40 | URL url = new URL(API_URL + baseCurrency + "?apikey=" + API_KEY); 41 | 42 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 43 | connection.setRequestMethod("GET"); 44 | 45 | BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 46 | StringBuilder response = new StringBuilder(); 47 | String line; 48 | 49 | while ((line = reader.readLine()) != null) { 50 | response.append(line); 51 | } 52 | 53 | reader.close(); 54 | connection.disconnect(); 55 | 56 | return response.toString(); 57 | } 58 | 59 | private static double extractConversionRate(String exchangeRatesJson, String targetCurrency) { 60 | // Parse the JSON and extract the conversion rate 61 | // You may use a JSON library like Jackson or Gson for more complex JSON parsing 62 | // For simplicity, we'll use a simple substring search here 63 | String rateStart = "\"" + targetCurrency + "\":"; 64 | int startIndex = exchangeRatesJson.indexOf(rateStart) + rateStart.length(); 65 | int endIndex = exchangeRatesJson.indexOf(",", startIndex); 66 | 67 | return Double.parseDouble(exchangeRatesJson.substring(startIndex, endIndex)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /student management.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | class Student { 5 | private String name; 6 | private int rollNumber; 7 | private String grade; 8 | 9 | public Student(String name, int rollNumber, String grade) { 10 | this.name = name; 11 | this.rollNumber = rollNumber; 12 | this.grade = grade; 13 | } 14 | 15 | public String getName() { 16 | return name; 17 | } 18 | 19 | public int getRollNumber() { 20 | return rollNumber; 21 | } 22 | 23 | public String getGrade() { 24 | return grade; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return "Student{" + 30 | "name='" + name + '\'' + 31 | ", rollNumber=" + rollNumber + 32 | ", grade='" + grade + '\'' + 33 | '}'; 34 | } 35 | } 36 | 37 | class StudentManagementSystem { 38 | private ArrayList students = new ArrayList<>(); 39 | 40 | public void addStudent(Student student) { 41 | students.add(student); 42 | System.out.println("Student added successfully."); 43 | } 44 | 45 | public void removeStudent(int rollNumber) { 46 | students.removeIf(student -> student.getRollNumber() == rollNumber); 47 | System.out.println("Student removed successfully."); 48 | } 49 | 50 | public Student searchStudent(int rollNumber) { 51 | for (Student student : students) { 52 | if (student.getRollNumber() == rollNumber) { 53 | return student; 54 | } 55 | } 56 | return null; 57 | } 58 | 59 | public void displayAllStudents() { 60 | for (Student student : students) { 61 | System.out.println(student); 62 | } 63 | } 64 | } 65 | 66 | public class StudentManagementSystemApp { 67 | public static void main(String[] args) { 68 | StudentManagementSystem system = new StudentManagementSystem(); 69 | Scanner scanner = new Scanner(System.in); 70 | 71 | while (true) { 72 | System.out.println("1. Add Student"); 73 | System.out.println("2. Remove Student"); 74 | System.out.println("3. Search Student"); 75 | System.out.println("4. Display All Students"); 76 | System.out.println("5. Exit"); 77 | 78 | System.out.print("Enter your choice: "); 79 | int choice = scanner.nextInt(); 80 | scanner.nextLine(); // Consume the newline character 81 | 82 | switch (choice) { 83 | case 1: 84 | System.out.print("Enter student name: "); 85 | String name = scanner.nextLine(); 86 | System.out.print("Enter roll number: "); 87 | int rollNumber = scanner.nextInt(); 88 | scanner.nextLine(); // Consume the newline character 89 | System.out.print("Enter grade: "); 90 | String grade = scanner.nextLine(); 91 | 92 | Student newStudent = new Student(name, rollNumber, grade); 93 | system.addStudent(newStudent); 94 | break; 95 | case 2: 96 | System.out.print("Enter roll number to remove: "); 97 | int removeRollNumber = scanner.nextInt(); 98 | system.removeStudent(removeRollNumber); 99 | break; 100 | case 3: 101 | System.out.print("Enter roll number to search: "); 102 | int searchRollNumber = scanner.nextInt(); 103 | Student foundStudent = system.searchStudent(searchRollNumber); 104 | if (foundStudent != null) { 105 | System.out.println("Student found: " + foundStudent); 106 | } else { 107 | System.out.println("Student not found."); 108 | } 109 | break; 110 | case 4: 111 | system.displayAllStudents(); 112 | break; 113 | case 5: 114 | System.out.println("Exiting the Student Management System. Goodbye!"); 115 | System.exit(0); 116 | break; 117 | default: 118 | System.out.println("Invalid choice. Please try again."); 119 | } 120 | } 121 | } 122 | } 123 | --------------------------------------------------------------------------------