Advertisement
Guest User

Untitled

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