Question: Using Java, how to make a Generic class that takes a MedicalRecordInput and after storing the inputs into a .txt file, asks the user if
Using Java, how to make a Generic class that takes a MedicalRecordInput and after storing the inputs into a .txt file, asks the user if they want the input to be saved as any collection type that will be displayed to the console. (ie. the user input can be saved as an ArrayList, Set, or any other collection). At present, the MedicalRecordInput has been successfully saved into .txt but a Generic class is needed to show understanding.




1 package records; 2 3 public class MedicationRecordRefractor extends UserRecord { 4 private String medicationName; 5 private int dosageInMg; 6 private String frequency: 8 public Medication RecordRefractor() () 9 106 public Medication RecordRefractor(String medicationName, int dosageInMg, String frequency) { 11 this.medicationName = medicationName; 12 this.dosageInMg = dosageInMg; 13 this.frequency = frequency: 14 15 } 16 17 // get methods 18 public String getMedicationName() { return medicationName; } 19 public int getDosageInMg() { return dosageInMg; } 20 public String getFrequency() { return frequency; } 21 22 // set methods 23 // Intent: Add Medication into Medication Record 24 // Postcondition: Count of Medication is incremented by 1 250 public void setMedicationName(String medicationName) { 26 this.medicationName = medicationName; 27 } 28 public void setDosageInMg(int dosageInMg) { this.dosageInMg = dosageInMg; } public void setFrequency(String frequency) { this.frequency = frequency; } 30 310 @Override 32 public String display() { 33 return "Medication name: " + this.getMedicationName() + " " + this.getDosageInMg() + "mg is taken "+ this. frequency + " a day."; 34 } 35 36e @Override A37 public String toString() { 38 return String.format("Medication name: + this.getMedicationName() + ", Dose: '+ this.getDosageInMg() + ", and Frequency: 39 } 40 41 } 42 29 + this.getFrequency(); 6 1 package records; 2 import java.util.Scanner; 4 public class Medication Input extends MedicationRecordRefractor { public Medication Input() {}; public Medication RecordRefractor createMedication Input({ 9 10 * Uses Scanner to collect User Input 11 * Precondition: Inputs of medication name(String), dosage (int), and frequency (string) in desired types # Postcondition: The inputs are successfully stored and displayed on console while (true) { // try/catch block to handle correct user input try { Scanner in = new Scanner(System.in); System.out.print("Please enter the Medication Name: "); String medicationName = in.next(); System.out.print("Please enter the dosage in (mg): "); int dosageInMg = in.nextInt(); System.out.print("Please enter the frequency this medication is taken (once, twice, three) : "); String frequency = in.next(); // New instance of MedicationRecord is created using user input MedicationRecordRefractor medicationRecord = new Medication RecordRefractor(medicationName, dosageInMg, frequency); // closing Scanner module in.close(); // outputs Medication Record to the console. System.out.println(medicationRecord.display()); return medicationRecord; } catch (Exception e) { // custom error message System.out.println("Invalid input. Please check again!"); 1 package records; 3 // Import classes for read and write I/0 and exceptions 40 import java.io.BufferedWriter; 8 9 100 /* 11 * Postcondition: creates medicationList text file and calls createMedicationInput() to write the user inputs into text file. 12 13 14 public class RecordwriteToFile { 15 public static void main(String[] args) { 16 // try/catch block for Io exception try { 18 // creating medicationList file 19 FileOutputStream outputStream = new FileOutputStream("medicationList.txt"); OutputStreamWriter outputStreamwriter = new OutputStreamWriter(outputStream, "UTF-16"); BufferedWriter bufferedwriter = new BufferedWriter(outputStreamwriter); // instance of Medication Input and user input toString to write to text file MedicationInput m = new Medication Input(); bufferedWriter.write(m.createMedication Input().toString()); bufferedWriter.newLine(); // closing buffer bufferedWriter.close(); // IOException to catch any issues creating or writing to txt file } catch (IOException e) { e.printStackTrace(); 1 package records; 3 public abstract class UserRecord { // instance variables protected int userId; 6 protected String userName; 8 9 10 116 13 15 16 17 18 19 20 21 // constructor public UserRecord() { } public UserRecord(int userid, String userName) { this.userId = userId; this.userName = userName; } // get methods public int getUserId() { return userId; } public String getUserName() { return userName; } // set methods public void setUserId(int userId) { this.userId = userId; } public void setUserName(String userName) { this.userName = userName; } public abstract String display() ;; 1 package records; 2 3 public class MedicationRecordRefractor extends UserRecord { 4 private String medicationName; 5 private int dosageInMg; 6 private String frequency: 8 public Medication RecordRefractor() () 9 106 public Medication RecordRefractor(String medicationName, int dosageInMg, String frequency) { 11 this.medicationName = medicationName; 12 this.dosageInMg = dosageInMg; 13 this.frequency = frequency: 14 15 } 16 17 // get methods 18 public String getMedicationName() { return medicationName; } 19 public int getDosageInMg() { return dosageInMg; } 20 public String getFrequency() { return frequency; } 21 22 // set methods 23 // Intent: Add Medication into Medication Record 24 // Postcondition: Count of Medication is incremented by 1 250 public void setMedicationName(String medicationName) { 26 this.medicationName = medicationName; 27 } 28 public void setDosageInMg(int dosageInMg) { this.dosageInMg = dosageInMg; } public void setFrequency(String frequency) { this.frequency = frequency; } 30 310 @Override 32 public String display() { 33 return "Medication name: " + this.getMedicationName() + " " + this.getDosageInMg() + "mg is taken "+ this. frequency + " a day."; 34 } 35 36e @Override A37 public String toString() { 38 return String.format("Medication name: + this.getMedicationName() + ", Dose: '+ this.getDosageInMg() + ", and Frequency: 39 } 40 41 } 42 29 + this.getFrequency(); 6 1 package records; 2 import java.util.Scanner; 4 public class Medication Input extends MedicationRecordRefractor { public Medication Input() {}; public Medication RecordRefractor createMedication Input({ 9 10 * Uses Scanner to collect User Input 11 * Precondition: Inputs of medication name(String), dosage (int), and frequency (string) in desired types # Postcondition: The inputs are successfully stored and displayed on console while (true) { // try/catch block to handle correct user input try { Scanner in = new Scanner(System.in); System.out.print("Please enter the Medication Name: "); String medicationName = in.next(); System.out.print("Please enter the dosage in (mg): "); int dosageInMg = in.nextInt(); System.out.print("Please enter the frequency this medication is taken (once, twice, three) : "); String frequency = in.next(); // New instance of MedicationRecord is created using user input MedicationRecordRefractor medicationRecord = new Medication RecordRefractor(medicationName, dosageInMg, frequency); // closing Scanner module in.close(); // outputs Medication Record to the console. System.out.println(medicationRecord.display()); return medicationRecord; } catch (Exception e) { // custom error message System.out.println("Invalid input. Please check again!"); 1 package records; 3 // Import classes for read and write I/0 and exceptions 40 import java.io.BufferedWriter; 8 9 100 /* 11 * Postcondition: creates medicationList text file and calls createMedicationInput() to write the user inputs into text file. 12 13 14 public class RecordwriteToFile { 15 public static void main(String[] args) { 16 // try/catch block for Io exception try { 18 // creating medicationList file 19 FileOutputStream outputStream = new FileOutputStream("medicationList.txt"); OutputStreamWriter outputStreamwriter = new OutputStreamWriter(outputStream, "UTF-16"); BufferedWriter bufferedwriter = new BufferedWriter(outputStreamwriter); // instance of Medication Input and user input toString to write to text file MedicationInput m = new Medication Input(); bufferedWriter.write(m.createMedication Input().toString()); bufferedWriter.newLine(); // closing buffer bufferedWriter.close(); // IOException to catch any issues creating or writing to txt file } catch (IOException e) { e.printStackTrace(); 1 package records; 3 public abstract class UserRecord { // instance variables protected int userId; 6 protected String userName; 8 9 10 116 13 15 16 17 18 19 20 21 // constructor public UserRecord() { } public UserRecord(int userid, String userName) { this.userId = userId; this.userName = userName; } // get methods public int getUserId() { return userId; } public String getUserName() { return userName; } // set methods public void setUserId(int userId) { this.userId = userId; } public void setUserName(String userName) { this.userName = userName; } public abstract String display()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
