Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,43 @@ public class BankAccount {
// - accountNumber (String)
// - balance (double)
// - ownerName (String)
private String accountNumber;
private double balance;
private String ownerName;


// TODO: 2 - Create a constructor that takes accountNumber, ownerName,
// and an initialBalance. Validate that initialBalance >= 0,
// throwing IllegalArgumentException if not. Assign all fields.

public BankAccount(String accountNumber, String ownerName, double initialBalance){
if(initialBalance <0) throw new IllegalArgumentException("Balance should be greater or equal to 0!");
this.accountNumber=accountNumber;
this.ownerName = ownerName;
this.balance = initialBalance;
}


// TODO: 3 - Create a getter method for balance (getBalance).
// Do NOT create a setter for balance — it should only change
// through deposit() and withdraw().

public double getBalance(){
return this.balance;
}

// TODO: 4 - Create a deposit(double amount) method.
// - If amount <= 0, throw IllegalArgumentException with message
// "Deposit amount must be positive"
// - Add amount to balance
// - Call the private logTransaction() helper with a descriptive message
// - Return the new balance
public double deposit(double amount){
if(amount<=0) throw new IllegalArgumentException("Deposit amount must be positive");
this.balance += amount;
logTransaction("$%s was added successfully to account: %s".formatted(amount, accountNumber));
return this.balance;

}

// TODO: 5 - Create a withdraw(double amount) method.
// - If amount <= 0, throw IllegalArgumentException with message
Expand All @@ -46,32 +64,50 @@ public class BankAccount {
// - Subtract amount from balance
// - Call the private logTransaction() helper with a descriptive message
// - Return the new balance
public double withdraw(double amount){
if(amount<=0) throw new IllegalArgumentException("Withdrawal amount must be positive");
if(amount > balance) throw new IllegalStateException("Insufficient funds");
this.balance -= amount;
logTransaction("$%s was removed successfully to account: %s".formatted(amount, accountNumber));

return this.balance;
}

// TODO: 6 - Override toString() to return a string in the format:
// "BankAccount{accountNumber='XXX', ownerName='XXX', balance=XXX}"


@Override
public String toString() {
return "BankAccount{" +
"accountNumber='" + accountNumber + '\'' +
", balance=" + balance +
", ownerName='" + ownerName + '\'' +
'}';
}

// TODO: 7 - Create a private helper method logTransaction(String message)
// that prints the message to the console prefixed with
// "[Transaction Log] ". This method should NOT be accessible
// from outside the class.

private void logTransaction(String message){
System.out.println("[Transaction Log]: %s".formatted(message));
}

public static void main(String[] args) {
// Uncomment and test after completing the TODOs:
// BankAccount account = new BankAccount("ACC-001", "Alice", 1000.0);
// System.out.println(account);
// System.out.println("Balance: " + account.getBalance());
//
// account.deposit(500.0);
// System.out.println("After deposit: " + account.getBalance());
//
// account.withdraw(200.0);
// System.out.println("After withdrawal: " + account.getBalance());
BankAccount account = new BankAccount("ACC-001", "Alice", 1000.0);
System.out.println(account);
System.out.println("Balance: " + account.getBalance());
//
account.deposit(500.0);
System.out.println("After deposit: " + account.getBalance());

account.withdraw(200.0);
System.out.println("After withdrawal: " + account.getBalance());

// // These should throw exceptions:
// // account.deposit(-100);
// // account.withdraw(999999);
//account.deposit(-100);
//account.withdraw(999999);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,77 @@ public final class ImmutablePerson {
// - name (String)
// - age (int)
// - email (String)
private final String name;
private final int age;
private final String email;


// TODO: 2 - Create a constructor that takes name, age, and email
// and assigns them to the fields. Since the fields are final,
// they can only be set here.

public ImmutablePerson(String name, int age, String email){
this.name = name;
this.age = age;
this.email = email;
}

// TODO: 3 - Create getter methods for all three fields:
// getName(), getAge(), getEmail().
// Do NOT create any setter methods — this class is immutable.
public String getName(){
return name;
}

public int getAge(){
return age;
}

public String getEmail() {
return email;
}

// TODO: 4 - Create a withName(String newName) method that returns
// a NEW ImmutablePerson with the changed name but the same
// age and email. The original object must remain unchanged.
public ImmutablePerson withName(String newName){
return new ImmutablePerson(newName, age, email);
}



// TODO: 5 - Create a withAge(int newAge) method that returns
// a NEW ImmutablePerson with the changed age but the same
// name and email. The original object must remain unchanged.

public ImmutablePerson withAge(int newAge){
return new ImmutablePerson(name, newAge, email);
}

// TODO: 6 - Override toString() to return a string in the format:
// "ImmutablePerson{name='XXX', age=XXX, email='XXX'}"


@Override
public String toString() {
return "ImmutablePerson{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}

public static void main(String[] args) {
// Uncomment and test after completing the TODOs:
// ImmutablePerson person = new ImmutablePerson("Alice", 30, "alice@example.com");
// System.out.println(person);
//
// // withName returns a NEW object — original is unchanged
// ImmutablePerson renamed = person.withName("Bob");
// System.out.println("Original: " + person);
// System.out.println("Renamed: " + renamed);
//
// // withAge returns a NEW object — original is unchanged
// ImmutablePerson aged = person.withAge(31);
// System.out.println("Original: " + person);
// System.out.println("Aged: " + aged);
ImmutablePerson person = new ImmutablePerson("Alice", 30, "alice@example.com");
System.out.println(person);

// withName returns a NEW object — original is unchanged
ImmutablePerson renamed = person.withName("Bob");
System.out.println("Original: " + person);
System.out.println("Renamed: " + renamed);

// withAge returns a NEW object — original is unchanged
ImmutablePerson aged = person.withAge(31);
System.out.println("Original: " + person);
System.out.println("Aged: " + aged);
}
}
31 changes: 24 additions & 7 deletions src/main/java/com/amigoscode/_3_oop/_2_inheritance/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,48 @@ public class Animal {
// TODO: 1 - Declare two protected fields:
// - name (String)
// - age (int)

protected String name;
protected int age;

// TODO: 2 - Create a constructor that takes name and age
// and assigns them to the fields.

public Animal(String name, int age){
this.name = name;
this.age = age;
}

// TODO: 3 - Create a makeSound() method that prints:
// "Some generic animal sound"
// Subclasses will override this with their specific sound.

public void makeSound(){
System.out.println("Some generic animal sound");
}

// TODO: 4 - Create an eat(String food) method that prints:
// "<name> is eating <food>"
// For example: "Buddy is eating kibble"
public void eat(String food){
String msg = "%s is eating %s".formatted(this.name, food);
System.out.println(msg);
}


// TODO: 5 - Override toString() to return:
// "Animal{name='XXX', age=XXX}"

@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

public static void main(String[] args) {
// Uncomment and test after completing the TODOs:
// Animal animal = new Animal("Generic", 5);
// System.out.println(animal);
// animal.makeSound();
// animal.eat("food");
Animal animal = new Animal("Generic", 5);
System.out.println(animal);
animal.makeSound();
animal.eat("food");
}
}
42 changes: 30 additions & 12 deletions src/main/java/com/amigoscode/_3_oop/_2_inheritance/Dog.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,61 @@

// TODO: 1 - Make this class extend Animal.
// Change the class declaration so Dog inherits from Animal.
public class Dog {
public class Dog extends Animal{

// TODO: 2 - Add a private field:
// - breed (String)
private String breed;


// TODO: 3 - Create a constructor that takes name, age, and breed.
// Call super(name, age) to initialize the parent fields,
// then set the breed field.

public Dog(String name, int age, String breed){
super(name, age);
this.breed = breed;
}

// TODO: 4 - Override the makeSound() method to print:
// "<name> says: Woof! Woof!"
// Use the @Override annotation. You can access `name` because
// it is a protected field in Animal.

@Override
public void makeSound(){
String sound = "%s says: Woof! Woof!".formatted(name);
System.out.println(sound);
}

// TODO: 5 - Add a fetch(String item) method specific to Dog.
// This method should print:
// "<name> fetches the <item>!"
// This method does not exist in Animal — it is unique to Dog.

public void fetch(String item){
String msg = "%s fetches the %s".formatted(name, item);
System.out.println(msg);
}

// TODO: 6 - Override toString() to return:
// "Dog{name='XXX', age=XXX, breed='XXX'}"

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", breed='" + breed + '\'' +
'}';
}

public static void main(String[] args) {
// Uncomment and test after completing the TODOs:
// Dog dog = new Dog("Buddy", 3, "Golden Retriever");
// System.out.println(dog);
// dog.makeSound();
// dog.eat("kibble"); // inherited from Animal
// dog.fetch("tennis ball"); // specific to Dog
Dog dog = new Dog("Buddy", 3, "Golden Retriever");
System.out.println(dog);
dog.makeSound();
dog.eat("kibble"); // inherited from Animal
dog.fetch("tennis ball"); // specific to Dog
//
// // Polymorphism: a Dog IS-AN Animal
// Animal animal = new Dog("Rex", 5, "German Shepherd");
// animal.makeSound(); // calls Dog's overridden version
Animal animal = new Dog("Rex", 5, "German Shepherd");
animal.makeSound(); // calls Dog's overridden version
}
}
Loading