Hi I am struggling with the code for exercise 5 on chapter 9

Hi I am struggling with the code for exercise 5 on chapter 9 of the book Java Programming 8th edition. I need a little help setting up the add, delete, and error message. I have the Salesperson class but I need a little help setting up the main class.

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 optoins: to add a record to the database, to delete a record from the database, or to change a record in the database. The 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 calue 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.

After each option executes, display the updated databse in ascending order by Salesperson ID number and prompt the user to select the next action. Save the application as SalespersonDatabase.

/*
* Creating the SalesPerson class
*/
package ctb;

/**
*
* @author ctb
*/
public class SalesPerson {
//Declaring private variables that will be used
private int ID;
private double annualSales;
private String name;

  
//Constructor: Set ID and Annual sales
public void SalesPerson(String name,int ID, double annualSales)
{
this.name=name;
this.ID=ID;
this.annualSales=annualSales;
}
//Sets name value
public void setName(String name)
{
this.name=name;
}
//Sets ID value
public void setID(int ID)
{
this.ID=ID;
}
//Sets sales value
public void setSales(double annualSales)
{
this.annualSales=annualSales;
}
//Returns name
public String getName()
{
return name;
}
//Returns ID number
public int getID()
{
return ID;
}
//Returns annual sales
public double getSales()
{
return annualSales;
}

}

Solution


public class SalesPerson implements Comparable<SalesPerson> {
   // Declaring private variables that will be used
   private int ID;
   private double annualSales;
   private String name;

   public void SalesPerson(String name, int ID, double annualSales) {
       this.name = name;
       this.ID = ID;
       this.annualSales = annualSales;
   }

   // Sets name value
   public void setName(String name) {
       this.name = name;
   }

   // Sets ID value
   public void setID(int ID) {
       this.ID = ID;
   }

   // Sets sales value
   public void setSales(double annualSales) {
       this.annualSales = annualSales;
   }

   // Returns name
   public String getName() {
       return name;
   }

   // Returns ID number
   public int getID() {
       return ID;
   }

   // Returns annual sales
   public double getSales() {
       return annualSales;
   }

   @Override
   public int compareTo(SalesPerson o) {
       // return ((Integer).age).compareTo(d1.age);
       int result = this.ID - o.getID();
       return result;

   }

}

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Arrays;

public class SalespersonDatabase {

   public static void main(String[] args) throws NumberFormatException, IOException {
       DataInputStream dis = new DataInputStream(System.in);
       boolean flag = true;
       int counter = -1;
       SalesPerson salesPerson[] = new SalesPerson[20];
       while (flag) {
           System.out.println(\"please select the option\");
           System.out.println(\"1. add the user\");
           System.out.println(\"2. delete user\");
           System.out.println(\"3. change user details\");
           System.out.println(\"4. exit the flow\");
           int value = Integer.parseInt(dis.readLine());
           switch (value) {
           case 1:
               if (counter >= 19) {
                   System.out.println(\"error: data base full\");
               } else {
                   System.out.println(\"input id value\");
                   int id = Integer.parseInt(dis.readLine());
                   boolean flag1 = false;
                   for (int i = 0; i < counter; i++) {
                       if (salesPerson[i].getID() == id) {
                           System.out.println(\"error: id already exists\");
                           flag1 = true;
                           break;
                       }
                   }
                   if (flag1 == false) {
                       System.out.println(\"enter name\");
                       String name = dis.readLine();
                       System.out.println(\"enter sales values\");
                       double sale = Double.parseDouble(dis.readLine());
                       salesPerson[++counter] = new SalesPerson();
                       salesPerson[counter].SalesPerson(name, id, sale);

                   }
               }
               Arrays.sort(salesPerson, 0, counter);

               display(salesPerson, counter);

               break;
           case 2:
               if (counter < 0) {
                   System.out.println(\"error: data base empty\");
               } else {
                   System.out.println(\"input id value\");
                   int id = Integer.parseInt(dis.readLine());
                   boolean flag1 = false;
                   for (int i = 0; i <= counter && counter > -1; i++) {
                       if (salesPerson[i].getID() == id) {
                           salesPerson[i].setID(-1);
                           flag1 = true;
                           Arrays.sort(salesPerson, 0, counter);
                           break;
                       }
                   }
                   if (flag1 == false) {
                       System.out.println(\"error: id doesn\'t exists\");
                   }
               }
               if (counter >= 0) {
                   Arrays.sort(salesPerson, 0, counter);

                   display(salesPerson, counter);
               }
               break;
           case 3:
               if (counter < 0) {
                   System.out.println(\"error: data base empty\");
               } else {
                   System.out.println(\"input id value\");
                   int id = Integer.parseInt(dis.readLine());
                   boolean flag1 = false;
                   for (int i = 0; i <= counter && counter > -1; i++) {
                       if (salesPerson[i].getID() == id) {
                           System.out.println(\"enter the new sale value\");
                           double sale = Double.parseDouble(dis.readLine());
                           salesPerson[i].setSales(sale);
                           flag1 = true;
                           break;
                       } else {
                           System.out.println(salesPerson[i].getID());
                       }
                   }
                   if (flag1 == false) {
                       System.out.println(\"error: id doesn\'t exists\");
                   }
               }
               if (counter >= 0) {
                   Arrays.sort(salesPerson, 0, counter);

                   display(salesPerson, counter);
               }
               break;
           case 4:
               flag = false;
               break;
           default:
               System.out.println(\"selected wrong input\");
               break;
           }

       }

   }

   public static void display(SalesPerson salesPerson[], int count) {

       if (count > -1)
           Arrays.sort(salesPerson, 0, count+1);
       for (int i = 0; i <= count; i++) {
           if (salesPerson[i].getID() == -1) {
               continue;
           } else {
               System.out.println(\"id: \" + salesPerson[i].getID() + \"\\t name: \" + salesPerson[i].getName()
                       + \"\\t sales: \" + salesPerson[i].getSales());
           }
       }
   }

}

Hi I am struggling with the code for exercise 5 on chapter 9 of the book Java Programming 8th edition. I need a little help setting up the add, delete, and erro
Hi I am struggling with the code for exercise 5 on chapter 9 of the book Java Programming 8th edition. I need a little help setting up the add, delete, and erro
Hi I am struggling with the code for exercise 5 on chapter 9 of the book Java Programming 8th edition. I need a little help setting up the add, delete, and erro
Hi I am struggling with the code for exercise 5 on chapter 9 of the book Java Programming 8th edition. I need a little help setting up the add, delete, and erro
Hi I am struggling with the code for exercise 5 on chapter 9 of the book Java Programming 8th edition. I need a little help setting up the add, delete, and erro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site