Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package test;
- import java.util.Scanner;
- public class Client {
- Scanner scan = new Scanner(System.in);
- public void search(Store storeMobilePhone){
- System.out.println("Enter the needed brand name: ");
- String brandName= scan.nextLine();
- System.out.println("Enter the minimum amount: ");
- double minPrice = scan.nextDouble();
- System.out.println("Enter the maximum price ");
- double maxPrice = scan.nextDouble();
- try{
- if(brandName.equalsIgnoreCase("Iphone") )
- {
- storeMobilePhone.stoc.rangeSearch(minPrice, maxPrice, brandName);
- }
- else if(brandName.equalsIgnoreCase("Samsung"))
- {
- storeMobilePhone.stoc.rangeSearch(minPrice, maxPrice, brandName);
- }
- else
- {
- throw new PhoneNotFoundException("Brand not found. Try again.");
- // System.out.println("Brand doesn't exist. Try again.");
- }
- }
- catch(Exception e ){e.printStackTrace();
- }
- }
- }
- package test;
- public interface IMobile {
- public String getBrandName();
- }
- package test;
- public interface IMobileFactory {
- public Object createMobile();
- }
- package test;
- public class Iphone implements IMobile {
- private double productionCost;
- private double price;
- public Iphone(double productionCost) {
- this.productionCost = productionCost;
- }
- @Override
- public String getBrandName() {
- return getClass().getName();
- }
- public void setPrice(){
- this.price=productionCost+(productionCost*0.20);
- }
- public double getPrice() {
- return price;
- }
- }
- package test;
- import java.util.Random;
- public class IphoneFactory implements IMobileFactory {
- //Factory Method
- @Override
- public Iphone createMobile() {
- return new Iphone(randomPrice());
- }
- //RandomPrice
- private double randomPrice(){
- double min=400;
- double max=900;
- double productionPrice = min + new Random().nextDouble() * (max-min);
- return productionPrice;
- }
- }
- package test;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class MobilePhone {
- ArrayList<Iphone> iphoneList;
- ArrayList<Samsung> samsungList;
- public MobilePhone() {
- this.iphoneList = new ArrayList<Iphone>() ;
- this.samsungList =new ArrayList<Samsung>();
- }
- public void addIphone(Iphone iphone)
- {
- this.iphoneList.add(iphone);
- }
- public void addSamsung (Samsung samsung)
- {
- this.samsungList.add(samsung);
- }
- /*public void ListCont(){
- for (Samsung Sam : samsungList)
- System.out.println(Sam.getBrandName() + "\n price is " + Sam.getPrice());
- }*/
- public void samsungItems ()
- {
- Iterator <Samsung> it = samsungList.iterator(); // se initializeaza iteratorul
- while (it.hasNext())
- {
- Samsung p=it.next(); //stocheaza adresa din memorie pentru obiecte de tip Samsung
- System.out.println("Samsung price: " + p.getPrice()); //p -> referinta la obiect Samsung + field getPrice()
- }
- }
- public void iphoneItems ()
- {
- Iterator <Iphone> it = iphoneList.iterator(); // se initializeaza iteratorul
- while (it.hasNext())
- {
- Iphone p=it.next(); //stocheaza adresa din memorie pentru obiecte de tip Samsung
- System.out.println("Iphone price: " + p.getPrice()); //p -> referinta la obiect Samsung + field getPrice()
- }
- }
- public void rangeSearch(double min, double max, String brand)
- {
- if(brand.equalsIgnoreCase("Iphone"))
- {
- Iterator <Iphone> it= iphoneList.iterator();
- while(it.hasNext())
- {
- Iphone p=it.next();
- if(p.getPrice()>=min && p.getPrice()<=max)
- {
- System.out.println("Available prices for Iphone : " + p.getPrice());
- }
- }
- }
- else
- {
- Iterator <Samsung> it= samsungList.iterator();
- while(it.hasNext())
- {
- Samsung p=it.next();
- if(p.getPrice()>=min && p.getPrice()<=max)
- {
- System.out.println("Available prices for Samsung : " + p.getPrice());
- }
- }
- }
- }
- }
- package test;
- public class PhoneNotFoundException extends Exception {
- public PhoneNotFoundException(String string) {
- super(string);
- }
- }
- package test;
- public class Samsung implements IMobile{
- private double productionCost;
- private double price;
- public Samsung(double productionCost) {
- this.productionCost = productionCost;
- }
- @Override
- public String getBrandName() {
- return getClass().getSimpleName();
- }
- public void setPrice()
- {
- this.price=productionCost+(productionCost*0.18);
- }
- public double getPrice(){
- return this.price;}
- }
- package test;
- import java.util.Random;
- public class SamsungFactory implements IMobileFactory{
- //Factory Method
- @Override
- public Samsung createMobile() {
- return new Samsung(randomPrice());
- }
- //Random Price
- private double randomPrice(){
- double min=400;
- double max=900;
- double productionPrice = min + new Random().nextDouble() * (max-min);
- // System.out.println("Inside random price " + productionPrice);
- return productionPrice;
- }
- }
- package test;
- import java.util.ArrayList;
- public class Store {
- MobilePhone stoc = new MobilePhone();
- public Store() {
- stoc = new MobilePhone();
- }
- public void toSale(IphoneFactory iphone)
- {
- Iphone newIphone =iphone.createMobile();
- newIphone.setPrice();
- this.stoc.addIphone(newIphone);
- }
- public void toSale(SamsungFactory samsung)
- {
- //System.out.println("Inside to sale before createMobile " );
- Samsung newSamsung = samsung.createMobile();
- //System.out.println("Inside to sale after createMobile " );
- newSamsung.setPrice();
- // System.out.println("After set price " );
- this.stoc.addSamsung(newSamsung);
- // System.out.println("after stock add samsung" );
- }
- }
- package test;
- public class Test {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //testat aparitie nume clasa
- //Iphone telefon = new Iphone();
- //String nume = telefon.getBrandName();
- // System.out.println(nume);
- //testat random productionPrice
- /*SamsungFactory randomPrice = new SamsungFactory();
- double random = randomPrice.randomPrice();
- System.out.println(random);*/
- //testat adaugare Samsung
- Store samsungNew = new Store();
- SamsungFactory fact=new SamsungFactory();
- samsungNew.toSale(fact);
- //samsungNew.stoc.ListCont();
- samsungNew.stoc.samsungItems();
- //testare adaugare
- IphoneFactory newIphone = new IphoneFactory();
- samsungNew.toSale(newIphone);
- samsungNew.toSale(newIphone);
- samsungNew.stoc.iphoneItems();
- //printare
- Client customer = new Client();
- // customer.search();
- customer.search(samsungNew);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement