Practice Day: Create a Book, Car, or BankAccount class

Practice OOP in Java by creating classes like Book, Car, or BankAccount with constructors, getters, setters, and real-world logic.
Practice Day: Create a Book, Car, or BankAccount class

On Day 21, we will practice what we have learned so far (classes, fields, constructors, access modifiers, getters, and setters) by building simple classes. Choose one or try all three: Book, Car, or BankAccount.

1. Book Class Example


class Book {
    private String title;
    private String author;
    private double price;

    // constructor
    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    // getters
    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public double getPrice() { return price; }

    // setter with validation
    public void setPrice(double price) {
        if (price > 0) {
            this.price = price;
        } else {
            System.out.println("Invalid price!");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Book b1 = new Book("Java Basics", "John Doe", 500.0);
        System.out.println(b1.getTitle() + " by " + b1.getAuthor() + " - $" + b1.getPrice());

        b1.setPrice(-100); // invalid
        b1.setPrice(450.0); // valid
        System.out.println("Updated Price: $" + b1.getPrice());
    }
}

2. Car Class Example


class Car {
    private String brand;
    private String model;
    private int year;

    // constructor
    public Car(String brand, String model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }

    // getters
    public String getBrand() { return brand; }
    public String getModel() { return model; }
    public int getYear() { return year; }

    // display method
    public void displayInfo() {
        System.out.println(year + " " + brand + " " + model);
    }
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car("Toyota", "Corolla", 2022);
        c1.displayInfo();
    }
}

3. BankAccount Class Example


class BankAccount {
    private String accountNumber;
    private double balance;

    // constructor
    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    // deposit method
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Deposited: " + amount);
        } else {
            System.out.println("Invalid deposit amount!");
        }
    }

    // withdraw method
    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            System.out.println("Withdrawn: " + amount);
        } else {
            System.out.println("Invalid withdrawal!");
        }
    }

    // getter for balance
    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount("12345", 1000);
        acc.deposit(500);
        acc.withdraw(300);
        System.out.println("Balance: " + acc.getBalance());
    }
}

Summary

  • Book shows encapsulation and validation using setters.
  • Car demonstrates constructors and a custom display method.
  • BankAccount introduces real-world logic like deposit/withdraw with checks.

Practice creating your own classes using constructors, fields, getters, setters, and validation.

إرسال تعليق