Question

In: Computer Science

Using java you have to create a simple program that will allow for the storage of...

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.

Solutions

Expert Solution

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 :


Related Solutions

***USING JAVA Scenario: You will be writing a program that will allow a user to find...
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word. Requirements: Do not use Arrays for this assignment. Do not use any String class methods (.phrase(), replace methods, regex methods)...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
4.) Create a Java program using the “extends” and “runnable” methods. You will be creating your...
4.) Create a Java program using the “extends” and “runnable” methods. You will be creating your own threaded program using the threadExtend.java code and the threadRunnable.java code. A.) Focus the threadExtend.java code DETAILS TO NOTE: - It creates 4 instances of the same thread. This means that the thread code is actually running 4 separate times - The thread accepts 2 parameters, an INTEGER from 1 to 4, as well as an ID - Note the parameters are hard coded...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT