So I am working on making a code for my Aunt who is a doctor, This is what the code does
1.Asks Name
2.Asks Gender(press 1 for male, 2 for female)
3.Occupation(1-student,2-job,3-Others)
if we select 3 we are made to write the other occupation
4.Ask age
5.Shows the Name,Gender,Occupation and age in a formal manner
proceeds to ask if we wanna add another patient
So What I want this code to be able to do is but have no idea how are:-
1. I want step 5 to be written on a text file in my computer and continue to add as we keep on adding more patients.
2. I want to be able to search through the patient based on the age,name,etc. You get the drill.
You don't need to write the full code if you don't want to but it will be really Helpful if anyone can teach me how to write informations in a text with example and how i can read those text files later and search them based on age and name
Here is the code
Ignore the Search by name,age etc part of the program I Havnt worked on them yet as I dont know how to write the informations I get to a text file in my computer and then read then in java and search thorough the database
SUBCLASS
MAIN CLASSimport java.util.InputMismatchException; import java.util.Scanner; public class methods { int serial=1; String nameFinal; String occupationFianl; String genderFinal; String drugFinal; String freqFinal; String routeFinal; int ageFinal; long numFinal; String durationFinal; //name public void InputName() { Scanner input = new Scanner(System.in); System.out.println("enter the name of the patient."); String name= input.nextLine(); nameFinal=name; } //age public void InputAge() { Scanner input = new Scanner(System.in); System.out.println("Enter the age of the patient."); int age=input.nextInt(); ageFinal=age; } //gender public void Inputgender() { boolean rep=true; Scanner input = new Scanner(System.in); System.out.println("Enter the Gender of the patient...1-male, 2-female."); do { try { int gender= input.nextInt(); if (gender==1) { genderFinal="Male"; rep=false; } else if(gender==2) { genderFinal="Female"; rep=false; } else throw new InputMismatchException("wrong input"); } catch (Exception e) { input.reset(); rep=true; } }while(rep==true); } //occupation public void InputOcc() { boolean rep=true; Scanner input = new Scanner(System.in); System.out.println("Enter the occupation of student."); System.out.println("1-fulltime Student, 2-Dropout School, 3-employed, 4-Unemployed."); do { try { int Occ= input.nextInt(); if (Occ==1) { occupationFianl="Fulltime Student"; rep=false; } else if(Occ==2) { occupationFianl="Dropout School"; rep=false; } else if(Occ==3) { occupationFianl="Employed"; rep=false; } else if(Occ==4) { occupationFianl="Unemployed"; rep=false; } else throw new InputMismatchException("wrong input"); } catch (Exception e) { input.reset(); rep=true; } }while(rep==true); } //drugasbused public void InputDrugUsed() { Scanner input = new Scanner(System.in); System.out.println("Enter the Drugs used"); input.reset(); String drug= input.nextLine(); drugFinal=drug; } //durationofdrug public void InputDrugDuration() { Scanner input = new Scanner(System.in); boolean rep=true; System.out.println("enter the duration(1-1 year & more, 2- 6months or less.)"); do { try { int duration= input.nextInt(); if (duration==1) { durationFinal="1 year& more."; rep=false; } else if(duration==2) { durationFinal="6 months or less."; rep=false; } else throw new InputMismatchException("wrong input"); } catch (Exception e) { input.reset(); rep=true; } }while(rep==true); } //frequency public void InputFrequency() { boolean rep = true; Scanner input = new Scanner(System.in); System.out.println("Enter the frequency of drug used"); System.out.println("1-daily, 2-twice a week, 3-once a week"); do { try { int freq= input.nextInt(); if (freq==1) { freqFinal="Daily."; rep=false; } else if(freq==2) { freqFinal="twice a week."; rep=false; } else if(freq==3) { freqFinal="Once a week."; rep=false; } else throw new InputMismatchException("wrong input"); } catch (Exception e) { input.reset(); rep=true; } }while(rep==true); } //route public void Inputroute() { boolean rep=true; Scanner input = new Scanner(System.in); System.out.println("means of route."); System.out.println("1-injection, 2-snorting"); do { try { int route= input.nextInt(); if (route==1) { routeFinal="Injection"; rep=false; } else if(route==2) { routeFinal="Snorting"; rep=false; } else throw new InputMismatchException("wrong input"); } catch (Exception e) { System.out.println("wrong input, Try again"); input.reset(); rep=true; } }while(rep==true); } //number public void InputNum() { Scanner input = new Scanner(System.in); System.out.println("enter contacnt nnumber."); long num=input.nextLong(); numFinal=num; } //getage public int getAge() { return ageFinal; } //getname public String getName() { return nameFinal; } //showlist public void showlist() { System.out.println("Name(Abuser): "+nameFinal); System.out.println("Age : "+ageFinal); System.out.println("Sex : "+genderFinal); System.out.println("Occupation(Victim) : "+occupationFianl); System.out.println("Drug Abused : "+drugFinal); System.out.println("Frequency of Dosage : "+freqFinal); System.out.println("Route : "+routeFinal); System.out.println("Phone number: "+numFinal); } //search by namePat //searchby occupationPat //searchbyage public void Collect() { InputName(); InputAge(); Inputgender(); InputOcc(); InputDrugUsed(); InputDrugDuration(); InputFrequency(); Inputroute(); InputNum(); showlist(); System.out.println(""); System.out.println(""); } }
import java.util.*; public class mainbody { public static void main(String[] args) { ArrayList<methods> arraysOfObj=new ArrayList<methods>(); Scanner input = new Scanner(System.in); String answer; int objNO=0; boolean repeat=true; do { arraysOfObj.add(new methods()); arraysOfObj.get(objNO).Collect(); objNO++; System.out.println("wanna add more?"); answer=input.nextLine(); if(answer.equalsIgnoreCase("yes")) { repeat=true; } else repeat=false; }while(repeat==true); //searches int arrays[]=new int[arraysOfObj.size()]; do { System.out.println("press 1 to search Database by name"); System.out.println("press 2 to seach by age"); System.out.println("press 3 to search by occupation status"); System.out.println("press 4 to search by Duration"); System.out.println("press 5 to search by frequency"); System.out.println("press 6 to show all list"); int choice=input.nextInt(); //search by name if(choice==1) { String nameS=input.nextLine(); for(int i=0;i<arrays.length;i++) { if(arraysOfObj.get(i).nameFinal.equalsIgnoreCase(nameS)) { arraysOfObj.get(i).showlist(); } } } if(choice==2) { int ageS=input.nextInt(); for(int i=0;i<arrays.length;i++) { if(arraysOfObj.get(i).getAge()==ageS) { arraysOfObj.get(i).showlist(); } } } if(choice==3) { String OccS=input.nextLine(); for(int i=0;i<arrays.length;i++) { if(arraysOfObj.get(i).occupationFianl.equalsIgnoreCase(OccS)) { arraysOfObj.get(i).showlist(); } } } if(choice==4) { String Duration=input.nextLine(); for(int i=0;i<arrays.length;i++) { if(arraysOfObj.get(i).durationFinal.contains(Duration)) { arraysOfObj.get(i).showlist(); } } } if(choice==5) { String freq=input.nextLine(); for(int i=0;i<arrays.length;i++) { if(arraysOfObj.get(i).freqFinal.contains(freq)) { arraysOfObj.get(i).showlist(); } } } if(choice==6) { for(int i=0;i<arrays.length;i++) { arraysOfObj.get(i).showlist(); } } } while(1!=0); } }