-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpizzaproject.c
173 lines (157 loc) · 5.2 KB
/
pizzaproject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Author: Madhur Thareja
//Date: 2021-08-02
//Description: Project on Pizza Ordering System in C
//Course: Introduction to Programming in C
// Create a pizza ordering system. The system should have the following features:
// 1. A pizza can have one of the following bases: Thin, Deep Pan, Stuffed, Whole Wheat
// 2. A pizza can have any number of the following toppings: Onions, Tomatoes, Pepperoni, Cheese, Olives
// 3. The price of the bases are as follows: Thin - 350, Deep Pan - 400, Stuffed - 300, Whole Wheat - 500
// 4. The price of the toppings are as follows: Onions - 30, Tomatoes - 30, Pepperoni - 50, Cheese - 20, Olives - 30
// 5. The price of a pizza is the sum of the price of the base and the price of the toppings
// 6. The system should be able to create a new pizza, print the details of the pizza, create a new order, print the details of the order, and complete the order
// 7. The system should be able to destroy the pizza after the order is completed
// 8. The system should be able to calculate the price of the pizza
// The main() function has been provided. You do not need to modify it. You need to implement the functions new_pizza, print_pizza, calculate_price, new_order, print_order, complete_order, and destroy_pizza
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#define NUM_TOPPINGS 5
#define NUM_BASES 4
#define MAX_NAME 100
#define MAX_PHONE 20
char *BASES[NUM_BASES] = {"Thin", "Deep Pan", "Stuffed", "Whole Wheat"};
char *TOPPINGS[NUM_TOPPINGS] = {"Onions", "Tomatoes", "Pepperoni", "Cheese", "Olives"};
int base_prices[NUM_BASES] = {350, 400, 300, 500};
int topping_prices[NUM_TOPPINGS] = {30, 30, 50, 20, 30};
typedef struct pizza
{
int base;
int topping[NUM_TOPPINGS];
} pizza;
typedef struct order
{
int order_id;
char *customer_name;
char *customer_phone;
struct pizza *p;
int price;
} order;
order *new_order(int order_id, char *customer_name, char *customer_phone, pizza *pizza, int price)
{
order *o = NULL;
// Allocate memory for the new order
o = (order *)malloc(sizeof(order));
o->order_id = order_id;
o->customer_name = customer_name;
o->customer_phone = customer_phone;
o->p = pizza;
o->price = price;
// Set its datamembers as per the parameters to this function
return o;
}
void print_pizza(pizza *p)
{
printf("The pizza you ordered is as follows:\n");
printf("Base: %s\n", BASES[p->base]);
printf("Toppings:\n");
for (int i = 0; i < NUM_TOPPINGS; i++)
{
if (p->topping[i] == 1)
{
printf("%s\n", TOPPINGS[i]);
}
}
}
void print_order(order *o)
{
printf("Order placed successfully. Order details are as follows:\n");
printf("Order ID: %d\n", o->order_id);
printf("Customer Name: %s\n", o->customer_name);
printf("Customer Phone: %s\n", o->customer_phone);
print_pizza(o->p);
printf("Total Price: %d\n", o->price);
}
void destroy_pizza(pizza *p)
{
// Free the pizza
free(p);
p = NULL;
}
void complete_order(order *o)
{
// destroy the pizza
destroy_pizza(o->p);
printf("Order %d completed successfully.\n", o->order_id);
free(o);
o = NULL;
}
pizza *new_pizza(int base, int toppings[])
{
pizza *p = NULL;
// Allocate memory for the new pizza
p = (pizza *)malloc(sizeof(pizza));
p->base=base;
for (int i = 0; i < NUM_TOPPINGS;i++)
p->topping[i] = toppings[i];
// Set its datamembers as per the parameters to this function
return p;
}
int calculate_price(pizza *p)
{
int total_price = 0;
total_price += base_prices[p->base];
for (int i = 0; i < NUM_TOPPINGS; i++)
{
if (p->topping[i] == 1)
{
total_price += topping_prices[i];
}
}
return total_price;
}
// The main() has been provided. You do not need to modify it
int main()
{
// Create a new pizza
int toppings[NUM_TOPPINGS];
// User input
int base = -1;
while (base < 0 || base >= NUM_BASES)
{
printf("The available bases are:\n");
for (int i = 0; i < NUM_BASES; i++)
{
printf("%d. %s\n", i, BASES[i]);
}
printf("Enter the base number: ");
scanf("%d", &base);
}
printf("Please enter the toppings. Enter 1 if the topping is present, 0 otherwise.\n");
for (int i = 0; i < NUM_TOPPINGS; i++)
{
printf("Do you want %s? (1 for yes, 0 for no)", TOPPINGS[i]);
scanf("%d", &toppings[i]);
while (toppings[i] != 0 && toppings[i] != 1)
{
printf("Invalid input. Please enter 1 or 0.\n");
i--;
printf("Do you want %s? (1 for yes, 0 for no)", TOPPINGS[i]);
scanf("%d", &toppings[i]);
}
}
pizza *p = new_pizza(base, toppings);
print_pizza(p);
// Create a new order
int order_id = 001;
char customer_name[MAX_NAME];
char customer_phone[MAX_PHONE];
printf("Enter the customer name: ");
scanf("%s", customer_name);
printf("Enter the customer phone number: ");
scanf("%s", customer_phone);
order *o = new_order(order_id, customer_name, customer_phone, p, calculate_price(p));
print_order(o);
// Complete the order
complete_order(o);
return 0;
}