Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Item class
- ===========
- package myClasses;
- public class Item {
- // object properties
- private String name;
- private double price;
- private int quentity;
- public Item(String name, double price) {
- this.name = name;
- this.price = price;
- this.quentity = 0;
- }
- public Item(String name, double price, int quentity) {
- super();
- this.name = name;
- this.price = price;
- this.quentity = quentity;
- }
- public Item(Item itm) {
- this.name = itm.name;
- this.price = itm.price;
- this.quentity = itm.quentity;
- }
- public String getName() {
- return name;
- }
- public boolean setName(String name) {
- if (name.length() >= 3) {
- this.name = name;
- return true;
- }
- return false;
- }
- public double getPrice() {
- return price;
- }
- public boolean setPrice(double price) {
- if (price >= 0) {
- this.price = price;
- return true;
- }
- return false;
- }
- public int getQuentity() {
- return quentity;
- }
- public boolean setQuentity(int quentity) {
- if (quentity >= 0) {
- this.quentity = quentity;
- return true;
- }
- return false;
- }
- @Override
- public String toString() {
- return "Item [name=" + name + ", price=" + price + ", quentity=" + quentity + "]";
- }
- public double getTotal() {
- return price * quentity;
- }
- }
- ItemList class
- ================
- package myClasses;
- public class ItemList {
- final private int MAX_SIZE = 20;
- private Item[] items;
- private int count;
- public ItemList() {
- items = new Item[MAX_SIZE];
- this.count = 0;
- }
- public boolean addItem(Item item) {
- if (count < items.length) {
- // this.items[count] = item;
- this.items[count] = new Item(item);
- count += 1;
- return true;
- }
- return false;
- }
- public void removeItem(Item item) {
- int p = findPlace(item);
- // if item exists in list
- if (p > -1) {
- for (int i = p + 1; i < count; i += 1) {
- items[i - 1] = items[i];
- }
- count -= 1;
- }
- }
- private int findPlace(Item item) {
- // to be checked later
- for (int i = 0; i < count; i += 1) {
- if (items[i].getName().equals(item.getName())) {
- return i;
- }
- }
- // not found
- System.out.println(item.getName() + " item not found");
- return -1;
- }
- public void removeAll() {
- count = 0;
- }
- public double getTotal() {
- double total = 0;
- for (int i = 0; i < count; i += 1) {
- total += items[i].getTotal();
- }
- return total;
- }
- @Override
- public String toString() {
- String res = "Items List" + "\n---------------------\n";
- for (int i = 0; i < count; i += 1)
- res += items[i] + "\n";
- res += "---------------------\n";
- res += "Total price is " + getTotal() + "\n";
- return res;
- }
- }
- Tester class
- =============
- package Tester;
- import myClasses.Item;
- import myClasses.ItemList;
- public class Testrer {
- public static void main(String[] args) {
- Item choc = new Item("Choclate", 7.90, 3);
- Item bamba = new Item("Bamba", 2.80, 10);
- Item bisli = new Item("Bisli", 1.60, 4);
- ItemList karawanList = new ItemList();
- karawanList.addItem(choc);
- karawanList.addItem(bamba);
- karawanList.addItem(bisli);
- System.out.println(karawanList);
- // remove bamba
- karawanList.removeItem(bamba);
- System.out.println("after removing item: " + karawanList);
- // remove all items
- choc.setPrice(4.20);
- choc.setQuentity(15);
- System.out.println(choc);
- System.out.println(karawanList);
- karawanList.removeAll();
- System.out.println(karawanList);
- ItemList lolo = new ItemList();
- lolo.addItem(choc);
- System.out.println("lolo list is" + lolo);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement