Question

Looking for some help with this Java program I am totally lost on how to go about this.

You have been approached by a local grocery store to write a program that will track the progress of their sales people (SaleAfter each option executes, display the updated database in ascending order by ID number and prompt the user to select the ne

You have been approached by a local grocery store to write a program that will track the progress of their sales people (SalesPerson.java) The information needed are: D Number integer Month 1 Sales Amount-Double Month 2 Sales Amount-Double Month 3 Sales Amount-Double Write a Java program that allows you to store the information of any salesperson up to 20 While the user decides to continue offer 3 options. To add a record to the database To delete a record from the database To change a record in the database 1. 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 have the user input the sales for months 1, 2, and 3 and add a new record to the database. Use a Method to add a new user to the database 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. Use a Method to delete the record and move the remaining records up one row. If the user selects the change option, issue an error if the database is empty. Otherwise prompt the user for an ID number, If the requested record does not exist, issue an error message, Use a Method to edit the sales amount for the requested ID. 2. 3.
After each option executes, display the updated database in ascending order by ID number and prompt the user to select the next action The program should be tested and displayed with the following sales persons. 1993 2504 5441 3442.20 2834.32 1778.33 1409.99 1138.50 1987.13 3010.40 2879.05 3482.10
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The program is written in Java 8 using Eclipse IDE and MySQL for database

Program

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SalesPerson {
   public static void main(String[] args) throws IOException, SQLException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       int choice;
       Connection con = getConnection();
       System.out.println("1. To add record");
       System.out.println("2. To delete record");
       System.out.println("3. To change record");
       System.out.print("Enter your choice: ");
       choice = Integer.parseInt(br.readLine());

       switch (choice) {
       case 1:
           toAdd(con, br);
           break;
       case 2:
           toDelete(con, br);
           break;
       case 3:
           toUpdate(con, br);
           break;
       default:
           System.out.println("Invalid Choice");
           break;
       }
   }
   //updating the database
   private static void toUpdate(Connection con, BufferedReader br)throws SQLException, IOException {
       System.out.print("Enter ID: ");
       int id = Integer.parseInt(br.readLine());
       Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("SELECT * FROM salesPerson where ID = " + id);

       if (rs.next() == true) {
           System.out.print("Enter month 1: ");
           double month1 = Double.parseDouble(br.readLine());
           System.out.print("Enter month 2: ");
           double month2 = Double.parseDouble(br.readLine());
           System.out.print("Enter month 3: ");
           double month3 = Double.parseDouble(br.readLine());

           stmt.executeUpdate("UPDATE salesPerson SET month1="+month1
                   + ", month2 = "+month2
                   + ", month3 = "+month3
                   + "WHERE id="+id);
           showResult(con);
       }
       else
           System.out.println("ID does not exist in the database");
      
   }
   //deleteing data from the database
   private static void toDelete(Connection con, BufferedReader br) throws SQLException, IOException {
       System.out.print("Enter ID: ");
       int id = Integer.parseInt(br.readLine());
       Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("SELECT * FROM salesPerson where ID = " + id);

       if (rs.next() == true) {

           stmt.executeUpdate("Delete From salesPerson Where id = "+id);
           showResult(con);
       }
       else
           System.out.println("ID does not exist in the database");
      
   }
   //inserting data to the database
   private static void toAdd(Connection con, BufferedReader br) throws SQLException, IOException {
       System.out.print("Enter ID: ");
       int id = Integer.parseInt(br.readLine());
       Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("SELECT * FROM salesPerson where ID = " + id);

       if (rs.next() == false) {
           System.out.print("Enter month 1: ");
           double month1 = Double.parseDouble(br.readLine());
           System.out.print("Enter month 2: ");
           double month2 = Double.parseDouble(br.readLine());
           System.out.print("Enter month 3: ");
           double month3 = Double.parseDouble(br.readLine());

           stmt.executeUpdate(
                   "Insert Into salesPerson values (" + id + ", " + month1 + ", " + month2 + ", " + month3 + ")");
           showResult(con);
       }
       else
           System.out.println("ID exists in the database");
   }
   //fecthing data from the database
   private static void showResult(Connection con) throws SQLException {
       System.out.println();
       Statement stmt = con.createStatement(); //for creating statement to fetch and retrieve data from DB
       ResultSet rs = stmt.executeQuery("SELECT * FROM salesPerson");//resultset hold the data
       System.out.println("ID Month 1 Month 2 Month 3");
       while(rs.next()) {
           System.out.print(rs.getString("ID")+" ");//the column name to get the value from that particular column
           System.out.print(rs.getString("month1")+" ");
           System.out.print(rs.getString("month2")+" ");
           System.out.print(rs.getString("month3"));
           System.out.println();
       }
       stmt.close();
      
   }
   //creating connection with the database
   public static Connection getConnection() {
       Connection con = null;
       try {

           Class.forName("com.mysql.cj.jdbc.Driver");// this is the name of the mysql driver
                                                       // Database Driver, server address, port no., database name,
                                                       // username, password
           con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javatest?autoReconnect=true&useSSL=false",
                   "root", "Aman@123");
           // supress the SSL error
           System.out.println("Database Connected Successfully");
       } catch (ClassNotFoundException e) {

           System.err.println("Load Falied: " + e);

       } catch (SQLException e) {
           System.err.println("Connection Falied: " + e);
       }
       return con;
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sale...
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
  • Hello. can anyone solve this. i am having hard time with this. please post a solution...

    Hello. can anyone solve this. i am having hard time with this. please post a solution ASAP a) Create a class named Student. Data fields for Student class include an integer Student ID, a String name, and a double CGPA. Methods include a constructor that requires values for all data fields, as well as get and set methods for each of the data fields. b) Create an application that allows a user to enter values for an array of 15...

  • This program will store a roster of most popular videos with kittens. The roster can include...

    This program will store a roster of most popular videos with kittens. The roster can include at most 10 kittens.You will implement structures to handle kitten information. You will also use functions to manipulate the structure. (1) Create a structure kitten. The structure should contain the following attributes: name; string color; string score; integer Important! The name of the structure and each of its field must match exactly for the program to work and be graded correctly. (2) Create a...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • 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...

  • Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double...

    Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double hashing uses the idea of applying a second hash function to key when a collision occurs.   The software program will be based on the following requirements: Development Environment: If the software program is written in C++, its project must be created using Microsoft Visual Studio 2017. If the software program is written in Java, its project must be created using NetBeans v8.2. Algorithm: If...

  • Can you help us!! Thank you! C++ Write a program that can be used by a...

    Can you help us!! Thank you! C++ Write a program that can be used by a small theater to sell tickets for performances. The program should display a screen that shows which seats are available and which are taken. Here is a list of tasks this program must perform • The theater's auditorium has 15 rows, with 30 seats in each row. A two dimensional array can be used to represent the seats. The seats array can be initialized with...

  • Write a Java program which will store, manipulate, and print student registration information. As part of...

    Write a Java program which will store, manipulate, and print student registration information. As part of the solution, identify the following classes: Student Admissions. The class Student must have the following fields – Name, Address, Id number, Courses, and Date, where: Name is a user defined class comprising of at minimum first name and last name. Address is a user defined class comprising of fields - street, city, state, and zip code. Date is a predefined class in the java.util...

  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

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