157 lines
5.8 KiB
C#
157 lines
5.8 KiB
C#
namespace biblioteca;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Console.Clear();
|
|
const int MAX_LIBRI = 10;
|
|
int scelta, input;
|
|
Libro[] biblioteca = new Libro[MAX_LIBRI];
|
|
bool oggettoEsistente;
|
|
(double, bool) ritornoRiempimento;
|
|
Dispenser elementoRiempimento;
|
|
|
|
Console.Write("Quanti dispenser considerare? ");
|
|
input = Convert.ToInt32(Console.ReadLine());
|
|
dispensers = new Dispenser[input];
|
|
|
|
do {
|
|
Console.WriteLine("Inserire un'opzione:");
|
|
Console.WriteLine("1. Crea biblioteca");
|
|
Console.WriteLine("2. Mostra biblioteca");
|
|
Console.WriteLine("3. Mostra libro");
|
|
Console.WriteLine("4. Applca sconto");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
CreaBiblioteca(biblioteca);
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
oggettoEsistente = true;
|
|
for (int i = 0; i < biblioteca.Length && oggettoEsistente; i++) {
|
|
if (biblioteca[i] == null) {
|
|
oggettoEsistente = false;
|
|
}
|
|
}
|
|
if (!oggettoEsistente) {
|
|
Console.WriteLine("Errore: è necessario creare la biblioteca prima di mostrarla.");
|
|
}
|
|
else {
|
|
for (int i = 0; i < biblioteca.Length; i++) {
|
|
Console.WriteLine($"Libro {i + 1}:");
|
|
biblioteca[i].StampaLibro();
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
Pausa();
|
|
break;
|
|
|
|
case 3:
|
|
oggettoEsistente = true;
|
|
for (int j = 0; j < dispensers.Length && oggettoEsistente; j++) {
|
|
if (dispensers[j] == null) {
|
|
oggettoEsistente = false;
|
|
}
|
|
}
|
|
if (!oggettoEsistente) {
|
|
Console.WriteLine("Errore: è necessario creare *tutti* i dispenser prima di mostrarli.");
|
|
}
|
|
else {
|
|
for (int j = 0; j < dispensers.Length; j++) {
|
|
Console.WriteLine($"Dispenser {j + 1}:");
|
|
dispensers[j].StampaDispenser();
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
Pausa();
|
|
break;
|
|
|
|
case 4:
|
|
quantitàRimasta = SelezionaDispenser(dispensers).Erogazione();
|
|
if (quantitàRimasta == 0) {
|
|
Console.WriteLine("Il dispenser è vuoto.");
|
|
}
|
|
else {
|
|
Console.WriteLine($"Quantità rimasta: {quantitàRimasta}");
|
|
}
|
|
Console.WriteLine("Erogazione effettuata");
|
|
Console.WriteLine();
|
|
Pausa();
|
|
break;
|
|
default:
|
|
Console.WriteLine("Opzione non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
|
|
}
|
|
while (scelta != 0);
|
|
}
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
static void CreaBiblioteca(Libro[] p_biblioteca) {
|
|
string nome;
|
|
double prezzo;
|
|
int numeroScaffale;
|
|
int numeroPagine;
|
|
string casaEditrice;
|
|
|
|
for (int i = 0; i < p_biblioteca.Length; i++) {
|
|
Console.Write("Inserire il titolo del libro: ");
|
|
nome = Console.ReadLine();
|
|
|
|
do {
|
|
Console.Write("Inserire il prezzo del libro:");
|
|
prezzo = Convert.ToDouble(Console.ReadLine());
|
|
if (prezzo <= 0) {
|
|
Console.WriteLine("Errore: il prezzo non può essere minore o uguale a zero.");
|
|
Pausa();
|
|
}
|
|
}
|
|
while (prezzo <= 0);
|
|
|
|
do {
|
|
Console.Write("Inserire il numero di pagine del libro:");
|
|
numeroPagine = Convert.ToInt32(Console.ReadLine());
|
|
if (numeroPagine <= 0) {
|
|
Console.WriteLine("Errore: il numero di pagine non può essere minore o uguale a zero.");
|
|
Pausa();
|
|
}
|
|
}
|
|
while (numeroPagine <= 0);
|
|
|
|
Console.Write("Inserire la casa editrice: ");
|
|
casaEditrice = Console.ReadLine();
|
|
|
|
do {
|
|
Console.WriteLine("Scegliere il numero di scaffale:");
|
|
for (int j = 0; j < p_biblioteca.Length; j++) {
|
|
Console.WriteLine($"{j}");
|
|
}
|
|
Console.Write("Scelta: ");
|
|
numeroScaffale = Convert.ToInt32(Console.ReadLine());
|
|
if (numeroScaffale < 0 || numeroScaffale > p_biblioteca.Length) {
|
|
Console.WriteLine("Numero di scaffale non valido.");
|
|
Pausa();
|
|
}
|
|
if (p_biblioteca[numeroScaffale] != null) {
|
|
Console.WriteLine("Scaffale occupato, sceglierne un altro.");
|
|
Pausa();
|
|
}
|
|
}
|
|
while (numeroScaffale < 0 || numeroScaffale > p_biblioteca.Length || p_biblioteca[numeroScaffale] != null);
|
|
|
|
p_biblioteca[numeroScaffale] = new Libro(nome, prezzo, Convert.ToString(numeroScaffale), Convert.ToString(numeroPagine), casaEditrice);
|
|
}
|
|
}
|
|
}
|