Sunteți pe pagina 1din 5

LAB ASSIGNMENT # 1

By: Abdul Hannan Malik (FA18-BCS-001)


Question:
Create a class tuck shop with following data members:
String Owner
String Food_Items[100]
Double Price [100]
Int Quantity [100]

Note:
 All arrays are open ended. It is not necessary that they are completely filled.
 All three arrays will work in synchronization with each other i-e the item at
index 0 will have its price in Price array at index 0 and quantity at index 0 of
Quantity array.
Methods:
Two Constructors (default, four-argument)
Set for Owner , get for Owner
Display()
AddFooditem (?) // Adds a food item, its price, and its quantity in the first available free
slot in Food_Items, price and quantity array

Buy (?) //decrement quantity if more than one item is present. If only one item is
present then the item should be removed from Food_Item array.

Note:
Do not create set and get methods for array data members as they cannot be updated
independent of each other.
Runner:
In the runner one object using default constructor.
Initialize its data by using the set functions.
Create another object using argument constructor.
Call AddFoodItem() and buy() methods

CODE:
OBJECT:
package Assignment;

public class tuckShop {

private String owner;


private String[] items = new String[100];
private double[] price = new double[100];
private int[] quantity = new int[100];

public tuckShop() {
owner = "Test";
for (int i = 0; i < 100; i++) {
items[i] = "null";
price[i] = 0;
quantity[i] = 0;
}
}

public tuckShop(String a, String[] b, double[] c, int[] d) {


owner = a;
for (int i = 0; i < b.length; i++) {
items[i] = b[i];
}
for (int i = 0; i < c.length; i++) {
price[i] = c[i];
}
int i = 0;
for (i = 0; i < d.length; i++) {
quantity[i] = d[i];
}
for (int j = i; j < 100; j++) {
items[j] = "null";
}
}

public void setOwner(String a) {


owner = a;
}

public String getOwner() {


return owner;
}

public void display() {


System.out.println("Owner: " + owner);
System.out.printf("%-10s%-9s%-8s\n", "Item", "Quantity", "Price");
for (int i = 0; i < 100; i++) {
if (items[i].equalsIgnoreCase("null")) {
continue;
} else {
System.out.printf("%-10s%-9d%4f\n", items[i], quantity[i], price[i]);
}
}
System.out.println("");
}

public void addItem(String a, double b, int c) {


for (int i = 0; i < items.length; i++) {
if (items[i].equalsIgnoreCase("null")) {
items[i] = a;
price[i] = b;
quantity[i] = c;
break;
}
}
}

public void buy(String name, int amount) {


for (int i = 0; i < 100; i++) {
if (name.equalsIgnoreCase(items[i])) {
if (amount > quantity[i]) {
System.out.println("Invalid Order! Small Number of items available\n");
} else {
quantity[i] -= amount;
System.out.println("Order Successful!\nBill is: " + price[i] * amount + "$\n");
}
if (quantity[i] == 0) {
items[i] = "null";
}

break;
}
}
System.out.println("Item Not Found!\n");
}
}

RUNNER:
package Assignment;
public class tuckShopRun {
public static void main(String[] args){
tuckShop t1 = new tuckShop();
t1.setOwner("Lester");
t1.addItem("Fries", 60, 5);
t1.buy("Apple", 3);
tuckShop t2 = new tuckShop("Twitch", new String[]{"Coffee", "Green Tea"}, new
double[]{50, 20}, new int[]{3, 2});
t2.display();
t2.buy("Green Tea", 2);
t2.display();
}
}

OUTPUT:

S-ar putea să vă placă și