PP 7.2 Create a class called SalesPerson that represents a salesperson in an organization. The SalesPerson class should have the name, the phone number, and the assigned district of a salesperson. Each salesperson acquires a certain sale amount every day. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each daily sale amount for a week is set to zero. Provide a method called setDailyAmount that accepts a day of a week as a number (0 for Sunday, 1 for Monday, and so on) and an amount and sets that amount as the daily sale amount. Provide another method called getDailyAmount that accepts a day of a week and returns the amount for that day. Provide a method called total that calculates the total sale amount for the week and a method called average that calculates the average daily sale amount for the week. Write a toString method that prints all the details of the salesperson. Write a driver class to exercise the SalesPerson class.
class SalesPerson {
private String name;
private String phoneNumber;
private String district;
private double[] dailySales;
// Constructor to set all instance values based on parameter values
public SalesPerson(String name, String phoneNumber, String district) {
this.name = name;
this.phoneNumber = phoneNumber;
this.district = district;
this.dailySales = new double[7]; // 7 days in a week
}
// Overloaded constructor to set daily sale amounts for a week to zero
public SalesPerson(String name, String phoneNumber, String district, double[] dailySales) {
this.name = name;
this.phoneNumber = phoneNumber;
this.district = district;
this.dailySales = dailySales;
}
// Method to set daily sale amount for a specific day
public void setDailyAmount(int dayOfWeek, double amount) {
if (dayOfWeek >= 0 && dayOfWeek < 7) {
dailySales[dayOfWeek] = amount;
} else {
System.out.println("Invalid day of the week.");
}
}
// Method to get daily sale amount for a specific day
public double getDailyAmount(int dayOfWeek) {
if (dayOfWeek >= 0 && dayOfWeek < 7) {
return dailySales[dayOfWeek];
} else {
System.out.println("Invalid day of the week.");
return 0.0;
}
}
// Method to calculate total sale amount for the week
public double total() {
double totalSales = 0.0;
for (double sale : dailySales) {
totalSales += sale;
}
return totalSales;
}
// Method to calculate average daily sale amount for the week
public double average() {
return total() / 7;
}
// toString method to print all the details of the salesperson
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Name: ").append(name).append("\n");
sb.append("Phone Number: ").append(phoneNumber).append("\n");
sb.append("District: ").append(district).append("\n");
sb.append("Daily Sales:\n");
for (int i = 0; i < dailySales.length; i++) {
sb.append("Day ").append(i).append(": ").append(dailySales[i]).append("\n");
}
sb.append("Total Sales: ").append(total()).append("\n");
sb.append("Average Daily Sales: ").append(average()).append("\n");
return sb.toString();
}
public String getName() {
// TODO Auto-generated method stub
return this.name = name;
}
}
public class PP7_2 {
public static void main(String[] args) {
double[] dailySales = {100.0, 200.0, 150.0, 300.0, 250.0, 180.0, 210.0};
SalesPerson salesPerson1 = new SalesPerson("John", "123-456-7890", "District A", dailySales);
// Alternatively, you can create an instance using the first constructor
// SalesPerson salesPerson1 = new SalesPerson("John Doe", "123-456-7890", "District A");
// Set daily sale amount for a specific day
salesPerson1.setDailyAmount(2, 175.0);
// Get daily sale amount for a specific day
System.out.println("Sales for day 2: $" + salesPerson1.getDailyAmount(2));
// Print all details of the salesperson
System.out.println(salesPerson1);
}
}
< Console >
Sales for day 2: $175.0
Name: John
Phone Number: 123-456-7890
District: District A
Daily Sales:
Day 0: 100.0
Day 1: 200.0
Day 2: 175.0
Day 3: 300.0
Day 4: 250.0
Day 5: 180.0
Day 6: 210.0
Total Sales: 1415.0
Average Daily Sales: 202.14285714285714
'[JAVA] 개발' 카테고리의 다른 글
[JAVA] JDK 21 설치, 환경 변수 설정 (1) | 2024.10.17 |
---|---|
Java Software Solution Ch7_PP3 (0) | 2024.05.01 |
Java Software Soution Ch7_PP8 (0) | 2024.05.01 |
Java Software Solution Ch11_PP3 ; Exception Class (0) | 2024.04.28 |
Java Software Solution Ch11_ PP1 ; Exception Class (try-catch) (0) | 2024.04.27 |