Since the solution for this particular exercise has not been

Since the solution for this particular exercise has not been posted, I was just wondering if I could receive some help with formulating the code for this particular problem. (Java Programming, Farrell, eight edition. Chapter 9, exercise 5). Not sure what structure I would use, and what the exact solution for this problem would be.

5. In Chapter 8, you created a Salesperson class with fields for an ID number and sales values. Now, create an application that allows you to store an array that acts as a database of any number of Salesperson objects up to 20. While the user decides to continue, offer three options: to add a record to the database, to delete a record from the database, or to change a record in the database. Then proceed as follows:

a. If the user selects the add option, issue an error message if the database is full. Otherwise, prompt the user for an ID number. If the ID number already exists in the database, issue an error message. Otherwise, prompt the user for a sales value and add the new record to the database.

b. If the user selects the delete option, issue an error message if the database is empty. Otherwise, prompt the user for an ID number. If the ID number does not exist, issue an error message. Otherwise, do not access the record for any future processing.

c. If the user selects the change option, issue an error message if the database is empty. Otherwise, prompt the user for an ID number. If the requested record does not exist, issue an error message. Otherwise, prompt the user, for a new sales value and change the sales value for the record.

Solution

package com.Sales;

public class Sales {

  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Helper h = new Helper();
       h.useDB();
   }
  
  

}

//-----------------------------------------------------------------------------

package com.Sales;

import java.util.HashMap;
import java.util.Scanner;

public class Helper {
  
   HashMap<Integer, Salesperson> h = new HashMap<Integer, Salesperson>();
   public void useDB(){
      
       String ch = new String(\"0\");
      
       while(!ch.equals(\"4\")) {
           Scanner reader = new Scanner(System.in);
           System.out.println(\"1. Add a record\");
           System.out.println(\"2. Delete a record\");
           System.out.println(\"3. Modify a record\");
           System.out.println(\"4. Exit\");
          
           // Reading from System.in
           System.out.println(\"Enter a choice: \");
           if(reader.hasNextLine())
           ch = reader.nextLine();
          
           switch(ch) {
           case \"1\":
               add();
               break;
           case \"2\":
               delete();
               break;
           case \"3\":
               modify();
               break;
           case \"4\":
               break;
           default:
                   System.out.println(\"Invalid input\");
                   break;
           }  
           reader.close();
           ch = \"0\";
       }
      
   }
  
   void add(){
      
       Scanner reader = new Scanner(System.in);
       float s_val;
       int id=0;
       System.out.println(\"Enter ID: \");
       if(reader.hasNext())
       id = reader.nextInt();
      
       if(h.containsKey(id) || h.size() >= 20)
           System.out.println(\"Array is full or contains the given ID\");
       else {
          
           System.out.println(\"Enter Sales Value: \");
           s_val = reader.nextInt();
          
           h.put(id,new Salesperson(s_val,id));
       }
       reader.close();

   }
  
   void delete(){
      
       Scanner reader = new Scanner(System.in);
      
       System.out.println(\"Enter ID:\ \");
       int id = reader.nextInt();
      
       if(!h.containsKey(id))
           System.out.println(\"Array does not contains the given ID\");
       else {
           h.remove(id);
       }
       reader.close();
   }
  
   void modify(){
      
       Scanner reader = new Scanner(System.in);
      
       System.out.println(\"Enter ID:\ \");
       int id = reader.nextInt();
      
       if(!h.containsKey(id))
           System.out.println(\"Array does not contains the given ID\");
       else {
           Salesperson s = h.get(id);
          
           System.out.println(\"Enter new Sales Value:\ \");
           float s_val = reader.nextInt();
          
           s.setSale_value(s_val);
          
       }
       reader.close();
      
   }

}

//------------------------------------------------------------------------------------

package com.Sales;

public class Salesperson {
  
   float sale_value;
   int id;
  
   public Salesperson(float sale_value, int id) {
       super();
       this.sale_value = sale_value;
       this.id = id;
   }
   public float getSale_value() {
       return sale_value;
   }
   public void setSale_value(float sale_value) {
       this.sale_value = sale_value;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
  
  

}

Since the solution for this particular exercise has not been posted, I was just wondering if I could receive some help with formulating the code for this partic
Since the solution for this particular exercise has not been posted, I was just wondering if I could receive some help with formulating the code for this partic
Since the solution for this particular exercise has not been posted, I was just wondering if I could receive some help with formulating the code for this partic

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site