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)...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
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...
Write a simple javascript program using express and node.js to create a simple webpage that can...
Write a simple javascript program using express and node.js to create a simple webpage that can lead to other pages within the program. if possible, Comment everything out so I could understand how every things works.  But basically, server should be hosted on localhost:8000 and should have a simple starting page. Maybe a welcome message, with at least two "links" that goes to a "homepage" and then a "exit" page.
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...
Write a java program using the following instructions: Write a program that determines election results. Create...
Write a java program using the following instructions: Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT