fix some errors of transaction;

still ongoing on transaction
This commit is contained in:
stmctommyau 2022-09-17 12:03:11 +08:00
parent c69d312335
commit dc8329de35

View File

@ -495,7 +495,7 @@ void list_tran(struct transaction db){
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++)
{
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
char date[11];
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);
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{
double total = db.row[i].price * db.row[i].quantity;
//reconstuct date and time and print all transaction info out
char date[11];
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);
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,39 +524,43 @@ struct transaction update_tran(struct transaction db,int index);
struct transaction remove_tran(struct transaction db,int index);
struct transaction showTran(struct transaction db,int index){
int choice;
do{
printf("Transaction detail\n");
printf("Date:day %s\n",db.row[index].date.day);
printf("Date:month %s\n",db.row[index].date.month);
printf("Date:year %s\n",db.row[index].date.year);
printf("Time:hour %s\n",db.row[index].time.hour);
printf("Time:minute %s\n",db.row[index].time.minute);
printf("Time:second %s\n",db.row[index].time.second);
printf("Price: $%.1lf\n",db.row[index].price);
printf("Quantity: %d\n",db.row[index].quantity);
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);
double total = db.row[index].price * db.row[index].quantity;
//reconstuct date and time and print all transaction info out
char date[11];
char time[9];
sprintf(date,"%02d-%02d-%04d",db.row[index].date.day,db.row[index].date.month,db.row[index].date.year);
sprintf(time,"%02d:%02d:%02d",db.row[index].time.hour,db.row[index].time.minute,db.row[index].time.second);
printf("%-15s%-10s%-10s%-10s%-10s%-10s%-10s\n","Date","Time","Barcode","ID","Price","Quantity","Total");
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("1 edit transaction\n");
printf("2 delete transaction\n");
printf("3 exit\n");
int choice;
bool valid = true;
do{
printf("Please input your choice: ");
fflush(stdin);
scanf("%d",&choice);
switch(choice){
case 1:
valid = true;
db = update_tran(db,index);
break;
case 2:
valid = true;
db = remove_tran(db,index);
break;
case 3:
valid = true;
break;
default:
printf("Invalid input, try again\n");
valid = false;
break;
}
}while(!valid);
}while(choice != 3);
}
@ -633,37 +637,37 @@ void add_tran(){
struct transaction update_tran(struct transaction db,int index){
char temp[100];
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);
scanf("%[^\n]", temp);//input until /n
if(strcmp(temp,"") != 0){//check if temp is not empty
db.row[index].date.day = atoi(temp);
}//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);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
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);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
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);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
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);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){
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);
scanf("%[^\n]", temp);
if(strcmp(temp,"") != 0){