Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 2: Owner notifies that the item is lost to the server. (writes to a file). Need help writing this in Java for Step 2.

Step 2: Owner notifies that the item is lost to the server. (writes to a file).

Need help writing this in Java for Step 2.

Extra information for Step 2:

Step1: Owner of valuable item registers item with tag ID and owner details to the server. (writes to a file). Step 1 Code:

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;

public class FileWriting { public static final String FILE_PATH = System.getProperty("user.home") + File.separatorChar + "My Documents"; //DEFAULT MY DOCUMENTS PATH: you can change it as per requirement public static String FILE_NAME = "tagFile.txt"; // ITEM DETAILS FILE NAME: you can change it as per requirement public static String menuInput; public static int menuChoice; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); do { showMenu(); menuInput = br.readLine(); //a normal string for user input while(!isDigit(menuInput)) //loop continues until the input is a digit { System.out.print("Enter your selection (Only numbers are allowed): "); menuInput = br.readLine(); //again, taking the console input into the string variable } menuChoice = Integer.parseInt(menuInput); //if it is a digit, accepting the input menuInput = ""; //setting the string variable to null, making it ready for further console inputs! switch(menuChoice) { case 1: //check if the file exists. If not, create the file! File itemFile = new File(FILE_PATH, FILE_NAME); if(!itemFile.exists()) { itemFile.createNewFile(); System.out.println("File created!"); } //File already exists; now taking inputs from the user about the item! System.out.print(" Enter Item Tag ID: "); String itemTagId = br.readLine().toUpperCase(); System.out.print("Enter Item Name: "); String itemName = br.readLine(); System.out.print("Enter Item Type: "); String itemType = br.readLine(); System.out.print("Enter Item Quantity (Only numbers are allowed): "); menuInput = br.readLine(); while(!isDigit(menuInput)) { System.out.println("Enter Item Quantity (Only numbers are allowed): "); menuInput = br.readLine(); } int itemQuantity = Integer.parseInt(menuInput); menuInput = ""; System.out.print("Enter Item Description: "); String itemDescription = br.readLine(); System.out.print("Enter Per Item Cost: "); double itemCost = Double.parseDouble(br.readLine()); String formattedItemDetails = "Tag ID: " + itemTagId + System.lineSeparator() + "Item Name: " + itemName + System.lineSeparator() + "Item Type: " + itemType + System.lineSeparator() + "Item Quantity: " + itemQuantity + System.lineSeparator() + "Item Description: " + itemDescription + System.lineSeparator() + "Cost of /item: $" + itemCost + System.lineSeparator(); //Writing these details to the file "tagFile.txt" FileWriter itemFileWriter = new FileWriter(itemFile, true); //setting true for append mode PrintWriter printWriter = new PrintWriter(itemFileWriter); printWriter.println(formattedItemDetails); System.out.println(" " + itemName + " is successfully added to the file! "); printWriter.close(); break; case 2: //for reading the file details, check if the file exists! itemFile = new File(FILE_PATH, FILE_NAME); if(!itemFile.exists()) //file does not exist { System.out.println("Could not read file: File does not exist! "); } //File already exists; now reading each lines one by one and displaying them on the console BufferedReader fileReader = new BufferedReader(new FileReader(itemFile)); String fileLine = fileReader.readLine(); if(fileLine == null) //if file contains no lines, displays a suitable message System.out.println(" File is empty! "); System.out.println(" Reading file details... "); while(fileLine != null) //if file has lines written in it { System.out.println(fileLine); //print the current line fileLine = fileReader.readLine(); //jump to the next line } fileReader.close(); break; case 3: System.out.println("User chose to exit the program!"); System.exit(0); //Exiting program with status code 0 = OK } }while(menuChoice >= 1 && menuChoice <= 3); // the loop continues until the input is in between 1 and 3 } public static void showMenu() //display the main menu { System.out.print("1. Write Item Details to Server (a file) 2. Show File Details 3. Exit " + "Enter your selection (Only numbers are allowed): "); } public static boolean isDigit(String s) //check if the string s is a digit or not { try { Integer.parseInt(s); return true; }catch(NumberFormatException e){ System.out.println(s + " is not a valid number!"); return false; } } }

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

Students also viewed these Databases questions

Question

Define unit testing, integration testing, and system testing.

Answered: 1 week ago