import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
/**
* Garage has customers.
*/
public class Garage {
/** Customer to reg number. */
private Map<String, Customer> storage = new HashMap<>();
/**
* Test method.
*/
public static void main(String[] args) throws IOException {
Garage aGarage = new Garage();
aGarage.addCustomer("1a", "Piter", "Wall street 11a", 110);
aGarage.addCustomer("2b", "Lois", "Wall street 22b", 220);
aGarage.addCustomer("3c", "Jonh", "Wall street 33c", 330);
Path path = Paths.get("README.txt");
if (!Files.exists(path)) {
Files.createFile(path);
}
try (final BufferedWriter writer = Files.newBufferedWriter(path)) {
aGarage.storage.forEach((k, customer) -> {
System.out.println("key : " + k + " customer : " + customer);
try {
writer.write(customer.regNo + "\n");
writer.write(customer.name + "\n");
writer.write(customer.address + "\n");
writer.write(customer.area + "\n\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}
/**
* Add customer.
*
* @param regNo customer reg number.
* @param name customer name.
* @param address customer address.
* @param area customer area.
*/
private void addCustomer(String regNo, String name, String address, int area) {
Customer customer = new Customer(regNo, name, address, area);
storage.put(regNo, customer);
}
/**
* Customer.
*/
private static class Customer {
/** Customer reg number. */
private String regNo;
/** Customer name. */
private String name;
/** Customer address. */
private String address;
/** Customer area. */
private int area;
/**
* Create customer.
*
* @param regNo customer reg number.
* @param name customer name.
* @param address customer address.
* @param area customer area.
*/
Customer(String regNo, String name, String address, int area) {
this.regNo = regNo;
this.name = name;
this.address = address;
this.area = area;
}
@Override
public String toString() {
return "Customer{" + "regNo='" + regNo + '\'' + ", name='" + name + '\'' + ", address='" + address + '\'' + ", area=" + area + '}';
}
}
}
Comments
Leave a comment