Question

Here is everything I have on my codes, the compiler does not allow me to input...

Here is everything I have on my codes, the compiler does not allow me to input my name, it will print out "Please enter your name: " but totally ignores it and does not allow me to input my name and just proceeds to my result of ID and Sale. Please help. I've bolded the part where it I should be able to input my name


package salesperson;

public class Salesperson
{
String Names;
int ID;
double Sales;

// craeting set method which I can transfer data into it later
public void setNames(String Names)
{
this.Names=Names;
}

// craeting set method which I can transfer data into it later
public void setId(int id)
{

this.ID=id;
}

// craeting set method which I can transfer data into it later
public void setSales(double sales)
{
this.Sales=sales;
}

public String getNames()
{
return Names;
}

//Get method that returns sales
public double getSales()
{
return Sales;
}

//Get method that returns id
public int getId()
{
return ID;
}
}

//Application class

package salesperson;
import java.util.Scanner;

public class SalespersonDatabase

{

public static void main(String arg[])

{
//create array which can hold maximum of 20
Salesperson sp[] = new Salesperson[20];
//keep track of the numbers of sales person
int numSalespersons=0;
int choice;
int id;
String Name;
double sale = 0;

Scanner sc =new Scanner(System.in);
while(true)
{
System.out.println("1.Add a record.");
System.out.println("2.Change a record.");
System.out.println("3.Quit.");

  
System.out.print("Please enter in your choice:");
choice= sc.nextInt();
//if statement to create the record
if (choice ==1)
{
//check to see if salesperson is full
if (numSalespersons>=20)
{
System.out.println("Error: Database is full");
}
//if salessperson is not full
else
{
System.out.print("Please enter your ID: ");
id= sc.nextInt();
//check if the ID had been inputed
boolean a=false;
for(int k=0;k<numSalespersons;k++)
{
if(sp[k].ID==id)
{
a=true;
break;
}
}
//print error if the id exist
if(a==true)
{
System.out.println("Error: ID already exist");
}
else
{
System.out.print("Please enter your Name: ");
Name= sc.nextLine();

  
System.out.print("Please enter your Sales amount: $");
sale= sc.nextDouble();


sp[numSalespersons]=new Salesperson();
sp[numSalespersons].ID=id;
sp[numSalespersons].Sales=sale;
sp[numSalespersons].Names=Name;
numSalespersons++;
}
  
  
  
}
  
}
else if(choice==2)
{
//print error, if it is empty
if (numSalespersons<=0)
{
System.out.println("Error: Database is empty");
}
else
{
System.out.print("Plese enter your ID: ");
id= sc.nextInt();
boolean b=false;
int k;
//check whether the ID exist
for(k=0;k<numSalespersons;k++)
{
if(sp[k].ID==id)
{
b=true;
break;
}
}
//print error message if the ID does not exist
if(b==false)
{
System.out.println("Error: ID does not exist");
}
//If the ID exist then change the record
else
{
System.out.print("Enter new Sales amount: $");
sale= sc.nextDouble();
sp[k].Sales=sale;
}
}
}
else
{
System.out.println("Wrong choice");
}
System.out.println("ID -Sale");
for(int j=0;j<numSalespersons;j++)
{
System.out.println(sp[j].ID+" $"+sp[j].Sales);
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

I have made instance variables as private(Java recommendation).

Also I fixed the code wich accepts name...etc

Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Salesperson.java

public class Salesperson {
   private String Names;
   private int ID;
   private double Sales;

   // craeting set method which I can transfer data into it later
   public void setNames(String Names) {
       this.Names = Names;
   }

   // craeting set method which I can transfer data into it later
   public void setId(int id) {

       this.ID = id;
   }

   // craeting set method which I can transfer data into it later
   public void setSales(double sales) {
       this.Sales = sales;
   }

   public String getNames() {
       return Names;
   }

   // Get method that returns sales
   public double getSales() {
       return Sales;
   }

   // Get method that returns id
   public int getId() {
       return ID;
   }
}

=====================================

// SalespersonDatabase.java

import java.util.Scanner;

public class SalespersonDatabase

{

   public static void main(String arg[])

   {
       // create array which can hold maximum of 20
       Salesperson sp[] = new Salesperson[20];
       // keep track of the numbers of sales person
       int numSalespersons = 0;
       int choice;
       int id;
       String Name;
       double sale = 0;

       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println("\n1.Add a record.");
           System.out.println("2.Change a record.");
           System.out.println("3.Quit.");

           System.out.print("Please enter in your choice:");
           choice = Integer.parseInt(sc.nextLine());
           // if statement to create the record
           if (choice == 1) {
               // check to see if salesperson is full
               if (numSalespersons >= 20) {
                   System.out.println("Error: Database is full");
               }
               // if salessperson is not full
               else {
                   System.out.print("Please enter your ID: ");
                   id = Integer.parseInt(sc.nextLine());
                   // check if the ID had been inputed
                   boolean a = false;
                   for (int k = 0; k < numSalespersons; k++) {
                       if (sp[k].getId() == id) {
                           a = true;
                           break;
                       }
                   }
                   // print error if the id exist
                   if (a == true) {
                       System.out.println("Error: ID already exist");
                   } else {
                      
                       System.out.print("Please enter your Name: ");
                       Name = sc.nextLine();

                       System.out.print("Please enter your Sales amount: $");
                       sale = Double.parseDouble(sc.nextLine());

                       sp[numSalespersons] = new Salesperson();
                       sp[numSalespersons].setId(id);
                       sp[numSalespersons].setSales(sale);
                       sp[numSalespersons].setNames(Name);
                       numSalespersons++;
                   }

               }

           } else if (choice == 2) {
               // print error, if it is empty
               if (numSalespersons <= 0) {
                   System.out.println("Error: Database is empty");
               } else {
                   System.out.print("Plese enter your ID: ");
                   id = Integer.parseInt(sc.nextLine());
                   boolean b = false;
                   int k;
                   // check whether the ID exist
                   for (k = 0; k < numSalespersons; k++) {
                       if (sp[k].getId() == id) {
                           b = true;
                           break;
                       }
                   }
                   // print error message if the ID does not exist
                   if (b == false) {
                       System.out.println("Error: ID does not exist");
                   }
                   // If the ID exist then change the record
                   else {
                       System.out.print("Enter new Sales amount: $");
                       sale = Double.parseDouble(sc.nextLine());
                       sp[k].setSales(sale);
                   }
               }
           }
           else if(choice==3)
           {
               System.out.println(":: PROGRAM EXIT ::");
               break;
           }
           else {
               System.out.println("Wrong choice");
           }
           System.out.println("ID -Sale");
           for (int j = 0; j < numSalespersons; j++) {
               System.out.println(sp[j].getId() + " $" + sp[j].getSales());
           }
       }
   }
}

====================================

output:


1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:1
Please enter your ID: 1234
Please enter your Name: Williams
Please enter your Sales amount: $43000
ID -Sale
1234 $43000.0

1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:1
Please enter your ID: 1234
Error: ID already exist
ID -Sale
1234 $43000.0

1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:1
Please enter your ID: 2345
Please enter your Name: James
Please enter your Sales amount: $56000
ID -Sale
1234 $43000.0
2345 $56000.0

1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:2
Plese enter your ID: 3456
Error: ID does not exist
ID -Sale
1234 $43000.0
2345 $56000.0

1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:2
Plese enter your ID: 2345
Enter new Sales amount: $65000
ID -Sale
1234 $43000.0
2345 $65000.0

1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:5
Wrong choice
ID -Sale
1234 $43000.0
2345 $65000.0

1.Add a record.
2.Change a record.
3.Quit.
Please enter in your choice:3
:: PROGRAM EXIT ::

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Here is everything I have on my codes, the compiler does not allow me to input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • I need this java program to contain methods that do some of the work that can...

    I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. Continue to take input for every employee in a company, and display their information until a sentinel value is entered, that exits the loop and ends the program. import java.util.Scanner; public class SalaryCalc { double Rpay = 0, Opay = 0; void...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT