0% found this document useful (0 votes)
14 views

Assignment 1

The Invoice class models invoice objects with fields for part number, description, quantity, and price per item. It includes validation for quantity and price, and a method to calculate the invoice amount. The InvoiceTest class demonstrates creating an Invoice, updating fields, and displaying details. The Employee class models employee objects with fields for name, salary, and methods for yearly salary calculation and raises. The EmployeeTest class demonstrates creating Employees, displaying yearly salaries, and applying raises.

Uploaded by

takhan2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment 1

The Invoice class models invoice objects with fields for part number, description, quantity, and price per item. It includes validation for quantity and price, and a method to calculate the invoice amount. The InvoiceTest class demonstrates creating an Invoice, updating fields, and displaying details. The Employee class models employee objects with fields for name, salary, and methods for yearly salary calculation and raises. The EmployeeTest class demonstrates creating Employees, displaying yearly salaries, and applying raises.

Uploaded by

takhan2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 1:

public class Invoice {


// Instance variables
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;

// Constructor
public Invoice(String partNumber, String partDescription, int quantity,
double pricePerItem) {
this.partNumber = partNumber;
this.partDescription = partDescription;
setQuantity(quantity); // Validate and set quantity
setPricePerItem(pricePerItem); // Validate and set price per item
}

// Setter and getter methods for partNumber


public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}

public String getPartNumber() {


return partNumber;
}

// Setter and getter methods for partDescription


public void setPartDescription(String partDescription) {
this.partDescription = partDescription;
}

public String getPartDescription() {


return partDescription;
}

// Setter and getter methods for quantity


public void setQuantity(int quantity) {
// If quantity is not positive, set it to 0
this.quantity = (quantity > 0) ? quantity : 0;
}

public int getQuantity() {


return quantity;
}

// Setter and getter methods for pricePerItem


public void setPricePerItem(double pricePerItem) {
// If pricePerItem is not positive, set it to 0.0
this.pricePerItem = (pricePerItem > 0) ? pricePerItem : 0.0;
}

public double getPricePerItem() {


return pricePerItem;
}

// Method to calculate the invoice amount


public double getInvoiceAmount() {
return quantity * pricePerItem;
}
}

public class InvoiceTest {


public static void main(String[] args) {
// Create an instance of the Invoice class
Invoice invoice1 = new Invoice("1756", "Power Drill", 10, 12.99);

// Display initial invoice information


System.out.println("Initial Invoice Information:");
displayInvoiceDetails(invoice1);

// Update quantity and price per item


invoice1.setQuantity(8);
invoice1.setPricePerItem(12.49);

// Display updated invoice information


System.out.println("\nUpdated Invoice Information:");
displayInvoiceDetails(invoice1);
}

// Helper method to display invoice details


private static void displayInvoiceDetails(Invoice invoice) {
System.out.println("Part Number: " + invoice.getPartNumber());
System.out.println("Part Description: " + invoice.getPartDescription());
System.out.println("Quantity: " + invoice.getQuantity());
System.out.println("Price Per Item: $" + invoice.getPricePerItem());
System.out.println("Invoice Amount: $" + invoice.getInvoiceAmount());
}
}

Question 2:

public class Employee {


// Instance variables
private String firstName;
private String lastName;
private double monthlySalary;

// Constructor
public Employee(String firstName, String lastName, double monthlySalary) {
this.firstName = firstName;
this.lastName = lastName;
setMonthlySalary(monthlySalary); // Validate and set monthly salary
}

// Setter and getter methods for firstName


public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {


return firstName;
}

// Setter and getter methods for lastName


public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {


return lastName;
}

// Setter and getter methods for monthlySalary


public void setMonthlySalary(double monthlySalary) {
// If monthlySalary is not positive, do not set its value
this.monthlySalary = (monthlySalary > 0) ? monthlySalary : 0.0;
}

public double getMonthlySalary() {


return monthlySalary;
}

// Method to calculate yearly salary


public double getYearlySalary() {
return monthlySalary * 12;
}

// Method to give a 10% raise


public void applyRaise() {
// Increase monthly salary by 10%
monthlySalary *= 1.10;
}
}
public class EmployeeTest {
public static void main(String[] args) {
// Create two Employee objects
Employee employee1 = new Employee("Jack", "Harlow", 2000.0);
Employee employee2 = new Employee("rory", "Ranger", 4000.0);

// Display each employee's yearly salary


displayYearlySalary(employee1);
displayYearlySalary(employee2);

// Give each employee a 10% raise


employee1.applyRaise();
employee2.applyRaise();

// Display each employee's yearly salary after the raise


System.out.println("\nAfter 10% Raise:");
displayYearlySalary(employee1);
displayYearlySalary(employee2);
}

// Helper method to display yearly salary for an employee


private static void displayYearlySalary(Employee employee) {
System.out.println(employee.getFirstName() + " " +
employee.getLastName() +
"'s Yearly Salary: $" + employee.getYearlySalary());
}
}

You might also like