[POO] aula04 ex1, ex2 added

This commit is contained in:
TiagoRG 2023-03-06 10:25:51 +00:00
parent 639eeba129
commit 1c74cbb006
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 203 additions and 0 deletions

View File

@ -0,0 +1,85 @@
package aula04;
class Product {
private final String name;
private final double price;
private final int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public double getTotalValue() {
return price * quantity;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
}
class CashRegister {
// TODO: completar implementação da classe
private final Product[] products = new Product[5];
public void addProduct(Product p) {
for (int i = 0; i < products.length; i++) {
if (products[i] == null) {
products[i] = p;
break;
}
}
}
public double getTotalValue() {
double total = 0;
for (Product product : products) {
if (product != null) {
total += product.getTotalValue();
}
}
return total;
}
public String toString() {
String result = String.format("%-15s %10s %10s %7s\n", "Product", "Price", "Quantity", "Total");
for (Product product : products) {
if (product != null) {
result += String.format("%-15s %10.2f %10d %7.2f\n", product.getName(), product.getPrice(), product.getQuantity(), product.getTotalValue());
}
}
result += String.format("%s %.2f\n", "Total value: ", getTotalValue());
return result;
}
}
public class CashRegisterDemo {
public static void main(String[] args) {
// Cria e adiciona 5 produtos
CashRegister cr = new CashRegister();
cr.addProduct(new Product("Book", 9.99, 3));
cr.addProduct(new Product("Pen", 1.99, 10));
cr.addProduct(new Product("Headphones", 29.99, 2));
cr.addProduct(new Product("Notebook", 19.99, 5));
cr.addProduct(new Product("Phone case", 5.99, 1));
// TODO: Listar o conteúdo e valor total
System.out.println(cr);
}
}

View File

@ -0,0 +1,118 @@
package aula04;
class Circle {
public double radius;
public Circle(double radius) {
if (radius <= 0)
return;
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public boolean equals(Circle c2) {
return this.radius == c2.radius;
}
public String toString() {
return "Circle with radius " + this.radius;
}
public double getArea() {
return Math.PI * Math.pow(this.radius, 2);
}
public double getPerimeter() {
return 2 * Math.PI * this.radius;
}
}
class Triangle {
public double side1;
public double side2;
public double side3;
public Triangle(double side1, double side2, double side3) {
if (side1 <= 0 || side2 <= 0 || side3 <= 0)
return;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double[] getSides() {
return new double[] {this.side1, this.side2, this.side3};
}
public void setSides(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public boolean equals(Triangle c2) {
return this.side1 == c2.side1 && this.side2 == c2.side2 && this.side3 == c2.side3;
}
public String toString() {
return "Triangle with sides " + this.side1 + ", " + this.side2 + ", " + this.side3;
}
public double getArea() {
double p = this.getPerimeter() / 2;
return Math.sqrt(p * (p - this.side1) * (p - this.side2) * (p - this.side3));
}
public double getPerimeter() {
return this.side1 + this.side2 + this.side3;
}
}
class Rectangle {
public double side1;
public double side2;
public Rectangle(double side1, double side2) {
if (side1 <= 0 || side2 <= 0)
return;
this.side1 = side1;
this.side2 = side2;
}
public double[] getSides() {
return new double[] {this.side1, this.side2};
}
public void setSides(double side1, double side2) {
this.side1 = side1;
this.side2 = side2;
}
public boolean equals(Rectangle c2) {
return this.side1 == c2.side1 && this.side2 == c2.side2;
}
public String toString() {
return "Rectangle with sides " + this.side1 + ", " + this.side2;
}
public double getArea() {
return this.side1 * this.side2;
}
public double getPerimeter() {
return 2 * (this.side1 + this.side2);
}
}
public class Shapes {
public static void main(String[] args) {
}
}