Last Real World problem (in module 04 - 05), you added functionality to allow th
ID: 3710475 • Letter: L
Question
Last Real World problem (in module 04 - 05), you added functionality to allow the user to enter multiple patients until the user indicated done. You are to enhance this functionality. Add the following functionality to your report app:
Each field in this file is delimited by a carat (^)
The fields included in this file are ID^Last Name^First Name^Address Line 1^Address Line 2(optional)^City^State^Zip^ZipPlus4(Optional)^Payment Date^Payment Amount^Amount Owed
The fields marked as optional may not contain data. For example, the format might appear like this:
ID^Last Name^First Name^Address Line 1^^City^State^Zip^^Payment Date^Payment Amount^Amount Owed
When producing the report
Skip those fields that are not part of the report format
Rearrange those fields that are in a different order so they correspond to the report format
Add a prompt to the user "Do you want to output the report to the screen ('S'), to a file ('F') or both ('B')".
If the user enters 'S' your code should display the report to the screen as it currently does.
Whether the user enters an uppercase or lowercase letter should not matter. For example 'S' or 's' should not matter. In both cases, the user should display the data as described above.
If the user enters "F" (or "f"), then the app should
Prompt the user for the desired file name (including the file path)
Output the report to a file using the file name / path the user entered. This output should be in the exact same format as the report is-if sent to the screen except that it outputs the report to a file.
If the user enters "B" (or "b"), then the app should output the report to both the screen and output it to a file.
Contents of text file mentioned above
12345^Jones^John^1234 Test Drive^PO box 123^Test
City^IN^11234^1234^12/05/2015^250.00^25000.00
12346^Jones^Sara^1234 Test Drive^PO box 123^Test
City^IN^11234^1234^12/20/2017^50.00^50000.00
12347^Doe^John^1235 XYZ Drive^^Test
City^IN^11234^^01/05/2016^350.00^56799.00
12348^Doe^Sara^1235 XYZ Drive^^Test
City^IN^11234^^11/09/2017^100.00^5020.52
12349^Lawson^Lonnie^12 South Drive^^Test
City^IN^11236^^03/15/2013^253.51^25065.52
12349^Anderson^Sara^156 North Avenue^^Test
City^IN^11246^^05/05/2013^21.33^251.56
12350^Smith^Andy^2455 East Street^^Test
City^IN^11246^^12/05/2017^365.21^2543.33
Language is Java
Explanation / Answer
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class User
{
private final String id;
private final String lstName;
private final String FstName;
private final String addressLine1;
String addressLine2;
private final String City;
private final String State;
private final long zip;
long zipPlus4;
private final double payAmount;
private final String payDate;
private final double payDue;
public User(String id,String lstName,String fstName,
String addressLine1,String addressLine2,String City,
String State,long zip,long zipPlus4,
String payDate,double payAmount,double payDue)
{
this.id=id;
this.lstName=lstName;
this.FstName=fstName;
this.addressLine1=addressLine1;
this.addressLine2=addressLine2;
this.City=City;
this.State=State;
this.zip=zip;
this.zipPlus4=zipPlus4;
this.payAmount=payAmount;
this.payDate=payDate;
this.payDue=payDue;
}
public void setAddLine2(String add){
this.addressLine2=add;}
public void setZipPlus4(long zip){
this.zipPlus4=zip;}
public long getZip()
{
return zip;
}
public double getPayAMount()
{
return payAmount;
}
public double getDueAMount()
{
return payDue;
}
public String getDate()
{
return payDate;
}
public String getaddressLine1()
{
return addressLine1;
}
public String getaddressLine2()
{
return addressLine2;
}
public String getCity()
{
return City;
}
public String getState()
{
return State;
}
public String getlstName()
{
return lstName;
}
public String getId()
{
return id;
}
public String getfstName()
{
return FstName;
}
public long getZipPlus4(){
return zipPlus4;}
}
public class Report {
public static void main(String[] args){
List<User> lst=new ArrayList<User>();
Scanner in=new Scanner(System.in);
File file = new File(args[0]);
if (!file.exists()) {
System.out.println(args[0] + " does not exist.");
return;
}
if (!(file.isFile() && file.canRead())) {
System.out.println(file.getName() + " cannot be read from.");
return;
}
try {
List<String> allLines = Files.readAllLines(Paths.get(args[0]));
for (String line : allLines) {
//System.out.println(line);
String[] str=line.split("\^");
User user=new User(str[0],str[1],str[2],
str[3],"".equals(str[4])?"NoValue":str[4],str[5],str[6],
Long.valueOf(str[7]),"".equals(str[8])?Long.MAX_VALUE:Long.valueOf(str[8]),
str[9],Double.valueOf(str[10]),Double.valueOf(str[11]));
lst.add(user);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Do opertauiosn,");
System.out.println("Do you want to output the report to the screen ('S'), to a file ('F') or both ('B')");
String ch=in.nextLine();
StringBuilder sb=new StringBuilder();
for (User user : lst) {
sb.append(user.getId()+"^"+user.getlstName()+","+user.getfstName()+"^"+user.getaddressLine1()
+"^"+("NoValue".equals(user.getaddressLine2())?"":user.getaddressLine2())+"^"+user.getCity()+"^"+user.getState()+"^"+user.getZip()+"^"+(
Long.MAX_VALUE==user.getZipPlus4()?"":user.getZipPlus4())
+"^"+user.getPayAMount()+"^"+user.getPayAMount()+"^"+user.getDueAMount());
sb.append(" ");
}
if("S".equalsIgnoreCase(ch))
{
System.out.println(sb.toString());
}
else if("F".equalsIgnoreCase(ch))
{
System.out.println("Enter output file name with full path");
String outputFilename=in.nextLine();
log(sb.toString(),outputFilename);
System.out.println("File successfully written");
}
else if("B".equalsIgnoreCase(ch))
{
System.out.println(sb.toString());
System.out.println("Enter output file name with full path");
String outputFilename=in.nextLine();
log(sb.toString(),outputFilename);
System.out.println("File successfully written");
}
}
public static void log(String message,String fName){
try {
PrintWriter out = new PrintWriter(new FileWriter(fName, true), true);
out.write(message);
out.close();
} catch (Exception e) {
System.out.print("There is something wrong in output file/file path..retry !");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.