diff --git a/foreksempel/src/main/java/of2/lf/Car.java b/foreksempel/src/main/java/of2/lf/Car.java new file mode 100644 index 0000000000000000000000000000000000000000..0d6aba71c04f63e0b3aadecf7cba9c16fd01076d --- /dev/null +++ b/foreksempel/src/main/java/of2/lf/Car.java @@ -0,0 +1,41 @@ +package of2.lf; + +public class Car { + + // Felter / intern tilstand: + private String model; + private String brand; + private String regNum; + private int productionYear; + private double weight; + private double kmDriven; + + // Konstruktører: + public Car(String model, String brand, String regNum, int productionYear, double weight) { + this.model = model; + this.brand = brand; + this.regNum = regNum; + this.productionYear = productionYear; + this.weight = weight; + this.kmDriven = 0; + } + + // Konstruktører: + public Car(String model, String brand, String regNum, int productionYear, double weight, double kmDriven) { + this(model, brand, regNum, productionYear, weight); + this.kmDriven = kmDriven; + } + + @Override + public String toString() { + return "Car [brand=" + brand + ", kmDriven=" + kmDriven + ", model=" + model + ", productionYear=" + + productionYear + ", regNum=" + regNum + ", weight=" + weight + "]"; + } + + public static void main(String[] args) { + /* + * // Modell, Produsent, Registeringsnummer, Produksjonsår, Kilometerstand, Vekt + * Car car = new Car("S-MAX", "Ford", "AQ12345", 2019, 356.0); + */ + } +} diff --git a/foreksempel/src/main/java/of2/lf/SelfServiceCheckout.java b/foreksempel/src/main/java/of2/lf/SelfServiceCheckout.java new file mode 100644 index 0000000000000000000000000000000000000000..9edbe9c244524558af6bfc8bee7a1286ef55ba67 --- /dev/null +++ b/foreksempel/src/main/java/of2/lf/SelfServiceCheckout.java @@ -0,0 +1,85 @@ +package of2.lf; + +import java.util.Arrays; +import java.util.Collection; + +public class SelfServiceCheckout { + + // Konstanter - del 1 + public static final Collection<String> days = Arrays.asList("mon", "tue", "wed", "thu", "fri", "sat", "sun"); + + // Felter / Tilstand + private String day; + private String phoneNumber; + + // Konstruktør - del 3 + public SelfServiceCheckout(String day) { + validateDay(day); + this.day = day; + } + + // Registrer medlems-ID (telefonnummer) + public void registerPhoneNumber(String phoneNumer) { + if (this.phoneNumber != null) { + throw new IllegalStateException("Illegal operation"); + } + if (isValidPhoneNumber(phoneNumer)) { + this.phoneNumber = phoneNumer; + } else { + throw new IllegalArgumentException("Not a valid phone number, please re-enter."); + } + } + + // Scanne varer + public void scanItem(String itemName, int amount, double price) { + double rebate = 0; + if (phoneNumber != null && day.equals("thu")) { + rebate = 0.1; + } + System.out.println(amount + "x " + itemName + ": " + (price - (price * rebate)) + " kr"); + } + + // Valideringsmetoder - del 2 + private void validateDay(String day) { + if (!days.contains(day)) { + throw new IllegalArgumentException("Invalid weekday"); + } + } + + private boolean isValidPhoneNumber(String phoneNumber) { + String cleanedPhoneNumber = phoneNumber.replaceAll("\\s", ""); + if (cleanedPhoneNumber.startsWith("00479") || cleanedPhoneNumber.startsWith("00474")) { + if (cleanedPhoneNumber.length() != 12) { + return false; + } + } else if (cleanedPhoneNumber.startsWith("+479") || cleanedPhoneNumber.startsWith("+474")) { + if (cleanedPhoneNumber.length() != 11) { + return false; + } + } else { + return false; + } + + char[] chars = cleanedPhoneNumber.toCharArray(); + for (char c : chars) { + if (!Character.isDigit(c)) { + return false; + } + } + return true; + } + + // main-metode for å teste koden + public static void main(String[] args) { + + SelfServiceCheckout checkout = new SelfServiceCheckout("thu"); + + checkout.scanItem("Norvegia", 2, 120.0); + + checkout.registerPhoneNumber("004742345678"); + + checkout.scanItem("Tomat", 3, 5.0); + + } + +} diff --git a/foreksempel/src/main/java/of3/kode/SelfServiceCheckout.java b/foreksempel/src/main/java/of3/kode/SelfServiceCheckout.java new file mode 100644 index 0000000000000000000000000000000000000000..09935186bc460c7cc8c58c338f4fed194761bd7b --- /dev/null +++ b/foreksempel/src/main/java/of3/kode/SelfServiceCheckout.java @@ -0,0 +1,123 @@ +package of3.kode; + +import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +public class SelfServiceCheckout { + + // Konstanter - del 1 + public static final Collection<String> days = Arrays.asList("mon", "tue", "wed", "thu", "fri", "sat", "sun"); + + // Felter / Tilstand + private String day; + private boolean adminMode; + private String password; + private String phoneNumber; + + // Konstruktør - del 3 + public SelfServiceCheckout(String day, String password) { + validateDay(day); + validatePassword(password); + this.day = day; + this.password = password; + this.adminMode = false; + } + + // Aktiver admin-modus + public void activateAdminMode(String password) { + if (this.adminMode) { + throw new IllegalStateException("Illegal operation"); + } + if (this.password.equals(password)) { + this.adminMode = true; + } else { + throw new IllegalArgumentException("Wrong password, permission denied"); + } + } + + // Registrer medlems-ID (telefonnummer) + public void registerPhoneNumber(String phoneNumer) { + if (this.phoneNumber != null) { + throw new IllegalStateException("Illegal operation"); + } + if (isValidPhoneNumber(phoneNumer)) { + this.phoneNumber = phoneNumer; + } else { + throw new IllegalArgumentException("Not a valid phone number, please re-enter."); + } + } + + // Scanne varer + public void scanItem(String itemName, int amount, double price) { + double rebate = 0; + if (phoneNumber != null) { + rebate = 0.1; + } + System.out.println(amount + "x " + itemName + ": " + (price - (price * rebate)) + " kr"); + } + + // Valideringsmetoder - del 2 + private void validateDay(String day) { + if (!days.contains(day)) { + throw new IllegalArgumentException("Invalid weekday"); + } + } + + private boolean validatePassword(String password) { + if (password.length() < 6 || password.length() > 10) { + return false; + } + char[] chars = password.toCharArray(); + boolean containsNumeric = false; + boolean containsAlphabetic = false; + for (char c : chars) { + if (Character.isDigit(c)) { + containsNumeric = true; + } else if (Character.isAlphabetic(c)) { + containsAlphabetic = true; + } + } + return containsAlphabetic && containsNumeric; + } + + private boolean isValidPhoneNumber(String phoneNumber) { + String cleanedPhoneNumber = phoneNumber.replaceAll("\\s", ""); + if (cleanedPhoneNumber.startsWith("00479") || cleanedPhoneNumber.startsWith("00474")) { + if (cleanedPhoneNumber.length() != 12) { + return false; + } + } else if (cleanedPhoneNumber.startsWith("+479") || cleanedPhoneNumber.startsWith("+474")) { + if (cleanedPhoneNumber.length() != 11) { + return false; + } + } else { + return false; + } + + char[] chars = cleanedPhoneNumber.toCharArray(); + for (char c : chars) { + if (!Character.isDigit(c)) { + return false; + } + } + return true; + } + + // main-metode for å teste koden + public static void main(String[] args) { + + SelfServiceCheckout checkout = new SelfServiceCheckout("thu", "passord123"); + + checkout.scanItem("Norvegia", 2, 120.0); + + checkout.registerPhoneNumber("004742345678"); + + checkout.scanItem("Tomat", 3, 5.0); + + checkout.activateAdminMode("passord123"); + + } + +}