I NEED HELP WITH THIS QUESTION PROGRAMMING LANGUAGE : JAVA A class hierarchy wil
ID: 3817520 • Letter: I
Question
I NEED HELP WITH THIS QUESTION
PROGRAMMING 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 EExplanation / Answer
Here is the code for the question with comments. Completed part 1 to 6 in the question. Due to time limit and length of this exercise, could not complete 7th. Please do rate the answer if it helped. Thank you very much.
Customer.java
public class Customer {
int CustomerID;
String Name, SurName;
//unparametrized constructor
public Customer()
{
CustomerID=0;
Name=SurName="none";
}
//parameterized constructor with all field values
public Customer(int id,String name,String surname)
{
CustomerID=id;
Name=name;
SurName=surname;
}
//copy constructor
public Customer(Customer cust)
{
CustomerID = cust.CustomerID;
Name=cust.Name;
SurName=cust.SurName;
}
public String toString()
{
return "Id: "+CustomerID+" Name: "+Name+" Surname: "+SurName;
}
//getter and setters
public int getCustomerID() {
return CustomerID;
}
public void setCustomerID(int customerID) {
CustomerID = customerID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSurName() {
return SurName;
}
public void setSurName(String surName) {
SurName = surName;
}
}
NationalCustomer.java
public class NationalCustomer extends Customer {
int licence;
String occupation;
//Default constructor
NationalCustomer()
{
super(); //call base class constructor
licence=0;
occupation="none";
}
NationalCustomer(int id,String name, String surname, int lic, String occup)
{
super(id,name,surname);//call the base class constructor
licence=lic;
occupation=occup;
}
NationalCustomer(NationalCustomer customer)
{
super(customer); //call base class constructor
licence=customer.licence;
occupation=customer.occupation;
}
public String toString()
{
return super.toString()+" Licence: "+licence+" Occupation: "+occupation;
}
public int getLicence() {
return licence;
}
public void setLicence(int licence) {
this.licence = licence;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
}
InternationalCustomer.java
public class InternationalCustomer extends Customer{
String country;
String city;
//Default constructor
InternationalCustomer()
{
super(); //call base class constructor
country="none";
city="none";
}
InternationalCustomer(int id,String name, String surname, String country1,String city1)
{
super(id,name,surname);//call the base class constructor
country=country1;
city=city1;
}
InternationalCustomer(InternationalCustomer customer)
{
super(customer); //call base class constructor
country=customer.country;
city=customer.city;
}
public String toString()
{
return super.toString()+" Country: "+country+" City: "+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;
}
}
ProductRatings.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ProductRatings {
int customerRatings[][];
ArrayList<Customer> customers;
String productNames[];
String filename;
public ProductRatings(String fname)
{
filename=fname;
}
//load the file specified into the objects
public void loadFile() throws FileNotFoundException
{
ArrayList<int[]> ratingslist=new ArrayList<int[]>();
Scanner scanner=new Scanner(new File(filename));
scanner.useDelimiter("[, ]"); //use comma and newline as delimiters
int numOfProducts=scanner.nextInt(); //fist int in the file is number of products
productNames=new String[numOfProducts];
for(int i=0;i<numOfProducts;i++) //now read all product names
{
productNames[i]=scanner.next();
customers=new ArrayList<Customer>();
}
Customer customer;
String custType;
//now start reading customer data
while(scanner.hasNext())
{
int ratings[]=new int[numOfProducts];
custType=scanner.next();
if(custType.equals("n")) //if customer type is national
customer=new NationalCustomer();
else
customer=new InternationalCustomer();
customer.setCustomerID(scanner.nextInt()); //read all customer data one by one
customer.setName(scanner.next());
customer.setSurName(scanner.next());
//System.out.println(customer.getName());
if(custType.equals("n"))
{
((NationalCustomer)customer).setLicence(scanner.nextInt());
((NationalCustomer)customer).setOccupation(scanner.next());
}
else
{
((InternationalCustomer)customer).setCountry(scanner.next());
((InternationalCustomer)customer).setCity(scanner.next());
}
customers.add(customer); //add the cusstomer to list
//now read the ratings info for this customer
for(int i =0;i<numOfProducts;i++)
{
ratings[i]=scanner.nextInt();
}
ratingslist.add(ratings);
}
scanner.close();
//update the 2D array of ratings. length/rows is as many as there are customer and number of columns is as there are products
customerRatings=new int[customers.size()][numOfProducts];
for(int i=0;i<customers.size();i++)
customerRatings[i]=ratingslist.get(i);
//displayProductNames();
//displayCustomers();
}
//type=0 all customers
//type =1 only national customers
//type =2 nternational customers
//type=3 national doctors
//function to return average rating for products by customer of type specified
public double[] getAverageRating(int type)
{
int numOfproducts=productNames.length;
double avg[]=new double[numOfproducts];
int n[]=new int[numOfproducts];
boolean shouldCount=false;
for(int i=0;i<customerRatings.length;i++)
{
//for each record we check whether or not that record has to be considered / counted.
//we loook at the type passed and the classtype of the customer object
if(type==0)
shouldCount=true;
else if(type==1 && customers.get(i) instanceof NationalCustomer)
shouldCount=true;
else if(type==2 && customers.get(i) instanceof InternationalCustomer)
shouldCount=true;
else if(type==3 && customers.get(i) instanceof NationalCustomer &&
((NationalCustomer)customers.get(i)).getOccupation().equals("Doctor"))
shouldCount=true;
else
shouldCount=false;
if(shouldCount)//only if we should consider this customer ...
{
for(int j=0;j<numOfproducts;j++) //add to avg and increase the number of records counted
{
avg[j] += customerRatings[i][j];
n[j]++;
}
}
}
//till now avg was actually holding the sum of all ratings in that category, now we divide it by
//number of records to actualy get the average.
for(int j=0;j<numOfproducts;j++)
avg[j]/=n[j];
return avg;
}
//type=0 national
//type=1 international
//prints customers of specified type who have rated a product below its average rating.
public void printBelowAverageCustomers(int type)
{
int numOfprodcuts=productNames.length;
double avg[]=getAverageRating(0); //first get average rating by all customers
String typeName[]={"National","International"}; //names for different type of customers
Customer c;
for(int i=0;i<numOfprodcuts;i++)
{
System.err.println("Average rating for Product "+productNames[i]+" is "+String.format("%.2f", avg[i]));
System.out.println("List of "+typeName[type]+" customers who have rated below average for this product is :");
for(int j=0;j<customers.size();j++)
{
c=customers.get(j);
if(
((type==0 && c instanceof NationalCustomer) || (type==1 && c instanceof InternationalCustomer))
&& customerRatings[j][i] < avg[i]) //check if the customer if of specified type and his rating is below avg of product
System.out.println(c + " Rating: "+customerRatings[j][i]);
}
System.out.println("____________________________________________");
}
}
//
public void displayProductNames()
{
System.out.println(" ");
for(int i=0;i<productNames.length;i++)
System.out.println(productNames[i]+" ");
}
public void displayCustomers()
{
for(int i=0;i<customers.size();i++)
{
System.out.println(customers.get(i));
}
}
//return the ith product name
public String getProductName(int i)
{
return productNames[i];
}
public static void main(String[] args) {
String filename="ratings.txt";
ProductRatings ratingmgr=new ProductRatings(filename);
try {
ratingmgr.loadFile();
String typeName[]={"all","National","International","National doctor"};
for(int i=0;i<typeName.length;i++)
{
System.out.println("Average rating of products by "+typeName[i]+" customers");
double avg[]=ratingmgr.getAverageRating(i);
for(int j=0;j<avg.length;j++)
{
System.out.println("Product "+ratingmgr.getProductName(j)+": "+String.format("%.2f",avg[j]));
}
}
ratingmgr.printBelowAverageCustomers(0);
ratingmgr.printBelowAverageCustomers(1);
} catch (FileNotFoundException e) {
System.out.println("File not found : "+filename);
}
}
}
Sample input file ratings.txt
5,A,B,C,D,E
n,101,Mary,Jane,32,Computer Engineer
3,4,3,5,1
i,201,John,Smith,US,New Jersey
5,2,1,4,3
n,102,Alex,Morgan,35,Doctor
4,3,4,4,2
n,103,Emma,Roberts,06,Nurse
3,2,4,3,2
i,202,Mario,Gomez,Germany,WolfsBurg
4,1,3,2,1
Sample output
Average rating of products by all customers
Product A: 3.80
Product B: 2.40
Product C: 3.00
Product D: 3.60
Product E: 1.80
Average rating of products by National customers
Product A: 3.33
Product B: 3.00
Product C: 3.67
Product D: 4.00
Product E: 1.67
Average rating of products by International customers
Product A: 4.50
Product B: 1.50
Product C: 2.00
Product D: 3.00
Product E: 2.00
Average rating of products by National doctor customers
Product A: 4.00
Product B: 3.00
Product C: 4.00
Product D: 4.00
Product E: 2.00
Average rating for Product A is 3.80
List of National customers who have rated below average for this product is :
Id: 101 Name: Mary Surname: Jane Licence: 32 Occupation: Computer Engineer Rating: 3
Id: 103 Name: Emma Surname: Roberts Licence: 6 Occupation: Nurse Rating: 3
____________________________________________
Average rating for Product B is 2.40
List of National customers who have rated below average for this product is :
Id: 103 Name: Emma Surname: Roberts Licence: 6 Occupation: Nurse Rating: 2
____________________________________________
Average rating for Product C is 3.00
List of National customers who have rated below average for this product is :
____________________________________________
Average rating for Product D is 3.60
List of National customers who have rated below average for this product is :
Id: 103 Name: Emma Surname: Roberts Licence: 6 Occupation: Nurse Rating: 3
____________________________________________
Average rating for Product E is 1.80
List of National customers who have rated below average for this product is :
Id: 101 Name: Mary Surname: Jane Licence: 32 Occupation: Computer Engineer Rating: 1
____________________________________________
Average rating for Product A is 3.80
List of International customers who have rated below average for this product is :
____________________________________________
Average rating for Product B is 2.40
List of International customers who have rated below average for this product is :
Id: 201 Name: John Surname: Smith Country: US City: New Jersey Rating: 2
Id: 202 Name: Mario Surname: Gomez Country: Germany City: WolfsBurg Rating: 1
____________________________________________
Average rating for Product C is 3.00
List of International customers who have rated below average for this product is :
Id: 201 Name: John Surname: Smith Country: US City: New Jersey Rating: 1
____________________________________________
Average rating for Product D is 3.60
List of International customers who have rated below average for this product is :
Id: 202 Name: Mario Surname: Gomez Country: Germany City: WolfsBurg Rating: 2
____________________________________________
Average rating for Product E is 1.80
List of International customers who have rated below average for this product is :
Id: 202 Name: Mario Surname: Gomez Country: Germany City: WolfsBurg Rating: 1
____________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.