Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

expenselist.java: //ExpenseList class //Defines an unordered list of Expense objects //Uses the generic unordered list (List ) class and Expense class public class ExpenseList {

image text in transcribed

expenselist.java:

//ExpenseList class

//Defines an unordered list of Expense objects

//Uses the generic unordered list (List) class and Expense class

public class ExpenseList

{

private List expenses;

public ExpenseList()

{

expenses = new List();

}

public void add(Expense exp)

{

expenses.add(exp);

}

public boolean isEmpty()

{

return expenses.isEmpty();

}

public boolean contains(Expense exp)

{

return expenses.contains(exp);

}

public Expense first()

{

return expenses.first();

}

public Expense next()

{

return expenses.next();

}

public void enumerate()

{

expenses.enumerate();

}

public double maxExpense()

{

double max =0.0, amt;

Expense exp = expenses.first();

while (exp!=null)

{

amt = exp.getAmount();

if (amt>max)

max = amt;

exp = expenses.next();

}

return max;

}

public double minExpense()

{

double min =Double.MAX_VALUE, amt;

Expense exp = expenses.first();

if (exp==null) return 0.0;

else

{

while (exp!=null)

{

amt = exp.getAmount();

if (amt

min = amt;

exp = expenses.next();

}

}

return min;

}

/*

public double avgExpense()

{

//TODO: complete this method

//Finds the average of all expenses

}

public double totalExpense()

{

//TODO: complete this method

//Finds the sum of all expenses

}

public double amountSpentOn(String expItem)

{

//TODO: complete this method

//Finds the amount spent on a particular item

}

*/

}

expenselistdemo.java:

//ExpenseListDemo

//Program reads a text file containing list of expenses

//File contains date, description and amount one on each line:

//Sep 21, 2018

//Lunch

//4.51

//Program then processes the file and displays expense data such as

//max expense amount, min expense amount, etc.

import java.util.Scanner;

import java.io.*;

public class ExpenseListDemo

{

public static void main(String[] args) throws IOException

{

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter the filename to read from: ");

String filename = keyboard.nextLine();

File file = new File(filename);

Scanner inputFile = new Scanner(file);

ExpenseList sepexpenses = new ExpenseList();

String date, desc, cost;

Expense exp=null;

while (inputFile.hasNext())

{

date = inputFile.nextLine();

desc = inputFile.nextLine();

cost = inputFile.nextLine();

exp = new Expense(date, desc, Double.parseDouble(cost));

sepexpenses.add(exp);

}

inputFile.close();

System.out.println("September Expenses");

System.out.println("Max expense: $" + sepexpenses.maxExpense());

System.out.println("Min expense: $" + sepexpenses.minExpense());

//System.out.println("Average expense: $" + sepexpenses.avgExpense());

//System.out.println("The amount spent on groceries: $" + sepexpenses.amountSpentOn("Groceries"));

//System.out.println("Total expense: $" + sepexpenses.totalExpense());

}

}

sepexpenses.txt:

Sep 21, 2018 Lunch 4.51 Sep 21, 2018 Gas 25.00 Sep 21, 2018 Movies 36.36 Sep 21, 2018 Groceries 22.55 Sep 24, 2018 Groceries 72.58 Sep 24, 2018 Breakfast 11.48 Sep 24, 2018 Tennis 15.00 Sep 24, 2018 Hair Salon 34.50 Sep 25, 2018 CreditCard 245.00 Sep 27, 2018 CarRental 360.01 Sep 27, 2018 Pizza 11.50 Sep 28, 2018 Gas 81.1

Exercise 1: Complete the avgExpense, totalExpense and amountSpentOn methods in the TODO section of the ExpenseList.java. Uncomment the System.out.println statements in the the following output: Enter the filename to read from: sepexpenses.txt September Expenses Max expense: $360.01 Min expense: $4.51 Average expense: $76.635 The amount spent on groceries: $95.13 Total expense: $919.62 Exercise 1: Complete the avgExpense, totalExpense and amountSpentOn methods in the TODO section of the ExpenseList.java. Uncomment the System.out.println statements in the the following output: Enter the filename to read from: sepexpenses.txt September Expenses Max expense: $360.01 Min expense: $4.51 Average expense: $76.635 The amount spent on groceries: $95.13 Total expense: $919.62

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions