Advertisement
Guest User

ioana iphone

a guest
Feb 24th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package test;
  7.  
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class Client {
  12. Scanner scan = new Scanner(System.in);
  13. public void search(Store storeMobilePhone){
  14. System.out.println("Enter the needed brand name: ");
  15. String brandName= scan.nextLine();
  16. System.out.println("Enter the minimum amount: ");
  17. double minPrice = scan.nextDouble();
  18. System.out.println("Enter the maximum price ");
  19. double maxPrice = scan.nextDouble();
  20. try{
  21. if(brandName.equalsIgnoreCase("Iphone") )
  22. {
  23. storeMobilePhone.stoc.rangeSearch(minPrice, maxPrice, brandName);
  24. }
  25. else if(brandName.equalsIgnoreCase("Samsung"))
  26. {
  27. storeMobilePhone.stoc.rangeSearch(minPrice, maxPrice, brandName);
  28. }
  29. else
  30. {
  31. throw new PhoneNotFoundException("Brand not found. Try again.");
  32. // System.out.println("Brand doesn't exist. Try again.");
  33. }
  34. }
  35. catch(Exception e ){e.printStackTrace();
  36.  
  37. }
  38.  
  39. }
  40. }
  41. package test;
  42.  
  43.  
  44. public interface IMobile {
  45. public String getBrandName();
  46.  
  47. }
  48. package test;
  49.  
  50.  
  51. public interface IMobileFactory {
  52. public Object createMobile();
  53. }
  54. package test;
  55.  
  56.  
  57. public class Iphone implements IMobile {
  58.  
  59. private double productionCost;
  60. private double price;
  61.  
  62. public Iphone(double productionCost) {
  63. this.productionCost = productionCost;
  64. }
  65.  
  66.  
  67. @Override
  68. public String getBrandName() {
  69. return getClass().getName();
  70. }
  71.  
  72. public void setPrice(){
  73. this.price=productionCost+(productionCost*0.20);
  74. }
  75.  
  76. public double getPrice() {
  77. return price;
  78. }
  79.  
  80.  
  81.  
  82. }
  83. package test;
  84.  
  85. import java.util.Random;
  86.  
  87.  
  88. public class IphoneFactory implements IMobileFactory {
  89.  
  90. //Factory Method
  91. @Override
  92. public Iphone createMobile() {
  93. return new Iphone(randomPrice());
  94. }
  95.  
  96. //RandomPrice
  97. private double randomPrice(){
  98. double min=400;
  99. double max=900;
  100. double productionPrice = min + new Random().nextDouble() * (max-min);
  101. return productionPrice;
  102. }
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109. package test;
  110.  
  111. import java.util.ArrayList;
  112. import java.util.Iterator;
  113.  
  114.  
  115. public class MobilePhone {
  116. ArrayList<Iphone> iphoneList;
  117. ArrayList<Samsung> samsungList;
  118.  
  119. public MobilePhone() {
  120. this.iphoneList = new ArrayList<Iphone>() ;
  121. this.samsungList =new ArrayList<Samsung>();
  122. }
  123.  
  124.  
  125.  
  126. public void addIphone(Iphone iphone)
  127. {
  128. this.iphoneList.add(iphone);
  129. }
  130.  
  131. public void addSamsung (Samsung samsung)
  132. {
  133. this.samsungList.add(samsung);
  134. }
  135.  
  136. /*public void ListCont(){
  137. for (Samsung Sam : samsungList)
  138. System.out.println(Sam.getBrandName() + "\n price is " + Sam.getPrice());
  139. }*/
  140.  
  141. public void samsungItems ()
  142. {
  143. Iterator <Samsung> it = samsungList.iterator(); // se initializeaza iteratorul
  144. while (it.hasNext())
  145. {
  146. Samsung p=it.next(); //stocheaza adresa din memorie pentru obiecte de tip Samsung
  147. System.out.println("Samsung price: " + p.getPrice()); //p -> referinta la obiect Samsung + field getPrice()
  148. }
  149.  
  150. }
  151.  
  152. public void iphoneItems ()
  153. {
  154. Iterator <Iphone> it = iphoneList.iterator(); // se initializeaza iteratorul
  155. while (it.hasNext())
  156. {
  157. Iphone p=it.next(); //stocheaza adresa din memorie pentru obiecte de tip Samsung
  158. System.out.println("Iphone price: " + p.getPrice()); //p -> referinta la obiect Samsung + field getPrice()
  159. }
  160.  
  161. }
  162.  
  163. public void rangeSearch(double min, double max, String brand)
  164. {
  165. if(brand.equalsIgnoreCase("Iphone"))
  166. {
  167. Iterator <Iphone> it= iphoneList.iterator();
  168. while(it.hasNext())
  169. {
  170. Iphone p=it.next();
  171. if(p.getPrice()>=min && p.getPrice()<=max)
  172. {
  173. System.out.println("Available prices for Iphone : " + p.getPrice());
  174. }
  175. }
  176.  
  177. }
  178. else
  179. {
  180. Iterator <Samsung> it= samsungList.iterator();
  181. while(it.hasNext())
  182. {
  183. Samsung p=it.next();
  184. if(p.getPrice()>=min && p.getPrice()<=max)
  185. {
  186. System.out.println("Available prices for Samsung : " + p.getPrice());
  187. }
  188. }
  189.  
  190. }
  191. }
  192. }
  193. package test;
  194.  
  195.  
  196. public class PhoneNotFoundException extends Exception {
  197.  
  198. public PhoneNotFoundException(String string) {
  199. super(string);
  200. }
  201. }
  202. package test;
  203.  
  204. public class Samsung implements IMobile{
  205.  
  206. private double productionCost;
  207. private double price;
  208.  
  209. public Samsung(double productionCost) {
  210. this.productionCost = productionCost;
  211. }
  212.  
  213.  
  214. @Override
  215. public String getBrandName() {
  216. return getClass().getSimpleName();
  217. }
  218.  
  219. public void setPrice()
  220. {
  221. this.price=productionCost+(productionCost*0.18);
  222. }
  223.  
  224. public double getPrice(){
  225. return this.price;}
  226.  
  227. }
  228. package test;
  229.  
  230. import java.util.Random;
  231.  
  232. public class SamsungFactory implements IMobileFactory{
  233.  
  234.  
  235. //Factory Method
  236. @Override
  237. public Samsung createMobile() {
  238. return new Samsung(randomPrice());
  239. }
  240.  
  241. //Random Price
  242. private double randomPrice(){
  243. double min=400;
  244. double max=900;
  245. double productionPrice = min + new Random().nextDouble() * (max-min);
  246. // System.out.println("Inside random price " + productionPrice);
  247. return productionPrice;
  248. }
  249.  
  250. }
  251.  
  252. package test;
  253.  
  254. import java.util.ArrayList;
  255.  
  256.  
  257. public class Store {
  258. MobilePhone stoc = new MobilePhone();
  259.  
  260. public Store() {
  261. stoc = new MobilePhone();
  262. }
  263.  
  264.  
  265.  
  266. public void toSale(IphoneFactory iphone)
  267. {
  268. Iphone newIphone =iphone.createMobile();
  269. newIphone.setPrice();
  270. this.stoc.addIphone(newIphone);
  271.  
  272. }
  273.  
  274. public void toSale(SamsungFactory samsung)
  275. {
  276. //System.out.println("Inside to sale before createMobile " );
  277. Samsung newSamsung = samsung.createMobile();
  278. //System.out.println("Inside to sale after createMobile " );
  279. newSamsung.setPrice();
  280. // System.out.println("After set price " );
  281. this.stoc.addSamsung(newSamsung);
  282. // System.out.println("after stock add samsung" );
  283.  
  284. }
  285. }
  286.  
  287. package test;
  288.  
  289.  
  290. public class Test {
  291.  
  292.  
  293. /**
  294. * @param args the command line arguments
  295. */
  296. public static void main(String[] args) {
  297. //testat aparitie nume clasa
  298. //Iphone telefon = new Iphone();
  299. //String nume = telefon.getBrandName();
  300. // System.out.println(nume);
  301.  
  302. //testat random productionPrice
  303.  
  304. /*SamsungFactory randomPrice = new SamsungFactory();
  305. double random = randomPrice.randomPrice();
  306. System.out.println(random);*/
  307.  
  308. //testat adaugare Samsung
  309.  
  310. Store samsungNew = new Store();
  311. SamsungFactory fact=new SamsungFactory();
  312. samsungNew.toSale(fact);
  313. //samsungNew.stoc.ListCont();
  314. samsungNew.stoc.samsungItems();
  315.  
  316.  
  317. //testare adaugare
  318. IphoneFactory newIphone = new IphoneFactory();
  319. samsungNew.toSale(newIphone);
  320. samsungNew.toSale(newIphone);
  321. samsungNew.stoc.iphoneItems();
  322.  
  323. //printare
  324.  
  325. Client customer = new Client();
  326. // customer.search();
  327. customer.search(samsungNew);
  328.  
  329.  
  330. }
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement