1. Use the
java.io.ObjectOutputStream class to write a Java object to the file system
(serialize), and then use the same stream to read the file back into an object
reference. You will also customize the serialization and deserialization of the
ShoppingCart object
Ans CLICK HERE
2. Write a program to create a bank application that provides the basic functionalities to operate the savings account.
The application should provide the following functionalities:
A menu should be displayed that will allow the user to open a new account, deposit money, withdraw money, check balance, and exit.
If the open a new account option is selected, the program should prompt the user to enter the basic details, such as name, contact number, address, and opening amount. Further, it must generate an account number and allocate that number to the new account. In addition, the user details along with the account number must be saved in the Saving_Account.txt file; and a separate file for the new account with the account number as the name of the file must be created.
If the deposit money, withdraw money, or check balance option is selected, the program should ask for the account number from the user. Thereafter, it should validate the account number, and on the basis of the valid account number, it should execute the following functionalities: Deposit money option:
The program should ask the user to enter the deposit amount. In addition, it should update the transaction and the balance in the respective file of the account holder. Withdraw money option: The program should ask the user to enter the amount to withdraw. In addition, it should update the transaction and the balance in the respective file of the account holder. Check balance: It should display the current balance. If the exit option is selected, the application must terminate.
Ans CLICK HERE
3. Write a Java program to perform the following tasks:
Reading a text file
Writing in a text file
Searching in a text file
Ans 1. Create a Java application. Chapter10_Exercise03.
2. Create a class. FileOperation. in the Java application. Chapter10_Exercise03.
3. Replace the existing code in the FileOperation.java file with the following code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
public class FileOperation {
public void readFile() {
Scanner sc = new Scanner(System.in);
String fileName;
System.out.print("Enter the text file name that you want to read from the D:\\ drive: "); fileName = sc.nextLine();
try (BufferedReader br = new BufferedReader(new FileReader("D:\\" + fileName + ".txt "))) {
String s;
System.out.println("\nThe " + fileName + " contains the following text:\n");
System.out.println
while ((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
System.out.println("File does not exists."); menu();
}
}
public void writeFile() {
Scanner sc = new Scanner(System.in);
String fileName, text;
System.out.print("Enter the file name from the D:\\ drive in which you 'want to write: "); fileName = sc.nextLine ();
System.out.print("Enter the text you want to write in the " + fileName + " file: "); text = sc.nextLine();
try {
FileWriter fos = new FileWriter("D:\\" + fileName + ".txt", true);
BufferedWriter bw = new BufferedWriter(fos); bw.newLine(); bw.write(text);
bw.close (); fos.close();
System.out.println("\nThe text has been added in the file");
} catch (Exception e) {
System.out.println("File does not exists"); menu();
}
}
public void searchFileO {
Scanner sc = new Scanner(System.in);
String fileName, text; int counter = 0, flag = 0;
System.out.print("Enter the file name from the D:\\ drive in which you want to search: ");
fileName = sc.nextLine();
System.out.print("Enter the string you want to search in the " + fileName + " file: "); text = sc.nextLine();
try (BufferedReader br = new BufferedReader(new FileReader("D:\\" + fileName + ".txt "))) {
String s;
while ((s = br.readLine()) != null) {
String splitData() = s.split(" ");
{
for (int i = 0; i < splitData.length; i++) if (splitData(i].equals(text)) {
if (splitData(i].equals(text)) {
counter++; flag = 1;
}
}
} catch (Exception e) {
System.out.println("File does not exits"); menu () ;
}
if (flag == 1) (
if (counter == 1) {
System.out.println("\"" + text + "\" has occured " + counter + " time");
} else {
System.out.println("\"" + text + "\" has occured " + counter + " times");
}
} else {
System.out.println("The entered word doest not exist in the file.");
}
public void menu() { int option;
Scanner sc = new Scanner(System.in);
System, out. print In (" \n-----Menu---------") ;
System.out.println("1. Reading");
System.out.println("2. Writing");
System.out.println("3. Searching");
System.out.println("4. Exit");
System.out.print("\nChoose the option: ")
option = sc.nextInt();
switch (option) { case 1:
readFile(); menu(); break; case 2:
writeFile(); menu(); break; case 3:
searchFile();
menu(); break; case 4:
System.exit(0); break; default:
System.out.println("Incorrect menu option");
menu(); break;
}
}
public static void main(String!) args) {
FileOperation fo = new FileOperation() fo.menu();
}
}
No comments:
Post a Comment