Homework Objective: Practice creating and reading binary files. 1. Create a clas
ID: 3639205 • Letter: H
Question
Homework
Objective: Practice creating and reading binary files.
1. Create a class Expense that has a description (String) and amount (double) along with appropriate accessors and mutators.
2. Create a program that creates a binary file (checkbook.bin)
Ask the user the number of expenses (an int). Store this number in the binary file.
Then loop the required number of times to
- prompt the user for a description and amount.
- Store this in an expense object and
- write the object to the binary file.
Close the file after all expenses have been entered.
3. After the binary file has been created. Demonstrate your ability to read the binary file and calculate &display the total amount of expenses
Bonus: Include try/catch to handle FileNotFound Exceptions and other exceptions
Explanation / Answer
import java.util.Scanner;import java.io.*;
public class Expense implements Serializable { private String description; private double amount;
public String getDescription() { return description; } public double getAmount() { return amount; } public void setDescription(String description) { this.description = description; }
public void setAmount(double amount) { this.amount = amount; }
public Expense(String description, double amount) { this.description = description; this.amount = amount; }
private static void saveData(ObjectOutputStream out, Expense[] expenses) throws IOException { out.writeInt(expenses.length); for (Expense expense : expenses) { out.writeObject(expense); } }
private static void readAndPrint(ObjectInputStream in) throws IOException, ClassNotFoundException { int expenses_count = in.readInt(); for (int i = 0; i < expenses_count; i++) { Expense expense = (Expense) in.readObject(); System.out.println(expense.description + " " + expense.amount); } }
public static void main(String[] args) { //Create File File checkbook = new File("checkbook.bin"); ObjectOutputStream out = null; try { FileOutputStream checkbook_output = new FileOutputStream(checkbook); out = new ObjectOutputStream(checkbook_output); } catch (FileNotFoundException ex) { System.out.println("File not found!"); System.exit(1); } catch (IOException ex) {
}
int expense_count; Scanner in = new Scanner(System.in); System.out.print("How many expenses? "); expense_count = in.nextInt(); Expense[] expenses = new Expense[expense_count]; for (int i = 0; i < expense_count; i++) { String description; int amount; System.out.println("Description: "); description = in.next(); System.out.print("Amount: "); amount = in.nextInt(); expenses[i] = new Expense(description, amount); } try { saveData(out, expenses); } catch (IOException ex) { System.out.println("Data can not be written to file!"); System.exit(1); }
try { out.close(); } catch (IOException ex) {
}
try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(checkbook)); readAndPrint(inputStream); } catch (IOException ex) {
} catch (ClassNotFoundException ex) {
}
}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.