Files
Programutvecklingsmetodik/The_Store/huvud.cpp
2026-03-05 13:44:23 +01:00

203 lines
5.3 KiB
C++

//#####################################################
// LAB 3 I PUMA
// CRILLE & ROBIN 980107
// AFFÄREN
//=====================================================
#include "lista.cpp"
#include "twindow.hpp"
const int BUFFERSIZE = 100; //för säkerinmatning
const int MAXSIZE = 5; //maximalt antal element
const int SIST = 0; //inmatning i listan
const int FORST=!SIST; //inmatning i listan
const int FALSE=0; //boolsk varabel
const int TRUE=!FALSE; //boolsk varabel
const int antalKassor = 2;
const int BREDD = 7; //fönstrets bredd
const int HOJD = 2; //fönstrets höjd
//#####################################################
// SÄKER INMATNING
// SER TILL ATT BARA HELTAL MATAS IN
//=====================================================
int mataIn()
{
char buffer[BUFFERSIZE];
int f;
do
{
cin >> buffer;
if (strcmp(buffer,"0") == 0)
return 0;
else
{
f = atoi(buffer);
if (f!=0)
return f;
else
cout <<" Inget tal, forsok igen: ";
}
}while (!f);
}
//#####################################################
// SÄTTER IN ELEMENT I LISTAN
//=====================================================
void _listInsert(listClass <TWindow> &minLista)
{
if(minLista.listLength() < MAXSIZE)
{
TWindow *newItem;
newItem = new TWindow(Lleft, Ltop,(BREDD+Lleft),(HOJD+Ltop),BLUE,WHITE);
cout << " Vem kommer in i affaren?";
cout << *newItem;
newItem->editText();
if(minLista.newDuplicate(*newItem))
{
cout <<"\n Personen ar redan har.";
getch();
}
else
minLista.listInsert(minLista.listLength()+1, *newItem);
}
else
{
cout <<" Affaren ar full !!!";
getch();
}
}
//#####################################################
// VISAR ALLA ELEMENT I LISTAN
//=====================================================
void _listDisplay(listClass <TWindow> minLista)
{
TWindow *a;
a = new TWindow(1, 20,(BREDD+1),(HOJD+20),GREEN,WHITE);
if(!minLista.listIsEmpty())
for(int i=0 ; i<minLista.listLength() ; i++)
{
*a=minLista.retrive(i);
a->move(LmoveX,LmoveY);
cout << *a;
}
}
//#####################################################
// VISAR ALLA ELEMENT I KASSORNA
//=====================================================
void _queueDisplay(queueClass <TWindow> minQueue[])
{
TWindow *b,*c;
b = new TWindow(Qleft[0],Qtop,(BREDD+Qleft[0]),(HOJD+Qtop),RED,WHITE);
c = new TWindow(Qleft[1],Qtop,(BREDD+Qleft[1]),(HOJD+Qtop),RED,WHITE);
if(!minQueue[0].isEmpty())
{
for(int i=0 ; i<minQueue[0].length() ; i++)
{
*b=minQueue[0].retrive(i);
cout << *b;
b->move(QmoveX,QmoveY);
}
}
if(!minQueue[1].isEmpty())
{
for(int i=0 ; i<minQueue[1].length() ; i++)
{
*c=minQueue[1].retrive(i);
cout << *c;
c->move(QmoveX,QmoveY);
}
}
}
//#####################################################
// FLYTTAR EN PERSON TILL EN KASSA
//=====================================================
void _toQueue(queueClass<TWindow> minQueue[],listClass<TWindow> &minLista)
{
int kassa,position;
TWindow *flyttGubbe;
flyttGubbe = new TWindow(Lleft, Ltop,(BREDD+Lleft),(HOJD+Ltop),BLUE,WHITE);
cout <<" Vem vill du flytta till kon?";
cout << *flyttGubbe;
flyttGubbe->editText();
if(minLista.newDuplicate(*flyttGubbe))
{
cout <<"\n Till vilken kassa [1/2] ?";
cin >> kassa;
position=minLista.getPos(*flyttGubbe);
minQueue[kassa-1].ins(minLista.retrive(position));
minLista.listDel(position+1);
}
else
{
cout <<"\n Personen fanns inte i affaren!";
getch();
}
}
//#####################################################
// BETJÄNAR EN PERSON UR EN KASSA (TAR BORT)
//=====================================================
void _getOut(queueClass <TWindow> minQueue[])
{
int kassa;
cout <<"\n Fran vilken kassa [1/2] ?";
kassa=mataIn();
if(kassa>antalKassor)
cout <<" Vi har inte sa manga kassor!";
else if(minQueue[kassa-1].length()==0)
cout <<" Det finns ingen att betjana.";
else
{
minQueue[kassa-1].del(FORST);
return;
}
getch();
}
//#####################################################
// MAIN-MENU-ITEMS
//=====================================================
void menuItems()
{
clrscr();
TWindow *separera;
separera = new TWindow(63,0,64,15,YELLOW,YELLOW);
cout << *separera;
separera->move(LmoveX,LmoveY);
cout <<" Bengans begagnade barnklader. \n"
<<" ----------------------------- \n\n"
<<" 1. Lagg till person i affaeren. \n"
<<" 2. Flytta person till en ko. \n"
<<" 3. Betjana person ur en ko.\n"
<<" 0. Avsluta programmet. \n\n";
gotoxy(52,1);
cout <<"Kassa 1";
gotoxy(72,wherey());
cout <<"Kassa 2";
gotoxy(2,20);
cout <<"Dessa personer ar inne i affaren...";
gotoxy(2,9);
}
//#####################################################
// MAINFUNCTION
//=====================================================
void main()
{
queueClass<TWindow> minQueue[antalKassor];
listClass<TWindow> minLista;
char val = TRUE;
do
{
menuItems();
_listDisplay(minLista);
_queueDisplay(minQueue);
cin >> val;
switch (val)
{
case '1' : _listInsert(minLista);break;
case '2' : _toQueue(minQueue,minLista);break;
case '3' : _getOut(minQueue);break;
case '0' : cout <<" Programmet avslutat";break;
default: cout <<" Fel val"<< endl;
}
}while (val != '0');
}