Skip to content

Instantly share code, notes, and snippets.

@rohanjai777
Created November 8, 2022 14:26
Show Gist options
  • Save rohanjai777/98b1a9947552ed2aaa10b3b72d1d4c81 to your computer and use it in GitHub Desktop.
Save rohanjai777/98b1a9947552ed2aaa10b3b72d1d4c81 to your computer and use it in GitHub Desktop.
import java.util.*;
enum PageType{
A4,
A3,
A5
}
//----------------------------
class NoteBook{
private PageType pageType; //attributes
private int pageCount;
public void print(){
System.out.println(pageType+" "+pageCount);
}
private NoteBook(){} //private constructor for building with params
public NoteBook(PageType pageType, int pageCount){
this.pageType = pageType;
this.pageCount = pageCount;
}
public NoteBook clone(){ //copy this object to another
NoteBook notebook = new NoteBook();
notebook.pageType = this.pageType;
notebook.pageCount = this.pageCount;
return notebook;
}
}
//-------------------------------
class NoteBookRegistory{
private Map<String,NoteBook> notebooks = new HashMap<>();
void registerNotebooks(String type, NoteBook notebook){ //store the notebook object in map.
notebooks.put(type,notebook);
}
NoteBook getNotebook(String type){
return notebooks.get(type).clone(); //return the clone of current notebook object present in map
}
}
//---------------------------------
public class Client{
public static void main(String[] args){
//make first original objects
NoteBook n1 = new NoteBook(PageType.A4, 50);
NoteBook n2 = new NoteBook(PageType.A3, 50);
NoteBook n3 = new NoteBook(PageType.A5, 50);
//put these original objects in registory
NoteBookRegistory registory = new NoteBookRegistory();
registory.registerNotebooks("A4_50",n1);
registory.registerNotebooks("A3_50",n2);
registory.registerNotebooks("A5_50",n3);
//Make a list of inputs you want prototype of.
List<String> inputs = new ArrayList<>();
inputs.add("A4_50");
inputs.add("A4_50");
inputs.add("A3_50");
inputs.add("A5_50");
inputs.add("A5_50");
inputs.add("A5_50");
//loop the input and make notebooks for each input
List<NoteBook> notebooks = new ArrayList<>();
for(String input : inputs){
notebooks.add(registory.getNotebook(input));
}
//print all notebooks
for(NoteBook notebook : notebooks){
notebook.print();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment