Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED HELP PRGRAMMING LANGUAGE : JAVA A class hierarchy will be created using inh

ID: 3818124 • Letter: N

Question

NEED HELP

PRGRAMMING LANGUAGE : JAVA

A class hierarchy will be created using inheritance to represent clients The "Customer" class should look like this: l-) "CustomerID to represent customer number 2-) "Name" and "Surname" in String type representing first and last name 3-) Unparametric constructor 4-) Constructor that uses all variables 5-) Copy Constructor 6-) toString method 7-) Get and Set methods To represent national customers, derive a class named "National Customcr" with inheritance from the "Customer" class The "National Customer" class should be as follows: l-) "LicenccPlateNumber" in int type to represent the provincial traffic plate code 2-)"Occupation" in String type representing the customer's occupation 3-) Unparametric constructor 4-) Constructor that uses a variables 5-) Copy Constructor 6-) to String method 7-) Get and Set methods To represent international customers, derive a class named "International Customer" with inheritance from the "Customer" class The "International Customer" class should be as follows: 1-"Country" in the String type representing the country 2-)"City" in int type representing the city 3-) Unparametric constructor 4-) Constructor that uses all variables 5-) Copy Constructor 6-0 toString method 7-) Get and Set methods The first linc of thc file contains thc namc of thc product and the product namcs. For examplc, 5 The product is ratcd and the names of these products are given as products A, B, C, D and E The following lines are arranged as follows: customer information first (n: national, i n international meaning), On the bottom line there are grades that the relevant customer has made for the products. A comma-separated-value(CSV) file is a simple text format used to store a list of records. A comma is used a delimiter to separate the fields for each record. This format is commonly used to transfer data between a spreadsheet or database. The first line of the file contains the name of the product and the product names. For example, 5 The product is rated and the names of these products are given as products A, B, C, D and E

Explanation / Answer

package chegg;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class CustProdRating {
   public static void main(String args[]){
       try{
       FileInputStream fis=new FileInputStream("C:\Users\IBM_ADMIN\workspace\testnew\src\chegg\input.txt");
       Scanner sc=new Scanner(fis);
       ArrayList<Customer> lst=new ArrayList<Customer>();
       ArrayList<ArrayList<Integer>> custprd=new ArrayList<ArrayList<Integer>>();
       String prd=sc.next();
       StringTokenizer st=new StringTokenizer(prd,",");
       int nump=0;
       String[] prods=null;
       while(st.hasMoreTokens()){
       nump=Integer.parseInt(st.nextToken());
       prods=new String[nump];
       for(int i=0;i<nump;i++){
           prods[i]=st.nextToken();
       }
       }
       while(sc.hasNext()){
           String cust=sc.next();
           st=new StringTokenizer(cust,",");
           String type=st.nextToken();
           Customer customer=null;
           if(type.equals("n")){
               customer=new NationalCustomer(Integer.parseInt(st.nextToken()),st.nextToken(),st.nextToken(),Integer.parseInt(st.nextToken()),st.nextToken());
              
           }else if(type.equals("i")){
               customer=new InterNationalCustomer(Integer.parseInt(st.nextToken()),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());
           }
           lst.add(customer);
           String rating=sc.next();
           st= new StringTokenizer(rating,",");
           ArrayList<Integer> nlst=new ArrayList<Integer>();
           for(int i=0;i<nump;i++){
               nlst.add(Integer.parseInt(st.nextToken()));              
           }
           custprd.add(nlst);
          
       }
      
       for(Customer c:lst){
           System.out.println(c);
          
       }
       for(ArrayList<Integer> alst:custprd){
           for(int i:alst){
               System.out.print(i+" ");
           }
           System.out.println();
       }
       //calculate avg rating product wise
       double[] avg=new double[nump];
      
       for(ArrayList<Integer> alst:custprd){
           for(int i=0;i<nump;i++){
               avg[i]+=alst.get(i);
           }
       }
       for(int i=0;i<nump;i++){
           avg[i]=avg[i]/lst.size();
           System.out.println("Product "+prods[i]+":"+avg[i]);
       }
      
       //product wise average rating for national customers
       System.out.println("product wise average rating for national customers");
       avg=new double[nump];
       for(int i=0;i<lst.size();i++){
           if(lst.get(i) instanceof NationalCustomer){
               for(int j=0;j<nump;j++){
                   avg[j]+=custprd.get(i).get(j);
               }
           }
       }
       for(int i=0;i<nump;i++){
           //System.out.println(avg[i]);
           avg[i]=avg[i]/lst.size();
           System.out.println("Product "+prods[i]+":"+avg[i]);
       }
      
      
       //product wise average rating for international customers
       System.out.println("product wise average rating for international customers");
       avg=new double[nump];
       for(int i=0;i<lst.size();i++){
           if(lst.get(i) instanceof InterNationalCustomer){
               for(int j=0;j<nump;j++){
                   avg[j]+=custprd.get(i).get(j);
               }
           }
       }
       for(int i=0;i<nump;i++){
           //System.out.println(avg[i]);
           avg[i]=avg[i]/lst.size();
           System.out.println("Product "+prods[i]+":"+avg[i]);
       }
      
       //product wise average rating for national customers who are doctors
       System.out.println("product wise average rating for national customers who are doctors");
       avg=new double[nump];
       for(int i=0;i<lst.size();i++){
           if(lst.get(i) instanceof NationalCustomer){
               NationalCustomer nc=(NationalCustomer)lst.get(i);
               if(nc.getOccupation().equalsIgnoreCase("doctor")){
                   for(int j=0;j<nump;j++){
                       avg[j]+=custprd.get(i).get(j);
                   }
               }
           }
       }
       for(int i=0;i<nump;i++){
           //System.out.println(avg[i]);
           avg[i]=avg[i]/lst.size();
           System.out.println("Product "+prods[i]+":"+avg[i]);
       }
      
       //national customers who scored less than average credit
       System.out.println("national customers who scored less than average credit");
       avg=new double[nump];
       double[] totalavg=new double[nump];
       for(int i=0;i<lst.size();i++){
           for(int k=0;k<nump;k++)
               totalavg[k]+=custprd.get(i).get(k);
       }
       for(int i=0;i<nump;i++){
           //System.out.println(avg[i]);
           totalavg[i]=totalavg[i]/lst.size();
       }
       for(int j=0;j<custprd.size();j++){
               for(int k=0;k<nump;k++){
                   //System.out.println(custprd.get(j).get(k)+ " "+totalavg[k]);
                   if(custprd.get(j).get(k)<totalavg[k]){
                       if(lst.get(j) instanceof NationalCustomer)
                       System.out.println("Customer "+lst.get(j));
                   }
               }
          
       }
      
      
       //international customers who scored less than average credit
       System.out.println("international customers who scored less than average credit");
       avg=new double[nump];
       totalavg=new double[nump];
       for(int i=0;i<lst.size();i++){
           for(int k=0;k<nump;k++)
               totalavg[k]+=custprd.get(i).get(k);
       }
       for(int i=0;i<nump;i++){
           //System.out.println(avg[i]);
           avg[i]=avg[i]/lst.size();
           totalavg[i]=totalavg[i]/lst.size();
       }
           for(int j=0;j<custprd.size();j++){
               for(int k=0;k<nump;k++){
                   if(custprd.get(j).get(k)<totalavg[k]){
                       if(lst.get(j) instanceof InterNationalCustomer)
                       System.out.println("Customer "+lst.get(j));
                   }
               }
          
       }
      
       }catch(Exception e){
           e.printStackTrace();
       }
   }
  
  
}
class Customer{
   private int customerId;
   private String name;
   private String surname;
   public int getCustomerId() {
       return customerId;
   }
   public void setCustomerId(int customerId) {
       this.customerId = customerId;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getSurname() {
       return surname;
   }
   public void setSurname(String surname) {
       this.surname = surname;
   }
   public Customer(){
       this.customerId=-1;
       this.name="";
       this.surname="";
   }
   public Customer(int custId,String name,String surname){
       customerId=custId;
       this.name=name;
       this.surname=surname;
   }
   public Customer(Customer cust){
       this.customerId=cust.customerId;
       this.name=cust.name;
       this.surname=cust.surname;
   }
   public String toString(){
       return customerId+" "+name+" "+surname;
   }
}
class NationalCustomer extends Customer{
   private int licenseNo;
   private String occupation;
   public NationalCustomer(){
       super();
       licenseNo=-1;
       occupation="";
   }
   public NationalCustomer(int custId,String name,String surname, int licNo, String occup){
       super(custId,name,surname);
       this.licenseNo=licNo;
       this.occupation=occup;
   }
   public NationalCustomer(NationalCustomer ncust){
       super(ncust);
       this.licenseNo=ncust.licenseNo;
       this.occupation=ncust.occupation;
      
   }
   public int getLicenseNo() {
       return licenseNo;
   }
   public void setLicenseNo(int licenseNo) {
       this.licenseNo = licenseNo;
   }
   public String getOccupation() {
       return occupation;
   }
   public void setOccupation(String occupation) {
       this.occupation = occupation;
   }
   public String toString(){
       return super.toString()+" "+licenseNo+" "+occupation;
   }
  
}
class InterNationalCustomer extends Customer{
   private String country;
   private String city;
   public InterNationalCustomer(){
       super();
       country="";
       city="";
   }
   public String getCountry() {
       return country;
   }
   public void setCountry(String country) {
       this.country = country;
   }
   public String getCity() {
       return city;
   }
   public void setCity(String city) {
       this.city = city;
   }
   public InterNationalCustomer(int custId,String name,String surname, String country, String city){
       super(custId,name,surname);
       this.country=country;
       this.city=city;
   }
   public InterNationalCustomer(InterNationalCustomer icust){
       super(icust);
       this.country=icust.country;
       this.city=icust.city;
      
   }

   public String toString(){
       return super.toString()+" "+country+" "+city;
   }
  
}