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 java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
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.
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer to do the following: 1) Create a bank account by supplying a user id and password .2) Login using their id and password 3) Quit the program. Now if login was successful the user will be able to do the following: 1) Withdraw money. 2) Deposit money. 3) Request balance. 4) Quit the program. If login was not successful (for example the id or...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT