In: Computer Science
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it will be left to you to decide if they will methods in the data entry program, or methods from a validation class. The data entered for each request should be written to an encrypted file. Use combo boxes and "good lists" where appropriate. Throw exception objects to handle errors and fail securely.
Request.java
public class Request {
private String firstName, lastName;
private double amount;
public Request()
{
this.firstName = this.lastName = "";
this.amount = 0.0;
}
public Request(String firstName, String lastName, double amount)
{
this.firstName = firstName;
this.lastName = lastName;
this.amount = amount;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public double getAmount() {
return amount;
}
public String fileStr()
{
return(firstName + "," + lastName + "," + String.format("%.2f",
amount));
}
@Override
public String toString()
{
return(firstName + " " + lastName + " has requested an amount of $"
+ String.format("%,.2f", amount));
}
}
RequestTest.java (Main class)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class RequestTest {
private static final String FILENAME = "requests.txt";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Request> reqs = new ArrayList<>();
int choice;
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.println("\nREQUEST FOR MONEY:\n"
+ "------------------");
System.out.print("Enter first name: ");
String firstName = sc.nextLine().trim();
System.out.print("Enter last name: ");
String lastName = sc.nextLine().trim();
System.out.print("Enter request amount: $");
double amount = Double.parseDouble(sc.nextLine().trim());
while(amount < 1 || amount > 1000)
{
System.out.println("Request amount should be between $1 and
$1000!");
System.out.print("Enter request amount: $");
amount = Double.parseDouble(sc.nextLine().trim());
}
Request r = new Request(capitalizeStr(firstName),
capitalizeStr(lastName), amount);
reqs.add(r);
writeFile(FILENAME, reqs);
System.out.println(r);
break;
}
case 2:
{
System.out.println("\nDISPLAY ALL REQUESTS:\n"
+ "---------------------");
if(reqs.isEmpty())
System.out.println("Request list is empty!\n");
else
{
ArrayList<Request> rList = readFile(FILENAME);
for(Request r : rList)
System.out.println(r);
System.out.println();
}
break;
}
}
System.out.println();
}while(choice != 3);
}
private static void displayMenu()
{
System.out.print("1. Request for money\n"
+ "2. Display all requests\n"
+ "3. Exit\n"
+ "Your selection >> ");
}
private static String capitalizeStr(String s)
{
return(Character.toUpperCase(s.charAt(0)) + s.substring(1));
}
private static ArrayList<Request> readFile(String
filename)
{
ArrayList<Request> requests = new ArrayList<>();
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
String firstName = data[0];
String lastName = data[1];
double amount = Double.parseDouble(data[2]);
requests.add(new Request(firstName, lastName, amount));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found!
Exiting..");
System.exit(0);
}
return requests;
}
private static void writeFile(String filename,
ArrayList<Request> reqs)
{
if(reqs.isEmpty())
{
System.out.println("No data in list to write to file!\n");
return;
}
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(filename));
pw = new PrintWriter(fw);
for(Request r : reqs)
pw.write(r.fileStr() + System.lineSeparator());
pw.flush();
fw.close();
pw.close();
} catch (IOException ex) {
System.out.println("Error in writing data to " + filename + "!
Exiting..");
System.exit(0);
}
}
}
*********************************************************** SCREENSHOT *****************************************************
INPUT/OUTPUT FILE (requests.txt)
CONSOLE OUTPUT :