Compare commits

..

2 Commits

Author SHA1 Message Date
a0ddf33d85 transaction bug fix;
transaction fix
2022-09-21 12:55:21 +08:00
dc8329de35 fix some errors of transaction;
still ongoing on transaction
2022-09-17 12:03:11 +08:00
4 changed files with 76 additions and 57 deletions

View File

@ -216,7 +216,7 @@ struct inventory item_control(struct inventory db,int index){
default: default:
break; break;
} }
} while (choice != 0); } while (choice != 0&&choice != 2);
struct inventory temp = read_db_invt(); struct inventory temp = read_db_invt();
temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1)); temp.row = (struct inventory_row *)realloc(temp.row, sizeof(struct inventory_row) * (temp.db.row_count + 1));
temp.db.row_count += 1; temp.db.row_count += 1;
@ -495,7 +495,7 @@ void list_tran(struct transaction db){
void print_tran(struct transaction db, int cur, int end,struct Map* map){ void print_tran(struct transaction db, int cur, int end,struct Map* map){
printf("%-5s%-15s%-10s%-10s%-10s%-10s%-10s\n","No.","Date","Time","Barcode","Price","Quantity","Total"); printf("%-5s%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","No.","Date","Time","Barcode","ID","Price","Quantity","Total");
for (int i = cur; i < end; i++) for (int i = cur; i < end; i++)
{ {
if(map != NULL){ if(map != NULL){
@ -503,17 +503,17 @@ void print_tran(struct transaction db, int cur, int end,struct Map* map){
//reconstuct date and time and print all transaction info out //reconstuct date and time and print all transaction info out
char date[11]; char date[11];
char time[9]; char time[9];
sprintf(date,"%04d-%02d-%02d",db.row[map[i].key].date.day,db.row[map[i].key].date.month,db.row[map[i].key].date.year); sprintf(date,"%02d-%02d-%04d",db.row[map[i].key].date.day,db.row[map[i].key].date.month,db.row[map[i].key].date.year);
sprintf(time,"%02d:%02d:%02d",db.row[map[i].key].time.hour,db.row[map[i].key].time.minute,db.row[map[i].key].time.second); sprintf(time,"%02d:%02d:%02d",db.row[map[i].key].time.hour,db.row[map[i].key].time.minute,db.row[map[i].key].time.second);
printf("%-5d%-15s%-10s%-10ld%-10.2lf%-10d%-10.2lf\n",i+9-cur,date,time,db.row[map[i].key].barcode,db.row[map[i].key].price,db.row[map[i].key].quantity,total); printf("%-5d%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",i+9,date,time,db.row[map[i].key].barcode,db.row[map[i].key].id,db.row[map[i].key].price,db.row[map[i].key].quantity,total);
}else{ }else{
double total = db.row[i].price * db.row[i].quantity; double total = db.row[i].price * db.row[i].quantity;
//reconstuct date and time and print all transaction info out //reconstuct date and time and print all transaction info out
char date[11]; char date[11];
char time[9]; char time[9];
sprintf(date,"%04d-%02d-%02d",db.row[i].date.day,db.row[i].date.month,db.row[i].date.year); sprintf(date,"%02d-%02d-%04d",db.row[i].date.day,db.row[i].date.month,db.row[i].date.year);
sprintf(time,"%02d:%02d:%02d",db.row[i].time.hour,db.row[i].time.minute,db.row[i].time.second); sprintf(time,"%02d:%02d:%02d",db.row[i].time.hour,db.row[i].time.minute,db.row[i].time.second);
printf("%-5d%-15s%-10s%-10ld%-10.2lf%-10d%-10.2lf\n",i+9-cur,date,time,db.row[i].barcode,db.row[i].price,db.row[i].quantity,total); printf("%-5d%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",i+9,date,time,db.row[i].barcode,db.row[i].id,db.row[i].price,db.row[i].quantity,total);
} }
} }
} }
@ -524,46 +524,59 @@ struct transaction update_tran(struct transaction db,int index);
struct transaction remove_tran(struct transaction db,int index); struct transaction remove_tran(struct transaction db,int index);
struct transaction showTran(struct transaction db,int index){ struct transaction showTran(struct transaction db,int index){
int choice;
do{
printf("Transaction detail\n"); printf("Transaction detail\n");
printf("Date:day %s\n",db.row[index].date.day); double total = db.row[index].price * db.row[index].quantity;
printf("Date:month %s\n",db.row[index].date.month); //reconstuct date and time and print all transaction info out
printf("Date:year %s\n",db.row[index].date.year); char date[11];
printf("Time:hour %s\n",db.row[index].time.hour); char time[9];
printf("Time:minute %s\n",db.row[index].time.minute); sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year);
printf("Time:second %s\n",db.row[index].time.second); sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second);
printf("Price: $%.1lf\n",db.row[index].price); printf("%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity","Total");
printf("Quantity: %d\n",db.row[index].quantity); printf("%-15s%-10s%-10d%-10d%-10.2lf%-10d%-10.2lf\n",date,time,db.row[index].barcode,db.row[index].id,db.row[index].price,db.row[index].quantity,total);
printf("ID: %d\n",db.row[index].id);
printf("Barcode: %s\n",db.row[index].barcode);
printf("Total: $%.1lf\n",db.row[index].price * db.row[index].quantity);
printf("1 edit transaction\n"); printf("1 edit transaction\n");
printf("2 delete transaction\n"); printf("2 delete transaction\n");
printf("3 exit\n"); printf("3 exit\n");
int choice; bool valid = true;
do{ do{
printf("Please input your choice: "); printf("Please input your choice: ");
fflush(stdin); fflush(stdin);
scanf("%d",&choice); scanf("%d",&choice);
switch(choice){ switch(choice){
case 1: case 1:
valid = true;
db = update_tran(db,index); db = update_tran(db,index);
break; break;
case 2: case 2:
valid = true;
db = remove_tran(db,index); db = remove_tran(db,index);
break; break;
case 3: case 3:
valid = true;
break; break;
default: default:
printf("Invalid input, try again\n"); printf("Invalid input, try again\n");
valid = false;
break; break;
} }
}while(!valid);
}while(choice != 3); }while(choice != 3);
return db;
} }
//tran controls //tran controls
void add_tran(){ void add_tran(){
char* temp; char* temp;
struct transaction_row* row = (struct transaction_row*)malloc(sizeof(struct transaction_row)); struct transaction_row* row = (struct transaction_row*)malloc(sizeof(struct transaction_row));
char check;
printf("Use current date time? (y/n): ");
fflush(stdin);
scanf("%c",&check);
if(check == 'y'){
row->date = get_date();
row->time = get_time();
}else{
temp = prompt_item("Please input date:day \n>"); temp = prompt_item("Please input date:day \n>");
if(temp == NULL){ if(temp == NULL){
free(row); free(row);
@ -594,6 +607,12 @@ void add_tran(){
return; return;
} }
row->time.minute = atoi(temp); row->time.minute = atoi(temp);
temp = prompt_item("Please input time:second \n>");
if(temp == NULL){
free(row);
return;
}
}
temp = prompt_item("Please input price \n>"); temp = prompt_item("Please input price \n>");
if(temp == NULL){ if(temp == NULL){
free(row); free(row);
@ -633,37 +652,37 @@ void add_tran(){
struct transaction update_tran(struct transaction db,int index){ struct transaction update_tran(struct transaction db,int index){
char temp[100]; char temp[100];
printf("Update transaction(empty value to not change the value)\n"); printf("Update transaction(empty value to not change the value)\n");
printf("Please input the Date:day(original:%s)\n>",db.row[index].date.day); printf("Please input the Date:day(original:%d)\n>",db.row[index].date.day);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp);//input until /n scanf("%[^\n]", temp);//input until /n
if(strcmp(temp,"") != 0){//check if temp is not empty if(strcmp(temp,"") != 0){//check if temp is not empty
db.row[index].date.day = atoi(temp); db.row[index].date.day = atoi(temp);
}//else preserve the original value }//else preserve the original value
printf("Please input the Date:month(original:%s)\n>",db.row[index].date.month); printf("Please input the Date:month(original:%d)\n>",db.row[index].date.month);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){ if(strcmp(temp,"") != 0){
db.row[index].date.month = atoi(temp); db.row[index].date.month = atoi(temp);
} }
printf("Please input the Date:year(original:%s)\n>",db.row[index].date.year); printf("Please input the Date:year(original:%d)\n>",db.row[index].date.year);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){ if(strcmp(temp,"") != 0){
db.row[index].date.year = atoi(temp); db.row[index].date.year = atoi(temp);
} }
printf("Please input the Time:hour(original:%s)\n>",db.row[index].time.hour); printf("Please input the Time:hour(original:%d)\n>",db.row[index].time.hour);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){ if(strcmp(temp,"") != 0){
db.row[index].time.hour = atoi(temp); db.row[index].time.hour = atoi(temp);
} }
printf("Please input the Time:minute(original:%s)\n>",db.row[index].time.minute); printf("Please input the Time:minute(original:%d)\n>",db.row[index].time.minute);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){ if(strcmp(temp,"") != 0){
db.row[index].time.minute = atoi(temp); db.row[index].time.minute = atoi(temp);
} }
printf("Please input the Time:second(original:%s)\n>",db.row[index].time.second); printf("Please input the Time:second(original:%d)\n>",db.row[index].time.second);
fflush(stdin); fflush(stdin);
scanf("%[^\n]", temp); scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){ if(strcmp(temp,"") != 0){

View File

@ -469,7 +469,7 @@ bool append_transaction_db(struct transaction_row* row){
printf("Error in opening file\n"); printf("Error in opening file\n");
return false; return false;
} }
fprintf(fp,"%d-%02d-%02d\n",row->date.year,row->date.month,row->date.day); fprintf(fp,"%04d-%02d-%02d\n",row->date.year,row->date.month,row->date.day);
fprintf(fp,"%02d:%02d:%02d\n",row->time.hour,row->time.minute,row->time.second); fprintf(fp,"%02d:%02d:%02d\n",row->time.hour,row->time.minute,row->time.second);
fprintf(fp,"%ld\n",row->id); fprintf(fp,"%ld\n",row->id);
fprintf(fp,"%.1f\n",row->price); fprintf(fp,"%.1f\n",row->price);

View File

@ -20,11 +20,11 @@ typedef struct Date{
struct Date convert_to_date(char* date){ struct Date convert_to_date(char* date){
struct Date d; struct Date d;
char* token = strtok(date, "-"); char* token = strtok(date, "-");
d.day = atoi(token); d.year = atoi(token);
token = strtok(NULL, "-"); token = strtok(NULL, "-");
d.month = atoi(token); d.month = atoi(token);
token = strtok(NULL, "-"); token = strtok(NULL, "-");
d.year = atoi(token); d.day = atoi(token);
return d; return d;
} }