Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package subiectiterator;
- import java.util.*;
- import java.io.*;
- class Produs{
- private String cod_unic;
- private int pret;
- Produs(String cu, int p){
- cod_unic = cu;
- pret = p;
- }
- public String getCodUnic(){
- return cod_unic;
- }
- public int getPret(){
- return pret;
- }
- public String toString(){
- return cod_unic + " la pret de " + pret;
- }
- }
- class Parfum extends Produs{
- Parfum(String cu, int p){
- super(cu, p);
- }
- }
- class LotiuneCorp extends Produs{
- LotiuneCorp(String cu, int p){
- super(cu, p);
- }
- }
- interface Selector{
- Object current();
- boolean end();
- void next();
- }
- class Catalog{
- private Produs[] produse;
- private int i=0;
- Catalog(int size){
- produse = new Produs[size];
- }
- public void add(Produs p){
- if(i<produse.length) produse[i++]=p;
- }
- public void UmpleCatalog(){
- Random rand = new Random();
- int contor_p=100, contor_l=100;
- for(int i=1; i<produse.length+1; i++){
- int x = rand.nextInt(2);
- if(x==0){
- Produs nou_produs = new Parfum(("P"+contor_p), (rand.nextInt(50)+20));
- add(nou_produs);
- contor_p++;
- } else {
- Produs nou_produs = new LotiuneCorp(("LC"+contor_l), (rand.nextInt(50)+20));
- add(nou_produs);
- contor_l++;
- }
- }
- }
- int next=0;
- class SequenceSelector implements Selector{
- public Produs current(){
- return produse[next];
- }
- public boolean end(){
- return next==produse.length;
- }
- public void next(){
- next++;
- }
- }
- public void PrintareCatalog() throws FileNotFoundException {
- File file = new File("./src/subiectiterator/catagout");
- PrintWriter out = new PrintWriter(file);
- SequenceSelector selector = new SequenceSelector();
- while(!selector.end()){
- out.println(selector.current());
- selector.next();
- }
- out.close();
- }
- }
- public class Subiectiterator {
- public static void main(String[] args) {
- Catalog c = new Catalog(3);
- c.UmpleCatalog();
- try{
- c.PrintareCatalog();
- } catch (FileNotFoundException exept){
- System.out.println("sry errror");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement