Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.nio.*;
- 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 x=0;
- Scanner sc = new Scanner(System.in);
- do {
- try
- {
- x = sc.nextInt();
- sc.nextLine();
- break;
- } catch (InputMismatchException e) {
- System.err.println("This is not a number. Please try again.");
- sc.nextLine();
- }
- } while(true);
- return x;
- }
- //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();
- }
- 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));
- }
- // 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(scan.nextLine());
- scan.close();
- }
- // 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)
- {
- 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");
- break;
- }
- }
- }while(check);
- sc.close();
- }
- //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);
- sc1.close();
- }
- 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));
- }
- public static void AddOrder()
- {
- }
- 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);
- sc.close();
- }
- 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!");
- System.out.printf("Select one of the shown numbers please! \n1: Manage Medecines Stock \n2: Manage clients ordering lists.");
- int 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();
- }
- else if(m3 == 4)
- {
- RemoveClient(Clients);
- }
- else if(m3 == 5)
- {
- RemoveOrder();
- }
- }
- else
- {
- }
- ALtoFile("medecines.data", Meds);
- ALtoFile("clients.data",Clients);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement