File Handling Tasks
File Handling Tasks
File Handling Tasks
LAB REPORT 14
SUBJECT NAME:
OOP
Lecturer:
Mam Anum
SUBMITTED BY:
Objective: Develop a program for managing inventory transactions in a hardware store, including
stock updates, sales, and generating reports.
Instructions:
1. Initialize a file named "inventory.dat" with records for different items in the store. Each record
should include fields for item identification number, item name, current stock quantity, and cost per
unit.
4. Include features to calculate and update the total revenue earned from sales.
5. Allow the program to continue running until the user chooses to exit.
Sol:
Class main:
package lab14;
import java.io.*;
objectOut.close();
System.out.println("The Object was successfully written to a
file");
}
}
Class Manager:
package lab14;
import java.io.*;
import java.util.Scanner;
total_quantity_sold += s;
total_sales += sales;
}
@Override
public String toString() {//generating output for file
return "Manager{" +
"ID_num=" + ID_num +
", name='" + name + '\'' +
", current_stock_quantity=" + current_stock_quantity
+
", cost_per_unit=" + cost_per_unit +
", total_quantity_sold=" + total_quantity_sold +
", total_sales=" + total_sales +
'}';
}
}
Output:
On wrong entry
On correct entry
Written into a file:
Task 2 : To-Do List Management
Objective: Create a program to manage a to-do list using a text file. The program should allow users to
add tasks, mark tasks as completed, and display the pending tasks.
Instructions:
1. Initialize a text file named "todo_list.txt" to store the to-do list. Each line in the file should represent
a task with the following format: TaskID,TaskDescription,Status.
- Example: 1,Buy groceries,Incomplete
4. Allow the program to continue running until the user chooses to exit.
Sol:
Class todolist:
package lab14tsk2;
import java.util.Scanner;
public Todolist() {
}
}
}
Class main:
package lab14tsk2;
import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;
do {
System.out.println("Enter input respectively:" +
"1: for adding a new task " +
"2: for marking task as completed" +
"3: for displaying pending tasks" +
"-1: to exit ");
try {
input = obj.nextInt();
switch (input) {
case 1: {//case for adding task
task[i] = new Todolist();
task[i].add_new_task();
String s = task[i].toString();
Objectout.writeObject(s);
System.out.println();
i++;
break;
}
case 2: {//case for marking complete
System.out.println("Enter input for marking a
specific task completed");
int current_ID = obj.nextInt();
Output:
Written into a file:
Conclusion:
In current lab we handled exceptions i.e. input mismatch and illegal argument and how to write into a
file or get it from the file.