Advertisement
Guest User

Iterator

a guest
Jun 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package subiectiterator;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. class Produs{
  6. private String cod_unic;
  7. private int pret;
  8.  
  9. Produs(String cu, int p){
  10. cod_unic = cu;
  11. pret = p;
  12. }
  13.  
  14. public String getCodUnic(){
  15. return cod_unic;
  16. }
  17.  
  18. public int getPret(){
  19. return pret;
  20. }
  21.  
  22. public String toString(){
  23. return cod_unic + " la pret de " + pret;
  24. }
  25. }
  26.  
  27. class Parfum extends Produs{
  28. Parfum(String cu, int p){
  29. super(cu, p);
  30. }
  31. }
  32.  
  33. class LotiuneCorp extends Produs{
  34. LotiuneCorp(String cu, int p){
  35. super(cu, p);
  36. }
  37. }
  38.  
  39.  
  40. interface Selector{
  41. Object current();
  42. boolean end();
  43. void next();
  44. }
  45.  
  46. class Catalog{
  47. private Produs[] produse;
  48. private int i=0;
  49.  
  50. Catalog(int size){
  51. produse = new Produs[size];
  52. }
  53.  
  54. public void add(Produs p){
  55. if(i<produse.length) produse[i++]=p;
  56. }
  57.  
  58. public void UmpleCatalog(){
  59. Random rand = new Random();
  60.  
  61. int contor_p=100, contor_l=100;
  62. for(int i=1; i<produse.length+1; i++){
  63. int x = rand.nextInt(2);
  64. if(x==0){
  65. Produs nou_produs = new Parfum(("P"+contor_p), (rand.nextInt(50)+20));
  66. add(nou_produs);
  67. contor_p++;
  68. } else {
  69. Produs nou_produs = new LotiuneCorp(("LC"+contor_l), (rand.nextInt(50)+20));
  70. add(nou_produs);
  71. contor_l++;
  72. }
  73. }
  74. }
  75.  
  76. int next=0;
  77. class SequenceSelector implements Selector{
  78. public Produs current(){
  79. return produse[next];
  80. }
  81.  
  82. public boolean end(){
  83. return next==produse.length;
  84. }
  85.  
  86. public void next(){
  87. next++;
  88. }
  89.  
  90. }
  91.  
  92. public void PrintareCatalog() throws FileNotFoundException {
  93. File file = new File("./src/subiectiterator/catagout");
  94. PrintWriter out = new PrintWriter(file);
  95.  
  96. SequenceSelector selector = new SequenceSelector();
  97. while(!selector.end()){
  98. out.println(selector.current());
  99. selector.next();
  100. }
  101.  
  102. out.close();
  103. }
  104. }
  105.  
  106. public class Subiectiterator {
  107.  
  108. public static void main(String[] args) {
  109. Catalog c = new Catalog(3);
  110. c.UmpleCatalog();
  111.  
  112. try{
  113. c.PrintareCatalog();
  114. } catch (FileNotFoundException exept){
  115. System.out.println("sry errror");
  116. }
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement