Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Pharmacy {
- public static void Auth() {
- Scanner sc = new Scanner(System.in);
- boolean check = true;
- do {
- System.out.println("Enter Username:");
- String UserName = sc.nextLine();
- System.out.println("Enter Password");
- String Password = sc.nextLine();
- if (UserName.equals("Mahdi") && Password.equals("admin"))
- break;
- else
- System.out.println("Wrong! Please check again. \n");
- } while (check);
- sc.close();
- }
- public static int Read() {
- int m;
- Scanner sc = new Scanner(System.in);
- do {
- try {
- int x = sc.nextInt();
- m=x;
- sc.nextLine();
- break;
- } catch (InputMismatchException e) {
- System.err.println("This is not a number. Please try again.");
- sc.nextLine();
- }
- } while (true);
- return m;
- }
- // Method to send data from file to array
- public static ArrayList<String> FileToAL(String a) throws Exception {
- Scanner rw = new Scanner(new File(a));
- ArrayList<String> m = new ArrayList<String>();
- while (rw.hasNext()) {
- m.add(rw.nextLine());
- }
- rw.close();
- return m;
- }
- // Method to send data from array to file
- public static void ALtoFile(String a, ArrayList<String> b) throws Exception {
- PrintWriter bw = new PrintWriter(new FileWriter(a));
- for (int i = 0; i < b.size() - 1; i = i + 2) {
- bw.printf("%s\r\n", b.get(i));
- bw.printf("%s\r\n", b.get(i + 1));
- }
- bw.flush();
- bw.close();
- }
- // Method to show list
- public static void ShowList(ArrayList<String> a, String x, String y) {
- System.out.printf("%-20s%20s%n", x, y);
- for (int j = 0; j < a.size() - 1; j = j + 2)
- System.out.printf("%-20s%20s%n", a.get(j), a.get(j + 1));
- System.out.println();
- }
- // Method to add a medicine to the array
- public static void AddMed(ArrayList<String> c) {
- Scanner scan = new Scanner(System.in);
- String name;
- do {
- System.out.print("Enter Medecine Name: ");
- name = scan.nextLine();
- if ((CheckIfExists(name, c) == false))
- break;
- else {
- System.out.printf("%s already exists!\n", name);
- }
- } while ((CheckIfExists(name, c) == true));
- c.add(name);
- System.out.print("Enter Quantity: ");
- c.add(String.valueOf(Read()));
- System.out.println("Medecine Added.");
- System.out.println();
- }
- // Method to check if the med exists
- public static boolean CheckIfExists(String a, ArrayList<String> c) {
- for (int i = 0; i < c.size() - 1; i = i + 2) {
- if (a.equals(c.get(i)))
- return true;
- }
- return false;
- }
- // Method to Remove Medicines
- public static void RemoveMed(ArrayList<String> c) throws Exception{
- ShowList(c,"Medicine","Quantity");
- Scanner sc = new Scanner(System.in);
- boolean check = true;
- do {
- System.out.println("Enter the medicine you wish to remove.");
- String name = sc.nextLine();
- if (CheckIfExists(name, c) == false) {
- System.out.println("The medecine doesn't exist");
- continue;
- }
- for (int i = 0; i < c.size() - 1; i = i + 2) {
- if (name.equals(c.get(i))) {
- c.remove(i);
- c.remove(i);
- check = false;
- System.out.println("Medicine removed");
- System.out.println();
- break;
- }
- }
- } while (check);
- }
- // Method to Update the quantity of Medicine
- public static void UpdateMed(ArrayList<String> c) {
- Scanner sc1 = new Scanner(System.in);
- boolean check = false;
- do {
- System.out.println("Enter the Medecine you wish to update");
- String name = sc1.nextLine();
- for (int i = 0; i < c.size() - 1; i = i + 2) {
- if (name.equals(c.get(i))) {
- System.out.println("Enter the quantity you wish to add");
- int quantity = Read();
- c.set(i + 1, (String.valueOf(Integer.parseInt(c.get(i + 1)) + quantity)));
- check = true;
- }
- }
- } while (check = false);
- System.out.println("Quantity updated.");
- System.out.println();
- }
- // Method to Add clients
- public static void AddClient(ArrayList<String> c) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the name of the client: ");
- String name = sc.nextLine();
- c.add(name);
- c.add(String.valueOf(Integer.parseInt(c.get(c.size() - 2)) + 1));
- }
- // Method to add order
- public static void AddOrder(ArrayList<String> c) throws Exception {
- Scanner sc = new Scanner(System.in);
- System.out.printf("You chose to add an order.\nPlease Enter the name of the client: ");
- String name = sc.nextLine();
- PrintWriter bw;
- for (int i = 0; i < c.size(); i = i + 2) {
- if (name.equals(c.get(i))) {
- String fileName = "orderingList_" + c.get(i + 1) + "_.data";
- System.out.println("Enter Medecine Name:");
- String med = sc.nextLine();
- System.out.println("Enter Quantity");
- int quantity = Read();
- bw = new PrintWriter(new FileOutputStream(new File(fileName), true));
- bw.printf("%s\r\n", med);
- bw.printf("%s\r\n", String.valueOf(quantity));
- bw.flush();
- bw.close();
- break;
- }
- }
- }
- // Method to remove client
- public static void RemoveClient(ArrayList<String> c) {
- Scanner sc = new Scanner(System.in);
- boolean check = true;
- do {
- System.out.println("Enter the client you wish to remove.");
- String name = sc.nextLine();
- if (CheckIfExists(name, c) == false) {
- System.out.println("The client doesn't exist");
- continue;
- }
- for (int i = 0; i < c.size() - 1; i = i + 2) {
- if (name.equals(c.get(i))) {
- c.remove(i);
- c.remove(i);
- check = false;
- System.out.println("Client removed.");
- break;
- }
- }
- } while (check);
- }
- // Method to Remove order
- public static void RemoveOrder() {
- }
- public static void main(String[] args) throws Exception {
- // Creating array for Medicines
- ArrayList<String> Meds = new ArrayList<String>();
- Meds = FileToAL("medecines.data");
- // Creating Array for Clients
- ArrayList<String> Clients = new ArrayList<String>();
- Clients = FileToAL("clients.data");
- // Auth();
- System.out.println("Welcome!");
- int m1;
- do {
- System.out.printf(
- "Select one of the shown numbers please! \n1: Manage Medecines Stock \n2: Manage clients ordering lists. \n9: Exit");
- m1 = Read();
- if (m1 == 1) {
- System.out.printf(
- "You chose to manage medicines stock.\n1: to Show List.\n2: to Add. \n3: to Remove.\n4: to Update.\n ");
- int m2 = Read();
- if (m2 == 1) {
- ShowList(Meds, "Medicine", "Quantity");
- } else if (m2 == 2) {
- AddMed(Meds);
- } else if (m2 == 3) {
- RemoveMed(Meds);
- } else if (m2 == 4) {
- UpdateMed(Meds);
- ShowList(Meds, "Medicine", "Quantity");
- } else
- System.out.println("Enter one of the following numbers");
- } else if (m1 == 2) {
- 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. ");
- int m3 = Read();
- if (m3 == 1) {
- ShowList(Clients, "Client", "ID");
- } else if (m3 == 2) {
- AddClient(Clients);
- } else if (m3 == 3) {
- AddOrder(Clients);
- } else if (m3 == 4) {
- RemoveClient(Clients);
- } else if (m3 == 5) {
- RemoveOrder();
- }
- }
- else if(m1==9) {
- break;
- }
- ALtoFile("medecines.data", Meds);
- ALtoFile("clients.data", Clients);
- } while(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement