-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdmin.c
134 lines (113 loc) · 3.89 KB
/
Admin.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
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include "server.h"
int main()
{
while(1)
{
struct sockaddr_in serv;
int sd = socket(AF_INET,SOCK_STREAM,0);
if (sd == -1){
perror("Error: ");
return -1;
}
int portno = PORT_NO;
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = INADDR_ANY;
serv.sin_port = htons(portno);
printf("Hello! What would you like to do today?\n1)View the inventory\n2)Add an item\n3)Delete an item\n4)Update an item\n5)Add item by quantity\n");
int option;
scanf("%d",&option);
int size = sizeof(option);
if(connect(sd,(struct sockaddr *)&serv,sizeof(serv)) != -1)
{
write(sd,&option,size);
if(option == 1)
{
struct InventoryItem item;
while(read(sd,&item,sizeof(struct InventoryItem)))
{
printf("%d\t%s\t%d\n",item.productID,item.productName,item.price);
}
}
else if(option == 2)
{
printf("Enter the product ID, product name and the price of the product: \n");
int prodID;
char prodName[100];
int price;
scanf("%d",&prodID);
scanf("%s",prodName);
scanf("%d",&price);
struct InventoryItem item ;
item.productID = prodID;
strcpy(item.productName,prodName);
item.price = price;
write(sd,&item,sizeof(item));
char response[100];
read(sd,response,sizeof(response));
printf("%s\n",response);
}
else if(option == 3)
{
printf("Enter the productID that you wan't to remove:\n");
int prodID;
scanf("%d",&prodID);
write(sd,&prodID,sizeof(prodID));
char response[100];
read(sd,response,sizeof(response));
printf("%s\n",response);
}
else if(option == 4)
{
printf("Enter the product ID, product name and the price of the product you want to update (updated based on productID): \n");
int prodID;
char prodName[100];
int price;
scanf("%d",&prodID);
scanf("%s",prodName);
scanf("%d",&price);
struct InventoryItem item ;
item.productID = prodID;
strcpy(item.productName,prodName);
item.price = price;
write(sd,&item,sizeof(item));
char response[100];
read(sd,response,sizeof(response));
printf("%s\n",response);
}
else if(option == 5)
{
printf("Enter the productName, quantity and price of the product you want to add:\n");
char prodName[100];
int quantity;
int price;
scanf("%s",prodName);
scanf("%d",&quantity);
scanf("%d",&price);
write(sd,prodName,sizeof(prodName));
write(sd,&quantity,sizeof(quantity));
write(sd,&price,sizeof(price));
char str[2];
read(sd,&str,sizeof(str));
if(!strcmp(str,"0"))
{
printf("Successfully added.\n");
}
else
{
printf("Couldn't add the products.\n");
}
}
}
else{
printf("Unsuccessful connection.\n");
perror("");
}
close(sd);
}
}