Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hardware Inventory – Write a database to keep track of tools, their cost and num

ID: 3758593 • Letter: H

Question

Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session.

Example Program Session: (First Run)Enter request

4 - Exit ?3

?1
Enter record number ?5

( 1 to 100, 0 to return to main menu )

Enter tool name, quantity, cost
? saw 102 12
Enter record number ( 1 to 100, 0 to return to main menu ) ?7

Enter record number ?0
Enter request

?3

( 1 to 100, 0 to return to main menu )

Record # Tool name Quantity Cost 5 saw 102 12.00 7 hammer 75 8.00 Enter request

1 - Input new tool or update an existing tool 2 - Delete a tool
3 - List all tools
4 - Exit

?4

Press any key to continue . . . (Second Run)Enter request

?3
Record # Tool name 5 saw
7 hammer Enter request

or update

?4
Press any key to continue ...

Explanation / Answer

import java.io.*;
import java.util.*;

class Tool{
   String name;
   int qunatity;
   double cost;
   public Tool(String n,int q,double c){
       name = n;
       qunatity = q;
       cost = c;
   }
}

class main{
   public static void main(String[] args){
       // reading from database
       BufferedReader br = new BufferedReader(new FileReader("hardware.dat"));
       Tool[] tool = new Tool[100];
       int i = 0;
       while ((line = br.readLine()) != null) {
           String[] l = line.split(' ');
           Tool tl = new Tool(l[0],Integer.parseInt(l[1]),Double.parsedouble(l[2]));
           tool[i] = tl;
           i += 1;
       }
       br.close();
       // All functionality
       Scanner sc = new Scanner(System.in);
       while (true){
           System.out.println("1. Input new tool");
           System.out.println("2. Delete a Tool");
           System.out.println("3. List all Tools ");
           System.out.println("4. Exit");
           int n = sc.nextInt();
           if (n == 1){
               System.out.print("Enter item number (1 to 100), 0 to return to main menu: ");
               int item = sc.nextInt();
               if (item == 0)
                   break;
               System.out.println("Enter tool name, quantity, cost ");
               System.out.print("? ");
               String[] ll = sc.nextLine().split(' ');
               Tool tl = new Tool(ll[0],Integer.parseInt(ll[1]),Double.parsedouble(ll[2]));
               tool[item-1] = tl;
           }
           else if (n == 2){
               while (true){
                   System.out.print("Enter item number (1 to 100), 0 to return to main menu: ");
                   int item = sc.nextInt();
                   if (item == 0)
                       break;
                   tool[item-1].name = "None";
                   tool[item-1].cost = 0.0;
                   tool[item-1].qunatity = 0;
               }
           }
           else if (n == 3){
               System.out.println("Record # Tool name Quantity Cost");
               for (int i = 0; i < 100; i++){
                   if (tool[i].name.compareTo("None") != 0){
                       System.out.println((i+1) + ' ' + tool[i].name + ' ' + tool[i].quantity + ' ' + tool[i].cost);
                   }
               }
           }
           else
               break;
       }
       // Writing back to database
       File file = new File("hardware.dat");
       FileWriter fw = new FileWriter(file.getAbsoluteFile());
       BufferedWriter bw = new BufferedWriter(fw);
       for (int i = 0; i < 100; i++)
           bw.write(tool[i].name + ' ' + tool[i].quantity + ' ' + tool[i].cost +' ');

       bw.close();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote