Skip to content

Commit

Permalink
study java io
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Mar 5, 2021
1 parent 53681de commit 5248fef
Show file tree
Hide file tree
Showing 8 changed files with 17,531 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.banking.application;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.example.banking.domain.Account;
import com.example.banking.domain.CheckingAccount;
import com.example.banking.domain.Customer;

public class ReadCustomerFromFile {

public static void main(String[] args) throws Exception {
var file = new File("c:/tmp", "jack.dat");
// try-with-resources, Java 7+
try (var fis = new FileInputStream(file);
var ois = new ObjectInputStream(fis);) {
Customer jack = (Customer) ois.readObject();
System.err.println(jack);
var totalBalance = jack.getTotalBalance();
System.err.println("Total balance: "+totalBalance);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.banking.application;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import com.example.banking.domain.Account;
import com.example.banking.domain.CheckingAccount;
import com.example.banking.domain.Customer;

public class WriteCustomerToFile {

public static void main(String[] args) throws Exception {
var jack = new Customer("1111111110", "Jack Bauer");
jack.addAccount(new Account("tr1", 100_000));
jack.addAccount(new CheckingAccount("tr2", 200_000, 5_000));
jack.addAccount(new Account("tr3", 300_000));
var file = new File("c:/tmp", "jack.dat");
// try-with-resources, Java 7+
try (var fos = new FileOutputStream(file);
var oos = new ObjectOutputStream(fos);) {
oos.writeObject(jack);
}
System.err.println("Object is written to the file.");
}

}
8 changes: 7 additions & 1 deletion core-banking/src/com/example/banking/domain/Account.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.example.banking.domain;

import java.io.Serializable;

// Encapsulation: fields + methods
public class Account extends Object {
public class Account extends Object implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
// private : information hiding principle
private final String iban; // field/state/attribute
double balance; // < 0.0 -> illegal state
Expand Down
14 changes: 13 additions & 1 deletion core-banking/src/com/example/banking/domain/Customer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.example.banking.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

// DDD: Entity Class -> Persistent, Identity
public class Customer {
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private final String identity; // does allow to change
private String fullName;
private List<Account> accounts;
Expand Down Expand Up @@ -67,4 +72,11 @@ public double getTotalBalance8() {
public int getNumberOfAccounts() {
return accounts.size();
}

@Override
public String toString() {
return "Customer [identity=" + identity + ", fullName=" + fullName + ", accounts=" + accounts + "]";
}


}
Loading

0 comments on commit 5248fef

Please sign in to comment.