Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. import java.util.*;
  2. import java.nio.*;
  3. import java.io.*;
  4. public class Pharmacy {
  5.  
  6. public static void Auth()
  7. {
  8. Scanner sc = new Scanner(System.in);
  9. boolean check = true;
  10. do
  11. {
  12. System.out.println("Enter Username:");
  13. String UserName = sc.nextLine();
  14. System.out.println("Enter Password");
  15. String Password = sc.nextLine();
  16. if (UserName.equals("Mahdi") && Password.equals("admin"))
  17. break;
  18. else System.out.println("Wrong! Please check again. \n");
  19. } while (check);
  20. sc.close();
  21. }
  22.  
  23. public static int Read()
  24. {
  25. int x=0;
  26. Scanner sc = new Scanner(System.in);
  27. do {
  28. try
  29. {
  30. x = sc.nextInt();
  31. sc.nextLine();
  32. break;
  33.  
  34. } catch (InputMismatchException e) {
  35. System.err.println("This is not a number. Please try again.");
  36. sc.nextLine();
  37. }
  38. } while(true);
  39. return x;
  40. }
  41.  
  42. //Method to send data from file to array
  43. public static ArrayList<String> FileToAL(String a) throws Exception
  44. {
  45. Scanner rw = new Scanner(new File(a));
  46. ArrayList<String> m = new ArrayList<String>();
  47.  
  48. while(rw.hasNext())
  49. {
  50. m.add(rw.nextLine());
  51. }
  52. rw.close();
  53. return m;
  54. }
  55.  
  56. //Method to send data from array to file
  57. public static void ALtoFile(String a,ArrayList<String> b) throws Exception
  58. {
  59. PrintWriter bw = new PrintWriter(new FileWriter(a));
  60. for(int i = 0; i < b.size() -1 ; i = i +2)
  61. {
  62. bw.printf("%s\r\n",b.get(i));
  63. bw.printf("%s\r\n",b.get(i+1));
  64. }
  65. bw.flush();
  66. bw.close();
  67. }
  68.  
  69. public static void ShowList(ArrayList<String> a, String x,String y)
  70. {
  71. System.out.printf("%-20s%20s%n",x,y );
  72. for (int j=0;j<a.size()-1;j=j+2)
  73. System.out.printf("%-20s%20s%n",a.get(j),a.get(j+1));
  74. }
  75.  
  76. // Method to add a medicine to the array
  77. public static void AddMed(ArrayList<String> c ) {
  78. Scanner scan = new Scanner(System.in);
  79. String name ;
  80. do {
  81. System.out.print("Enter Medecine Name: ");
  82. name = scan.nextLine();
  83. if ((CheckIfExists(name,c)==false)) break;
  84. else {
  85. System.out.printf("%s already exists!\n",name);
  86. }
  87. }while((CheckIfExists(name,c)==true));
  88. c.add(name);
  89. System.out.print("Enter Quantity: ");
  90. c.add(scan.nextLine());
  91. scan.close();
  92. }
  93.  
  94. // Method to check if the med exists
  95. public static boolean CheckIfExists(String a,ArrayList<String> c) {
  96.  
  97. for(int i=0;i<c.size()-1;i=i+2)
  98. {
  99. if (a.equals(c.get(i))) return true;
  100. }
  101. return false;
  102. }
  103.  
  104. //Method to Remove Medicines
  105. public static void RemoveMed(ArrayList<String> c)
  106. {
  107. Scanner sc = new Scanner(System.in);
  108. boolean check = true;
  109. do
  110. {
  111. System.out.println("Enter the medicine you wish to remove.");
  112. String name = sc.nextLine();
  113. if(CheckIfExists(name,c) == false) {
  114. System.out.println("The medecine doesn't exist");
  115. continue;
  116. }
  117. for (int i = 0 ; i < c.size()-1; i=i+2)
  118. {
  119. if(name.equals(c.get(i)))
  120. {
  121. c.remove(i);
  122. c.remove(i);
  123. check = false;
  124. System.out.println("Medicine removed");
  125. break;
  126. }
  127. }
  128.  
  129. }while(check);
  130. sc.close();
  131. }
  132.  
  133. //Method to Update the quantity of Medicine
  134. public static void UpdateMed(ArrayList<String> c)
  135. {
  136. Scanner sc1 = new Scanner(System.in);
  137. boolean check = false;
  138. do
  139. {
  140. System.out.println("Enter the Medecine you wish to update");
  141. String name = sc1.nextLine();
  142. for(int i=0;i<c.size()-1;i=i+2)
  143. {
  144. if (name.equals(c.get(i)))
  145. {
  146. System.out.println("Enter the quantity you wish to add");
  147. int quantity = Read();
  148. c.set(i+1,(String.valueOf(Integer.parseInt(c.get(i+1))+quantity)));
  149. check=true;
  150. }
  151. }
  152. } while(check= false);
  153. sc1.close();
  154. }
  155.  
  156. public static void AddClient(ArrayList<String> c)
  157. {
  158. Scanner sc= new Scanner(System.in);
  159. System.out.println("Enter the name of the client: ");
  160. String name = sc.nextLine();
  161. c.add(name);
  162. c.add(String.valueOf(Integer.parseInt(c.get(c.size()-2))+1));
  163.  
  164. }
  165.  
  166. public static void AddOrder()
  167. {
  168.  
  169. }
  170.  
  171. public static void RemoveClient(ArrayList<String> c)
  172. {
  173. Scanner sc = new Scanner(System.in);
  174. boolean check = true;
  175. do
  176. {
  177. System.out.println("Enter the client you wish to remove.");
  178. String name = sc.nextLine();
  179. if(CheckIfExists(name,c) == false) {
  180. System.out.println("The client doesn't exist");
  181. continue;
  182. }
  183. for (int i = 0 ; i < c.size()-1; i=i+2)
  184. {
  185. if(name.equals(c.get(i)))
  186. {
  187. c.remove(i);
  188. c.remove(i);
  189. check = false;
  190. System.out.println("Client removed.");
  191. break;
  192. }
  193. }
  194.  
  195.  
  196. }while(check);
  197. sc.close();
  198.  
  199. }
  200.  
  201. public static void RemoveOrder()
  202. {
  203.  
  204. }
  205.  
  206.  
  207. public static void main(String[] args) throws Exception
  208. {
  209. // Creating array for Medicines
  210. ArrayList<String> Meds = new ArrayList<String>();
  211. Meds = FileToAL("medecines.data");
  212.  
  213. // Creating Array for Clients
  214. ArrayList<String> Clients = new ArrayList<String>();
  215. Clients = FileToAL("clients.data");
  216.  
  217. //Auth();
  218. System.out.println("Welcome!");
  219.  
  220. System.out.printf("Select one of the shown numbers please! \n1: Manage Medecines Stock \n2: Manage clients ordering lists.");
  221. int m1 = Read();
  222. if (m1 == 1)
  223. {
  224. System.out.printf("You chose to manage medicines stock.\n1: to Show List.\n2: to Add. \n3: to Remove.\n4: to Update.\n ");
  225. int m2 = Read();
  226. if(m2 == 1)
  227. {
  228. ShowList(Meds,"Medicine","Quantity");
  229. }
  230. else if(m2 == 2)
  231. {
  232. AddMed(Meds);
  233. }
  234. else if (m2 == 3)
  235. {
  236. RemoveMed(Meds);
  237. }
  238. else if (m2 == 4)
  239. {
  240. UpdateMed(Meds);
  241. ShowList(Meds,"Medicine","Quantity");
  242. }
  243. else System.out.println("Enter one of the following numbers");
  244. }
  245. else if (m1 == 2)
  246. {
  247. System.out.printf("You chose to manage clients ordering lists.\n1: to Show List.\n2: to Add new client. \n3: to Add new order \n4: to Remove Client.\n5: to Remove order. ");
  248. int m3 = Read();
  249. if(m3 == 1)
  250. {
  251. ShowList(Clients,"Client","ID");
  252. }
  253. else if(m3 == 2)
  254. {
  255. AddClient(Clients);
  256. }
  257. else if(m3 == 3)
  258. {
  259. AddOrder();
  260. }
  261. else if(m3 == 4)
  262. {
  263. RemoveClient(Clients);
  264. }
  265. else if(m3 == 5)
  266. {
  267. RemoveOrder();
  268. }
  269. }
  270. else
  271. {
  272.  
  273. }
  274. ALtoFile("medecines.data", Meds);
  275. ALtoFile("clients.data",Clients);
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement