1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mart [117]
3 years ago
6

Fred wants to analyze his spending habits of the past few years and has gathered information on the checks he has written from 2

014 – 2018. The data is in a file in the form YYYY XXXXXX.XX where YYYY represents the year and XXXXXX.XX represents the dollar amount of the check. Because it took a while to gather the information, the data is not in year order. See the Input Data at the end of the assignment. After reading in the data, he wants to print a report by year of the number of checks written, the total amount spent and the average amount of the check. Also, an overall total of the five years appears after the individual data of each year.
Using one array to store the running total of the check amounts by year, an array to hold the number of checks written by year write the Java code to read and store the data and then do the necessary computations to produce a report in the format below. Use printf to display the dollar amounts with two decimal places and right adjust all output. You can set an int variable baseYear to 2014 and subtract it from the actual year to get the correct subscript in each array.

Sample report
Year # Checks Total Spent Average Check
2014 25 3452.23 198.02
. . . .
. . . .
. . . .
2018 33 1097.87 125.67
Overall Total 254 15478.98 167.65

The numbers appearing in the sample report above are not related to the actual input data to follow.
Input Data

2017 876.20
2014 345.67
2014 100.00
2016 345.89
2017 45.34
2015 89.23
2016 1000.00
2017 239.09
2018 67.89
2018 198.00
2015 145.45
2018 180.00
2015 505.23
2014 78.65
2014 42.98
2014 370.00
Computers and Technology
1 answer:
finlep [7]3 years ago
4 0

Answer:

See explaination

Explanation:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class SpendAnalysis {

// year 2014 data will be at index 0

// year 2015 data will be at index 1

// year 2016 data will be at index 2

// year 2017 data will be at index 3

// year 2018 data will be at index 4

private static int[] checkIssued={0,0,0,0,0}; // will store the count of check for the year

private static double[] totalAmount={0,0,0,0,0}; // will store the total amount spent for that year

private static final int BASE_YEAR=2014; // base year

// need to update the below line so that it points to the input text file need to be read

private static final String INPUT_FILE="D:\\spent.txt";

public static void main(String[] args) {

// first read all the data from the input file and populate the array

try {

populateData();

} catch (FileNotFoundException e) {

System.out.println(INPUT_FILE+" could not read. Program will terminate now.");

System.exit(1);

}

// second print the data in the console

reportSpending();

}

// read data from input file and update the two arrays

public static void populateData() throws FileNotFoundException {

Scanner fileInput = new Scanner(new File(INPUT_FILE));

while(fileInput.hasNext()){

String[] lineTokens = fileInput.nextLine().split("\\s+");

if(lineTokens.length==2){

int year = Integer.parseInt(lineTokens[0].trim());

double amount = Double.parseDouble(lineTokens[1].trim());

checkIssued[year-BASE_YEAR]+=1;

totalAmount[year-BASE_YEAR]+=amount;

}

}

}

// print the report in the console

public static void reportSpending(){

System.out.println(String.format("%-13s%-15s%-15s%-15s","Year","# Checks","Total Spent","Average Check"));

System.out.println(String.format("%-15d%-15d$%-15.2f$%-15.2f",2014,checkIssued[0],totalAmount[0],totalAmount[0]/checkIssued[0]));

System.out.println(String.format("%-15d%-15d$%-15.2f$%-15.2f",2015,checkIssued[1],totalAmount[1],totalAmount[1]/checkIssued[1]));

System.out.println(String.format("%-15d%-15d$%-15.2f$%-15.2f",2016,checkIssued[2],totalAmount[2],totalAmount[2]/checkIssued[2]));

System.out.println(String.format("%-15d%-15d$%-15.2f$%-15.2f",2017,checkIssued[3],totalAmount[3],totalAmount[3]/checkIssued[3]));

System.out.println(String.format("%-15d%-15d$%-15.2f$%-15.2f",2018,checkIssued[4],totalAmount[4],totalAmount[4]/checkIssued[4]));

int totalChecks = checkIssued[0]+checkIssued[1]+checkIssued[2]+checkIssued[3]+checkIssued[4];

double totalSpent = totalAmount[0]+totalAmount[1]+totalAmount[2]+totalAmount[3]+totalAmount[4];

System.out.println(String.format("%-15s%-15d$%-15.2f$%-15.2f","Overall Total",totalChecks,totalSpent,totalSpent/totalChecks));

}

}

You might be interested in
"The _____ of the Open Systems Interconnection (OSI) model generates the receiver’s address and ensures the integrity of message
aleksklad [387]

Answer:

The transport layer

Explanation: Layer 4, is the transport layer of the Open System Interconnection (OSI), that handles message transfer of data between end systems or hosts and ensures complete packets transfer.

7 0
3 years ago
Read 2 more answers
Edison wants to purchase a new computer and go to the Caribbean for spring break. The computer is priced at $1,299, and the vaca
Otrada [13]

Answer:

This question is imcomplete, here is the complete question

Edison wants to purchase a new computer and go to the Caribbean for spring break. The computer is priced at $1,299, and the vacation is priced at $750. He has only $1,537 in his checking account, so he cannot afford to purchase both. After much thought, Edison buys the computer and writes a check for $1,299. Identify what role money plays in each of the following parts of the story. Hint: Select each role only once. Role of Money Medium of Exchange Unit of Account Store of Value Edison can easily determine that the price of the computer is more than the price of the vacation. Edison has $1,537 in his checking account. Edison writes a check for $1,299.

Identify what role money plays in each of the following parts of the story.

Explanation:

Brian can effortlessly determine and conclude that the cost of the computer is more than that of the price of the vacation.= Unit of Account

Brian has a total amount of $1,537 in his checking account.= Store of value

Brian then writes a check for $1,299.= Medium of Exchange

5 0
3 years ago
Create a scenario of a hypothetical company that needs an information system. This scenario should include the following informa
Effectus [21]

A  hypothetical company that needs an information system is Memphis drilling and construction company.

<h3>What is the size of a project?</h3>

This is known to be the  the number of firms taking part in the project and thus in the case above, it is small project.

In terms of Staff/experts availability, there are experts from the mining industry with years of experience, soil survey analyst as well as other minor staff.

In terms of System level of criticality, it is big because the extent to drilling will make use of the information system and other major decisions made.

Learn more about  information system from

brainly.com/question/14688347

#SPJ1

6 0
1 year ago
How do you take a picture with this app
Troyanec [42]
You have to be answering someone and to the left of the answer button there will be a camera. Click it and you can take a picture.
7 0
3 years ago
Read 2 more answers
Create a dictionary that will hold AT LEAST 3 categories for food with at least 3 foods for each category. E.g. Fruits --&gt; Ap
RSB [31]

Answer:

  1. food_category = {
  2.    "Fruits": ["Apple", "Strawberries", "Orange"],
  3.    "Vegetables": ["Carrots", "Peas", "Onions"],
  4.    "Meats": ["Chicken", "Beef", "Mutton"]
  5. }
  6. for cat in food_category:
  7.    output = cat + ": "
  8.    for food in food_category[cat]:
  9.        output += food + ", "
  10.    print(output)
  11. answer = input("Do you want to add category or food (Y - Yes  N - No): ")
  12. while(answer == "Y"):
  13.    choice = input("New Category (N) or New Food (nf): ")
  14.    
  15.    if(choice == "N"):
  16.        new_cat = input("Enter new category name: ")
  17.        if(new_cat not in food_category):
  18.            food_category[new_cat] = ""
  19.    elif(choice == "nf"):
  20.        cat = input("Enter category: ")
  21.        new_food = input("Enter new food name: ")  
  22.        food_category[cat].append(new_food)
  23.    
  24.    answer = input("Do you want to add new category or food (Y - Yes N - No): ")
  25. print("All food categories: ")
  26. for cat in food_category:
  27.    print(cat)
  28. selected_cat = input("Select a category to view the food list: ")
  29. output = ""
  30. for food in food_category[selected_cat]:
  31.    output += food + ", "
  32. print(output)

Explanation:

The solution code is written in Python 3.

Firstly, create a food category dictionary data structure (Line 1 -5). Next, display all the food category and the food in each category (Line 7 - 11)

Next prompt user to feedback if they wish to add new category or new food (Line 13).

While the answer is yes prompt user to enter their choice, either category or food and use if else if statements to handle the choice made by the user. If the choice is new category, prompt user to enter new category name and check if the new category is already exist in the dictionary. If not, add that category to dictionary (Line 18 - 21).

If the choice is food, nf, prompt user to input food category and enter the new food name and add it to the food_category list (Line 22 - 25).

After that prompt user to enter if they wish to continue to add new data (Line 27).

After collecting all info from user, write a for loop to display all the categories (Line 29 - 31).

At last, prompt user to select a category and display all the food of the selected category (Line 33 - 37).

8 0
2 years ago
Other questions:
  • It is important that researchers design rigorous study methods to eliminate: a. inferences. b. the need for a CONSORT flow chart
    10·1 answer
  • It can be useful to have a mentor because they will help you
    7·2 answers
  • William found out that someone used his report on American culture without his permission. What is William a victim of?
    13·2 answers
  • Write a sentence about a mineral you use and how you use it
    12·1 answer
  • Which is most harmful computer virus define​
    15·1 answer
  • A computer is a multipurpose device that accepts input, processes data, stores data, and produces output, all according to a ser
    9·1 answer
  • You are the leader of a team at work. What type of leader would you like to be – one that gets involved and works with the team
    15·2 answers
  • Which is the purpose of adding B-Roll footage to a sequence?
    10·1 answer
  • What is MVC architecture in relation to developing in web applications? How
    6·1 answer
  • In a typical office, biometric authentication might be used to control access to employees and registered visitors only. We know
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!