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);
}
}
}
}
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
Here is everything I have on my codes, the compiler does not allow me to input...
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...
This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw { static Scanner sc = new Scanner (System.in); public static void main(String[] args) throws IOException { PrintWriter outputFile = new PrintWriter ("output.txt"); outputFile.println("hello"); final int MAX_NUM = 10;...
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 = 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();...
Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...
Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...
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(); ...
Need help debugging. Create an application that keeps track of the items that a wizard can carry. console application which has no errors: import java.util.Scanner; public class Console { private static Scanner sc = new Scanner(System.in); public static String getString(String prompt) { System.out.print(prompt); String s = sc.nextLine(); return s; } public static int getInt(String prompt) { int i = 0; boolean isValid = false; while (!isValid) { System.out.print(prompt);...
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 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);...