Модуль: IB DP Paper 2


4. G

The company employs several sales personnel to sell its products to different retailers.
Each branch of the company keeps track of its own sales with a suite of programs that include the two classes SalesPerson and Sales.
 
class SalesPerson // each object contains details of one salesperson
{
private String id;
private Sales[] salesHistory; // details of the different sales
private int count = 0; // number of sales made
//constructor for a new salesperson
public SalesPerson(String id)
{
// code missing
}
// constructor for a salesperson transferred (together with
// their sales details) from another branch
public SalesPerson(String id, Sales[] s, int c)
{
// code missing
}
public int getCount(){return count;}
public String getId() {return id;}
public void setSalesHistory(Sales s)
{
salesHistory[count] = s;
count = count +1;
}
public double calcTotalSales() // calculates total sales for the
// salesperson
{
// code missing
}
public Sales largestSale() // calculates the sale with the largest
// value
{
// code missing
}
}

(g) Construct the method addSales(Sales s, String id), in the Main class, that will add a new Sales object s, to the salesperson with a specified ID.
Note: You can assume that the ID is a valid one.

Вставьте недостающие фрагменты кода
Java
Напишите программу ниже
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class SalesPerson // each object contains details of one salesperson
{
    private String id;
    private Sales[] salesHistory; // details of the different sales
    private int count = 0; // number of sales made

    //constructor for a new salesperson
    public SalesPerson (String id)
    {    


                                
}
// constructor for a salesperson transferred (together with
// their sales details) from another branch
    public SalesPerson(String id, Sales[] s, int c)
     {
// code missing
     }
     public int getCount(){return count;}
     public String getId() {return id;}
     public void setSalesHistory(Sales s)
      {
       salesHistory[count] = s;
       count = count +1;
      }
    public double calcTotalSales() // calculates total sales for the salesperson
    {
// code missing    


                                
}
}
 class Sales // each object contains details of one sale
    {
        private String itemId; // id of the item
        private double value; // the price of one item
        private int quantity; // the number of the items sold
        // constructor missing
        public Sales(String itemid, double value, int quantity)
        {
            this.itemId = itemid;
            this.value = value;
            this.quantity = quantity;
        }
        public double getValue() {return value;}
        public int getQuantity() {return quantity;}
    }    
public class Main
{
    //SalesPerson[] salesPeople;
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int n  = in.nextInt();
        SalesPerson[] salesPeople= new SalesPerson[3];
        salesPeople[0] = new SalesPerson("100");
        salesPeople[1] = new SalesPerson("101");
        salesPeople[2] = new SalesPerson("102");
        for(int i = 0;i<n;i++)
        {
            int id_person  = in.nextInt();
            int id_sale  = in.nextInt();
            int value = in.nextInt();
            int quantity = in.nextInt();
            addSales(salesPeople, new Sales(Integer.toString(id_sale), (double) value, quantity ), Integer.toString(id_person));
        }

        System.out.println(salesPeople[2].getId());
        System.out.println(salesPeople[0].getCount());
        System.out.println(salesPeople[0].calcTotalSales());
        System.out.println(highest(salesPeople));
    }
    static String highest(SalesPerson[] SalesPeople)
    {

        double total = 0;
        int index = -1;
        for (int x=0; x<SalesPeople.length; x++)
        {
            if (SalesPeople[x].calcTotalSales() > total)
            {
                total = SalesPeople[x].calcTotalSales();
                index = x;
            }
        }
        return SalesPeople[index].getId();
    }    
static void addSales(SalesPerson[] salesPeople, Sales s, String id)
    {    
    }
}