Commits

Nick Raptis committed 6c9da6c

Added code from midterm exam, group D

Comments (0)

Files changed (3)

src/axiologisijava_d_1550/DhmiourgosAntikeimenwn.java

+package axiologisijava_d_1550;
+
+import axiologisijava_d_1550.antikeimena.*;
+/**
+ * Midterm Java test. Group D
+ * @author Nick Raptis <airscorp@gmail.com>
+ */
+public class DhmiourgosAntikeimenwn {
+
+    /**
+     * @param args the command line arguments
+     */
+    public static void main(String[] args) {
+        Cart carts[] = new Cart[10];
+        //load carts
+        for (int i=0; i<carts.length; i++)
+        {
+            carts[i] = new Cart("Kwstas");
+        }
+
+        //Iterate through all carts
+        int sum=0;
+        for (int i=0; i<carts.length; i++)
+        {
+
+            System.out.println(
+                    carts[i].getName() +
+                    "'s Cart. Total price :" //not grammatically correct but..
+                    + carts[i].getTotalPrice()
+                );
+            sum += carts[i].getTotalPrice();
+        }
+        System.out.println("\nTotal price for ALL the carts: "+sum);
+    }
+
+}

src/axiologisijava_d_1550/antikeimena/Book.java

+package axiologisijava_d_1550.antikeimena;
+
+/**
+ * Should have:
+ * - Title and price properties
+ * - A getPrice method
+ * - If instantiated with just a title, should get a random price
+ *   between 1 and 50
+ * @author Nick Raptis <airscorp@gmail.com>
+ */
+public class Book {
+    String title;
+    int price;
+
+    public Book() {
+    }
+
+    public Book(String title, int price) {
+        this.title = title;
+        this.price = price;
+    }
+
+    /**
+     * Will assign a random price
+     */
+    public Book(String title) {
+        this.title = title;
+        java.util.Random r = new java.util.Random();
+        this.price = r.nextInt(50)+1;
+    }
+
+    public int getPrice() {
+        return price;
+    }
+
+    public void setPrice(int price) {
+        this.price = price;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+}

src/axiologisijava_d_1550/antikeimena/Cart.java

+package axiologisijava_d_1550.antikeimena;
+
+/**
+ * A Cart is a container of 3 Books
+ * Should have:
+ * - An owner's name
+ * - a getTotalPrice method
+ * - If instantiated with just a name, it should contain 3 books
+ * @author Nick Raptis <airscorp@gmail.com>
+ */
+public class Cart {
+    String name;
+    Book books[];
+
+    public Cart() {
+        books = new Book[3];
+    }
+
+    public Cart(String name, Book[] books) {
+        this.name = name;
+        this.books = books;
+    }
+
+    /**
+     * Will insert a set of predefined books
+     */
+    public Cart(String name) {
+        this();
+        this.name = name;
+        books[0] = new Book("Book of Java");
+        books[1] = new Book("Java Essentials");
+        books[2] = new Book("Java for Dummies");
+    }
+
+    public Book[] getBooks() {
+        return books;
+    }
+
+    public void setBooks(Book[] books) {
+        this.books = books;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getTotalPrice()
+    {
+        int sum=0;
+        for (int i=0; i<books.length; i++)
+        {
+            try {
+                sum += books[i].getPrice();
+            } catch(java.lang.NullPointerException e) {
+                //No more books to iterate
+                break;
+            }
+        }
+        return sum;
+    }
+
+}