Skip to content

Week 4: Java – Advanced OOP & String Manipulation


Q1. Bank Account System with Operations

Objective: Create a class Account with methods to perform credit, debit, and transfer operations.

import java.util.Scanner;

class Account {
    private String id;
    private String name;
    private int balance = 0;

    // Constructors
    public Account(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public Account(String id, String name, int balance) {
        this.id = id;
        this.name = name;
        this.balance = balance;
    }

    // Getters
    public String getId() { return id; }
    public String getName() { return name; }
    public int getBalance() { return balance; }

    // Methods
    public int credit(int amount) {
        balance += amount;
        return balance;
    }

    public int debit(int amount) {
        if (amount <= balance) balance -= amount;
        else System.out.println("Amount exceeded balance");
        return balance;
    }

    public int transferTo(Account another, int amount) {
        if (amount <= balance) {
            balance -= amount;
            another.balance += amount;
        } else System.out.println("Amount exceeded balance");
        return balance;
    }

    @Override
    public String toString() {
        return "Account [id=" + id + ", name=" + name + ", balance=" + balance + "]";
    }
}

public class P1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Account acc1 = new Account("A001", "Alice", 5000);
        Account acc2 = new Account("A002", "Bob", 2000);
        int choice;

        do {
            System.out.println("1: Show Accounts\n2: Credit\n3: Debit\n4: Transfer\n5: Exit\nEnter your choice: ");
            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    System.out.println(acc1);
                    System.out.println(acc2);
                    break;
                case 2:
                    System.out.print("Enter account ID: ");
                    String cid = sc.next();
                    System.out.print("Enter amount: ");
                    int camt = sc.nextInt();
                    if (cid.equals("A001")) acc1.credit(camt);
                    else if (cid.equals("A002")) acc2.credit(camt);
                    else System.out.println("Invalid Account ID!");
                    break;
                case 3:
                    System.out.print("Enter account ID: ");
                    String did = sc.next();
                    System.out.print("Enter amount: ");
                    int damt = sc.nextInt();
                    if (did.equals("A001")) acc1.debit(damt);
                    else if (did.equals("A002")) acc2.debit(damt);
                    else System.out.println("Invalid Account ID!");
                    break;
                case 4:
                    System.out.print("Source account ID: ");
                    String sid = sc.next();
                    System.out.print("Destination account ID: ");
                    String did2 = sc.next();
                    System.out.print("Amount: ");
                    int tamt = sc.nextInt();
                    if (sid.equals("A001") && did2.equals("A002")) acc1.transferTo(acc2, tamt);
                    else if (sid.equals("A002") && did2.equals("A001")) acc2.transferTo(acc1, tamt);
                    else System.out.println("Invalid account IDs!");
                    break;
                case 5:
                    System.out.println("Exiting... Thank you!");
                    break;
                default:
                    System.out.println("Invalid choice! Try again.");
            }
        } while (choice != 5);

        sc.close();
    }
}

Key Points:

  • Constructors with and without balance initialization.
  • credit, debit, and transferTo modify balances safely.
  • toString() overridden for easy account display.
  • User interacts via menu-driven program.

Q2. Advanced Bank Account with Input Validation

Objective: Same as above but with robust input checking.

Highlights:

  • Uses readPositiveDouble() and readInt() methods to validate input.
  • Ensures no negative balances or invalid amounts.
  • Menu-driven system for credit, debit, transfer, and display of accounts.

Q3. String Concatenation by Trimming

Objective: Concatenate two strings after trimming them to the length of the shorter string.

import java.util.Scanner;

public class StringConcatenation {
    public static String concatTrimmed(String a, String b) {
        int minLen = Math.min(a.length(), b.length());
        String aTrimmed = a.length() > minLen ? a.substring(a.length() - minLen) : a;
        String bTrimmed = b.length() > minLen ? b.substring(b.length() - minLen) : b;
        return aTrimmed + bTrimmed;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first string: ");
        String first = scanner.nextLine();
        System.out.print("Enter second string: ");
        String second = scanner.nextLine();

        String result = concatTrimmed(first, second);
        System.out.println("Result: " + result);
        scanner.close();
    }
}

Key Points:

  • Uses Math.min() to find the shorter string length.
  • substring() extracts the last minLen characters of each string.
  • Concatenates trimmed strings and prints result.

Week 4 Notes:

  1. Multiple constructors allow flexibility in object creation.
  2. Encapsulation: Use private fields and public methods to modify/access data.
  3. Input validation prevents invalid operations (negative balance, overdraft).
  4. String manipulation: substring, Math.min, and concatenation.

Comments