Skip to content

Commit

Permalink
S.R.P
Browse files Browse the repository at this point in the history
  • Loading branch information
dmc0001 authored Sep 23, 2022
1 parent 5974029 commit 5e9ef9e
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 47 deletions.
48 changes: 1 addition & 47 deletions SingleResponsibilityPrinciple/src/App.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,5 @@
import java.util.ArrayList;
import java.util.List;

public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello!");

Stock( new Item("lolodmc69", 1000, 2));
Stock( new Item("jeo9", 1000, 2));
System.out.println("Hello, World!");
}
}
class Item {
String code;
double price;
double quantity;

public Item(String code, double price, double quantity) {
this.code = code;
this.price = price;
this.quantity = quantity;
}

}

public static class Stock{
public Stock( List<Item> items){
items.add(new Item(code, price, quantity));
items
}
public boolean isAvailableQuantity(Item x){
if (items.equals(x.code) && Items.quantity < x.quantity) {
return true;
}
return false;
}
}

class ShopingCart{
public ShopingCart( List<Item> cartItems){
cartItems.add(new Item(code, price, quantity));
}
public void removeFromCart(Item item){
if (cartItems.contains(item)) {
cartItems.remove(item);
}
}

public boolean add(Item item){
return true;
}
}
16 changes: 16 additions & 0 deletions SingleResponsibilityPrinciple/src/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Book {
String name;
String authorName;
int year;
int price;
String isbn;

public Book(String name, String authorName, int year, int price, String isbn) {
this.name = name;
this.authorName = authorName;
this.year = year;
this.price = price;
this.isbn = isbn;
}

}
62 changes: 62 additions & 0 deletions SingleResponsibilityPrinciple/src/InvoiceBestPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
public class InvoiceBestPractice {
private Book book;
private int quantity;
private double discountRate;
private double taxRate;
private double total;

public InvoiceBestPractice(Book book, int quantity, double discountRate, double taxRate, double total) {
this.book = book;
this.quantity = quantity;
this.discountRate = discountRate;
this.taxRate = taxRate;
this.total = total;
}

public double calculateTotal() {
double price = ((book.price - book.price * discountRate) * this.quantity);
double priceWithTaxes = price * (1 + taxRate);
return priceWithTaxes;
}

public Book getBook() {
return this.book;
}

public void setBook(Book book) {
this.book = book;
}

public int getQuantity() {
return this.quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getDiscountRate() {
return this.discountRate;
}

public void setDiscountRate(double discountRate) {
this.discountRate = discountRate;
}

public double getTaxRate() {
return this.taxRate;
}

public void setTaxRate(double taxRate) {
this.taxRate = taxRate;
}

public double getTotal() {
return this.total;
}

public void setTotal(double total) {
this.total = total;
}

}
9 changes: 9 additions & 0 deletions SingleResponsibilityPrinciple/src/InvoicePersistence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class InvoicePersistence {
InvoiceBestPractice invoice;
public InvoicePersistence(InvoiceBestPractice invoice){
this.invoice = invoice;
}
public void saveToFile(String fileName){
//Creates a file with given name and writes the invoice
}
}
12 changes: 12 additions & 0 deletions SingleResponsibilityPrinciple/src/InvoicePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class InvoicePrinter {
InvoiceBestPractice invoice;
public void InvoicePrinter(InvoiceBestPractice invoice){
this.invoice = invoice;
}
public void print(){
System.out.println(invoice.getQuantity() +" x "+invoice.getBook().name+" "+invoice.getBook().price+" $ ");
System.out.println("Discount Rate :" + invoice.getDiscountRate());
System.out.println("Tax Rate :" + invoice.getTaxRate());
System.out.println("Total :"+ invoice.getTotal());
}
}
32 changes: 32 additions & 0 deletions SingleResponsibilityPrinciple/src/InvoiceWorsePractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class InvoiceWorsePractice {
// WORSE PRACTICE
private Book book;
private int quantity;
private double discountRate;
private double taxRate;
private double total;


public InvoiceWorsePractice(Book book, int quantity, double discountRate, double taxRate, double total) {
this.book = book;
this.quantity = quantity;
this.discountRate = discountRate;
this.taxRate = taxRate;
this.total = total;
}

public double calculateTotal(){
double price = ((book.price - book.price*discountRate)*this.quantity);
double priceWithTaxes = price * (1 + taxRate);
return priceWithTaxes;
}
public void printInvoice(){
System.out.println(quantity+" x "+book.name+" "+book.price+" $ ");
System.out.println("Discount Rate :" + discountRate);
System.out.println("Tax Rate :" + taxRate);
System.out.println("Total :"+ total);
}
public void saveToFile(String fileName){
//Creates a file with given name and writes the invoice
}
}

0 comments on commit 5e9ef9e

Please sign in to comment.