Η αρίθμηση της άσκησης είναι ενδεικτική του επιπέδου δυσκολίας, από την πιο απλή έως την πιο περίπλοκη. Εάν έχετε οποιαδήποτε σχόλια, ερωτήσεις ή προτάσεις: γράψτε μας στο info @bloginnovazione.it
import java.util.Scanner;
public class CompareTwoStrings {
public static void main(String[] args) {
Scanner risposta = new Scanner(System.in);
System.out.println("Inserisci la prima stringa");
String str1 = risposta.nextLine();
System.out.println("Inserisci la seconda stringa");
String str2 =risposta.nextLine();
if(str1.equals(str2))
{
System.out.println("La stringa " + str1 + " è uguale a " + str2);
}
else
{
System.out.println("La stringa " + str1 + " è diversa da " + str2);
}
risposta.close();
}
}
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CompareTwoStrings {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Digitare prima stringa:");
String str1 = input.nextLine();
System.out.println("Digitare seconda stringa:");
String str2 = input.nextLine();
boolean verifica = str1.equals(str2); // check equals
boolean verificaContenuto1 = str1.contains(str2); //check inclusion
boolean verificaContenuto2 = str2.contains(str1);
if (verifica){
System.out.println("La stringa " + str1 + " è uguale alla stringa "+ str2 + " (sensibile al maiuscolo).");
}
else if (verificaContenuto1){
Pattern p = Pattern.compile(str2);
Matcher m = p.matcher(str1);
int count1 = 0;
while(m.find()){
count1 += 1;
}
System.out.println("La stringa2 " + str2 + " è contenuta nella stringa1 " + str1 + " " + count1 + " volte.");
}
else if (verificaContenuto2){
Pattern p = Pattern.compile(str1);
Matcher m = p.matcher(str2);
int count2 = 0;
while(m.find()){
count2 += 1;
}
System.out.println("La stringa1 " + str1 + " è contenuta nella stringa2 " + str2 + " " + count2 + " volte.");
}
else {
System.out.println("La stringa " + str1 + " è diversa alla stringa " + str2 + ".");
}
input.close();
System.out.println("Fine.");
}
}
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CompareTwoStrings {
public static String bi2int (String bin){
char[] numbers = binary.toCharArray();
int result = 0;
for(int i=numbers.length - 1; i>=0; i--)
if(numbers[i]=='1'){
result += Math.pow(2, (numbers.length-i - 1));
}
return result;
}
public static String bi2esa (String bin){
}
public static String int2bi (String integer){
int integer1 = Integer.parseInt(integer);
String result = Integer.toBinaryString(integer1);
return result;
}
public static String int2esa (String integer){
}
public static String esa2bi (String esa){
}
public static String esa2int (String esa){
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Digitare un numero binario, intero o esadecimale:");
String numero = input.nextLine();
Pattern pBinario = Pattern.compile("[A-Z]"|"[2-9]", Pattern.CASE_INSENSITIVE);
Matcher mBinario = pBinario.matcher(numero);
boolean binario = mBinario.find();
Pattern pIntero = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE);
Matcher mIntero = pIntero.matcher(numero);
boolean intero = mIntero.find();
Pattern pEsadecimale = Pattern.compile("[G-Z]", Pattern.CASE_INSENSITIVE);
Matcher mEsadecimale = pEsadecimale.matcher(numero);
boolean esadecimale = mEsadecimale.find();
if (binario){
System.out.println(numero + " è binario!");
System.out.println("In interi corrisponde a " + bi2int(numero) + ",");
System.out.println("in esadecimali corrisponde a " + bi2esa(numero) + ".");
}
if (intero){
System.out.println(numero + " è intero!");
System.out.println("In binario corrisponde a " + int2bi(numero) + ",");
System.out.println("in esadecimali corrisponde a " + int2esa(numero) + ".");
}
if (esadecimale){
System.out.println(numero + " è esadecimale!");
System.out.println("In binario corrisponde a " + esa2bi(numero) + ",");
System.out.println("in interi corrisponde a " + esa2int(numero) + ".");
}
if (!esadecimale && !intero && !binario){
System.out.println("Input errato!");
}
input.close();
}
}
Το πρόγραμμα διαβάζει την τιμή που έχει εισαχθεί στο πεδίο κειμένου που αντιστοιχεί στη θερμοκρασία σε βαθμούς Κελσίου. Εάν το εισαγόμενο κείμενο είναι κενό, δεν πρέπει να εκτελεστεί καμία ενέργεια. Διαφορετικά, το κείμενο μετατρέπεται σε διπλή τιμή και αυτή η τιμή αυξάνεται κατά 273.15 για να ληφθούν βαθμούς Kelvin, οι οποίοι στη συνέχεια γράφονται στο αντίστοιχο πεδίο κειμένου.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ConvertitoreTemperatura extends JFrame implements ActionListener{
JButton btnConverti;
JLabel lbTempCelsius, lbTempKelvin;
JTextField txtTempCelsius, txtTempKelvin;
JPanel pannello, pann1, pann2;
public ConvertitoreTemperatura() {
pannello = new JPanel();
pann1 = new JPanel(new GridLayout(0,1));
pann2 = new JPanel();
txtTempCelsius = new JTextField("",10);
txtTempKelvin = new JTextField("",10);
lbTempCelsius = new JLabel("Temperatura in Celsius");
lbTempKelvin = new JLabel("Temperatura in Kelvin");
btnConverti = new JButton("Converti Temperatura");
pann1.add(lbTempCelsius);
pann1.add(txtTempCelsius);
pann1.add(lbTempKelvin);
pann1.add(txtTempKelvin);
pann2.add(btnConverti);
pannello.add(pann1);
pannello.add(pann2);
setContentPane(pannello);
btnConverti.addActionListener(this);
setTitle("Convertitore Temperatura ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(400,150);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String str = txtTempCelsius.getText();
if (!str.contentEquals("")) {
double d = Double.parseDouble(str);
d = d + 273.15;
txtTempKelvin.setText(""+d);
}
}
public static void main(String[] args) {
ConvertitoreTemperatura frame = new ConvertitoreTemperatura();
}
}
public class SommaConcatenazione {
public static void main(String[] args) {
char simbolo;
int numero1;
int numero2;
String parola1;
String parola2;
int risultato;
String risultatoStringa;
Scanner scanner = new Scanner(System.in);
System.out.println("scegli l'operazione: numeri (n), Stringhe (s)");
simbolo = scanner.nextLine().charAt(0);
switch (simbolo) {
case 'n':
System.out.println("inserisci un numero");
numero1 = scanner.nextInt();
System.out.println("inserisci il secondo numero");
numero2 = scanner.nextInt();
risultato = numero1 + numero2;
System.out.println(numero1 + " + " + numero2 + " = " + risultato);
break;
case 's':
System.out.println("inserisci una parola");
parola1 = scanner.nextLine();
System.out.println("inserisci la seconda parola");
parola2 = scanner.nextLine();
risultatoStringa = parola1 + parola2;
System.out.println(parola1 + " " + parola2 + " = " + risultatoStringa);
break;
default:
System.out.println("operazione non valido");
break;
}
scanner.close();
System.out.println("FINE");
}
}
import java.util.Scanner;
public class Main{
public static int somma (int addendo1, int addendo2)
{
return addendo1 + addendo2;
};
public static String somma(String addendo1, String addendo2)
{
return addendo1 + addendo2;
};
public static void main(String[] args){
Scanner risposta = new Scanner(System.in);
System.out.println("Cosa vuoi sommare? 1-numeri....2-stringhe");
int input = risposta.nextInt();
if( input == 1)
{
System.out.println("Inserisci il primo numero");
int num1=risposta.nextInt();
System.out.println("Inserisci il secondo numero");
int num2=risposta.nextInt();
System.out.println("Il risultato è: "+ somma(num1,num2));
}
else
{
risposta.nextLine();
System.out.println("Inserisci la prima stringa");
String str1 = risposta.nextLine();
System.out.println("Inserisci la seconda stringa");
String str2 =risposta.nextLine();
System.out.println("Il risultato è: "+ somma(str1,str2));
}
risposta.close();
}
}
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Esercizio3 {
public static int somma(int addendo1, int addendo2){
return addendo1 + addendo2;
}
public static String somma(String stringa1, String stringa2){
return stringa1 + stringa2;
}
public static void main(String[] args) {
Scanner risposta = new Scanner(System.in);
System.out.println("Inserisci il primo valore");
String str1 = risposta.nextLine();
System.out.println("Inserisci il secondo valore");
String str2 =risposta.nextLine();
Pattern pattern1 = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE);
Matcher matcher1 = pattern1.matcher(str1.toUpperCase());
boolean matchFound1 = matcher1.find();
Pattern pattern2 = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE);
Matcher matcher2 = pattern2.matcher(str2.toUpperCase());
boolean matchFound2 = matcher2.find();
if( matchFound1 || matchFound2)
{
System.out.println("Il risultato è: "+ somma(str1,str2));
}
else
{
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
System.out.println("Il risultato è: "+ somma(num1,num2));
}
risposta.close();
}
}
Στην πρώτη περίπτωση θα χρησιμοποιήσουμε την αναδρομή, όπου μια μέθοδος καλεί τον εαυτό της για τη λύση, και στη δεύτερη περίπτωση θα χρησιμοποιήσουμε έναν βρόχο όπως for και while για να πάρουμε την επανάληψη.
Για να δημιουργήσουμε μια αναδρομική λύση, χρειαζόμαστε μια βασική περίπτωση όπου το πρόγραμμα τερματίζεται και η αναδρομή σταματά. Βλέπουμε ότι η βασική περίπτωση είναι παραγοντική του 1, που είναι 1, οπότε όταν η συνάρτησή σας καλεί παραγοντικό (1) μπορείτε απλώς να επιστρέψετε το 1 χωρίς να κάνετε υπολογισμούς. Και, εάν ο αριθμός είναι μεγαλύτερος από 1, συνεχίζουμε να εφαρμόζουμε τον παραγοντικό τύπο αναδρομικά καλώντας το ίδιο παραγοντικό με n - 1. Μόλις η είσοδος γίνει 1, η μέθοδος ακυρώνει την αναδρομική κλήση και επιστρέφει 1. Από εκεί και πέρα η στοίβα μεθόδων έχει άρχισε να κάνει κύλιση προς τα κάτω και τελικά υπολογίζεται και επιστρέφεται το παραγοντικό ενός αριθμού.
Χρησιμοποιώντας την επανάληψη, και μετά τον βρόχο for για τον υπολογισμό του παραγοντικού n! = n(Ν-1)(n-2) ... ..1. Μπορείτε να δείτε ότι ξεκινάμε με τον αριθμό και τον πολλαπλασιάζουμε με το παραγοντικό που έχει αρχικοποιηθεί με 1, στη συνέχεια μειώνουμε τον αριθμό κατά μία μονάδα μέχρι ο αριθμός να γίνει 1, που δεν είναι παρά n(Ν-1)(n-2) ... ..1.
import java.util.Scanner;
public class Factorial{
public static int recursiveFactorial(int n)
{
if( n <= 1)
{
return 1;
}
else
{
return n * recursiveFactorial( n - 1 );
}
}
public static int iterativeFactorial(int n){
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
return result;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in); // input
System.out.println("Insert an integer number: ");
int numero = input.nextInt();
if ( numero < 0 ){
System.out.println("The number must be positive!");
System.exit(0);
}
System.out.println("Recursive Factorial is: " + recursiveFactorial(numero));
System.out.println("Iterative Factorial is: " + iterativeFactorial(numero));
input.close();
}
}
Υπάρχουν πολλοί τρόποι για να αντιστρέψετε μια συμβολοσειρά στην Java. Μπορούμε να χρησιμοποιήσουμε το προηγμένο Java API για να αντιστρέψουμε γρήγορα τα περιεχόμενα οποιουδήποτε αντικειμένου String. Η βιβλιοθήκη Java παρέχει στις κλάσεις StringBuffer και StringBuilder τη μέθοδο reverse () που μπορεί να χρησιμοποιηθεί για την αντιστροφή του String στην Java. Αλλά επειδή η αντίστροφη είναι αναδρομική, μπορούμε να χρησιμοποιήσουμε την αναδρομή.
Εδώ είναι ο αλγόριθμος και οι κώδικες για την αντιστροφή μιας συγκεκριμένης συμβολοσειράς στην Java χωρίς τη χρήση StringBuffer ή άλλων μεθόδων API. Η παρακάτω μέθοδος δείχνει πώς μπορείτε να αντιστρέψετε τη συμβολοσειρά, την οποία μπορείτε να χρησιμοποιήσετε ξανά για να ελέγξετε εάν η δεδομένη συμβολοσειρά είναι παλίνδρομη ή όχι.
Μετά την επικύρωση της αρχικής εισαγωγής, γίνεται επίσκεψη στη συμβολοσειρά χαρακτήρα προς χαρακτήρα, ξεκινώντας από το τέλος προς την αρχή και δημιουργώντας μια αντίστροφη συμβολοσειρά.
import java.util.Scanner;
public class ReverseString {
public static String reverseString(String str){
if( str.length() == 0 ){
return str;
}
else{
return str.charAt(str.length()-1) + reverseString( str.substring(0, str.length() - 1) );
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in); // input
System.out.println("Insert a phrase: ");
String stringa = input.nextLine();
System.out.println("String reversed is " + reverseString(stringa));
input.close();
}
}
Για τη διαχείριση του ευρετηρίου, ας προχωρήσουμε στη δημιουργία της κλάσης ευρετηρίου όπου οι μέθοδοι διαβάζονται ευρετήριο, εισάγεται ευρετήριο και αφαιρείται ευρετήριο. Οι λειτουργίες προηγούνται πάντα από αναζήτηση δυαδικού κλειδιού.
Για να έχετε ευελιξία στη δομή δεδομένων, είναι προτιμότερο να χρησιμοποιήσετε μια λίστα συστοιχιών.
import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
//---------------------------------------------------------
// Main Class
//---------------------------------------------------------
public class Index {
//METODO MAIN
public static void main(String[] args){
Scanner input = new Scanner(System.in);
ArrayList<Integer> arrayInserito = new ArrayList<Integer>();
System.out.println("How many numbers do you want to enter ?");
int dimensione = input.nextInt();
int valoreLetto;
int i= 0;
for (i = 0; i <= (dimensione - 1); i++){
System.out.println("Input " + (i+1) + "^ Value");
valoreLetto = input.nextInt();
arrayInserito.add(valoreLetto);
}
VettoreNum primoArray = new VettoreNum(arrayInserito);
String decisione = "Y";
while (decisione.equalsIgnoreCase("Y")){
System.out.println("What execute ?");
System.out.println("(0) = INSERT Key");
System.out.println("(1) = REMOVE Key");
System.out.println("(2) = END");
int scelta = input.nextInt();
if (scelta == 2)
continue;
else {
System.out.println("Input key to search:");
int chiave = input.nextInt();
if (chiave < 0){
System.out.println("Error: the key must be positive.");
System.exit(0);
}
switch (scelta){
//Insert
case 0:
System.out.println("Array: " + primoArray.vettore);
System.out.println("The key is in the index : " + primoArray.cercaValore(chiave));
System.out.println("Position: " + primoArray.getPosizione());
System.out.println("The index after insert:");
System.out.println(primoArray.inserisciValore(chiave));
break;
//Remove
case 1:
System.out.println("Array: " + primoArray.vettore);
System.out.println("The key is in the index: " + primoArray.cercaValore(chiave));
if (primoArray.cercaValore(chiave)){
System.out.println("Position: " + primoArray.getPosizione());
System.out.println("The index after remove:");
System.out.println(primoArray.rimuoviValore(chiave));
}
else
System.out.println("The key is not in the index.");
break;
//no valid input
default:
System.out.println("Error: please insert a valid number.");
}
}
System.out.println("Do you want to do another ? (Y or N)");
decisione = input.next();
}
input.close();
System.out.println("END.");
}
}
//----------------------------------------------------------------
//CLASS VECTORNUM
//----------------------------------------------------------------
class VectorNum {
public int lunarray;
public int posizione;
public ArrayList<Integer> vettore;
public int sup;
public int inf;
public ArrayList<Integer> newArray;
public VettoreNum (ArrayList<Integer> vettore){
this.vettore = vettore;
this.lunarray = vettore.size();
this.posizione = -1;
this.inf = 0;
this.sup = vettore.size() - 1;
this.newArray = new ArrayList<Integer>();
}
public int getPosizione(){
return this.posizione;
}
public boolean cercaValore (int chiave){
if((this.sup - this.inf) > 1){
int m = Math.round((this.sup + this.inf)/2);
if (chiave < this.vettore.get(m)){
this.sup = m;
}
else {
this.inf = m;
}
cercaValore(chiave);
}
if (chiave <= this.vettore.get(this.inf))
this.posizione = this.inf;
else if (chiave <= this.vettore.get(this.sup))
this.posizione = this.sup;
else
this.posizione = this.sup + 1;
return (this.vettore.get(this.inf) == chiave || this.vettore.get(this.sup) == chiave);
}
public ArrayList<Integer> inserisciValore (int chiave){
newArray.clear();
int i;
for (i = 0; i < this.lunarray + 1; i++){
if (i < this.posizione)
this.newArray.add(this.vettore.get(i));
else if (i == this.posizione)
this.newArray.add(chiave);
else
this.newArray.add(this.vettore.get(i - 1));
}
this.vettore = this.newArray;
return this.vettore;
}
public ArrayList<Integer> rimuoviValore (int chiave){
vettore.remove(posizione);
return this.vettore;
}
}
Για τη διαχείριση του ευρετηρίου, ας προχωρήσουμε στη δημιουργία της κλάσης ευρετηρίου όπου οι μέθοδοι διαβάζονται ευρετήριο, εισάγεται ευρετήριο και αφαιρείται ευρετήριο. Οι λειτουργίες προηγούνται πάντα από αναζήτηση δυαδικού κλειδιού.
Για να έχετε ευελιξία στη δομή δεδομένων, είναι προτιμότερο να χρησιμοποιήσετε μια λίστα συστοιχιών.
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
public class Exercise_11 {
public static void main(String[] args) throws FileNotFoundException{
File mioFile = new File("textinput.txt");
if (!mioFile.exists()){//---------------------------------------------- file doesn't exist
System.out.println("File not found.");
}
else {//--------------------------------------------------------------- file exists
System.out.println("File exist.");
Scanner lettore = new Scanner(mioFile);
ArrayList<Integer> collezione = new ArrayList<Integer>();
while (lettore.hasNextLine()) {
int riga = lettore.nextInt();
collezione.add(riga);
}
lettore.close();
switch (collezione.size()){
case 0:
System.out.println("File empty.");
break;
case 1:
System.out.println(collezione.get(0));
break;
case 2:
System.out.println("Two numbers, and the sum is: " + (collezione.get(0)+collezione.get(1)));
break;
default:
System.out.println("More than two numbers, and the result is: " + prodotto(collezione));
}
}
}
public static int prodotto (ArrayList<Integer> x){
int i = 0;
int a = x.get(0);
for (i = 1; i<= (x.size()-1); i++){
a = a * x.get(i);
}
return a;
}
}
Για τη διαχείριση των πινάκων έχουμε φτιάξει μια κλάση πίνακα με μεθόδους και χαρακτηριστικά για διαχείριση. Η διαχείριση του μενού έγινε από το HashMap
import java.util.Scanner;
import java.util.HashMap;
//--------------------------------------------------------------------
// MAIN CLASS
//--------------------------------------------------------------------
public class Restaurant {
// Main method
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// Tables:
Table tavolo1 = new Table(1,1);
Table tavolo2 = new Table(2,2);
Table tavolo3 = new Table(3,2);
Table tavolo4 = new Table(4,8);
Table tavolo5 = new Table(5,8);
Table tavolo6 = new Table(6,8);
// Menu as HashMap:
HashMap<Integer, Integer> menu = new HashMap<Integer, Integer>();
menu.put(1,5);
menu.put(2,4);
menu.put(3,1);
menu.put(4,12);
menu.put(5,1);
menu.put(6,2);
// Handle of request at the tables:
while (true){
System.out.println("Next Customers ?");
System.out.println("(1): take a table");
System.out.println("(2): Get an order");
System.out.println("(3): The Bill");
System.out.println("(0): Go Out.");
int operazione = input.nextInt();
if (operazione == 0)
break;
//switch on previous request:
switch (operazione){
// Take a table------------------------------------------
case 1:
System.out.println("Hoe many seats ?");
System.out.println("(1): One seat");
System.out.println("(2): Two seats");
System.out.println("(3): from two to eight seats");
int persone = input.nextInt();
// switch on number of seats
switch (persone){
// One seat
case 1:
if (tavolo1.libero){
tavolo1.setOccupato();
System.out.println("Table number One !");
}
else
System.out.println("Sold Out.");
break;
// Two seats
case 2:
if (tavolo2.libero){
tavolo2.setOccupato();
System.out.println("Table number Two !");
}
else if (tavolo3.libero){
tavolo3.setOccupato();
System.out.println("Table number Three !");
}
else
System.out.println("Sodl out.");
break;
// 3 to 8 seats
case 3:
if (tavolo4.libero){
tavolo4.setOccupato();
System.out.println("Table number Four !");
}
else if (tavolo5.libero){
tavolo5.setOccupato();
System.out.println("Table number Five !");
}
else if (tavolo6.libero){
tavolo6.setOccupato();
System.out.println("Table number Six !");
}
else
System.out.println("Sold Out.");
break;
// more than eight seats
default:
System.out.println("Not available.");
}
break;
// Show Menu and take an order------------------------
case 2:
System.out.println("Tanle number:");
int num = input.nextInt();//table number to get order
System.out.println(".");
//switch on table to order:
switch (num){
case 1:
//loop to show menu on order
while (true){
System.out.println("MENU:");
System.out.println("(1) rise $5");
System.out.println("(2) spaghetti $4");
System.out.println("(3) chicken $10");
System.out.println("(4) salmon $12");
System.out.println("(5) Water $1");
System.out.println("(6) coca cola $2");
System.out.println(".");
System.out.println("Insert order (0) to exit:");
int selezione = input.nextInt();
if (selezione == 0)
continue;
else {
tavolo1.setOrdineAttuale(selezione, menu.get(selezione));
}
System.out.println("It's enough ?");
System.out.println("(1): Y; (2): N");
int ancora = input.nextInt();
if (ancora == 2)
break;
}
break;
case 2:
while (true){
System.out.println("MENU:");
System.out.println("(1) rise $5");
System.out.println("(2) spaghetti $4");
System.out.println("(3) chicken $10");
System.out.println("(4) salmon $12");
System.out.println("(5) Water $1");
System.out.println("(6) coca cola $2");
System.out.println(".");
System.out.println("Insert order (0) to exit:");
int selezione = input.nextInt();
if (selezione == 0)
continue;
else {
tavolo2.setOrdineAttuale(selezione, menu.get(selezione));
}
System.out.println("It's enough ?");
System.out.println("(1): Y; (2): N");
int ancora = input.nextInt();
if (ancora == 2)
break;
}
break;
case 3:
while (true){
System.out.println("MENU:");
System.out.println("(1) rise $5");
System.out.println("(2) spaghetti $4");
System.out.println("(3) chicken $10");
System.out.println("(4) salmon $12");
System.out.println("(5) Water $1");
System.out.println("(6) coca cola $2");
System.out.println(".");
System.out.println("Insert order (0) to exit:");
int selezione = input.nextInt();
if (selezione == 0)
continue;
else {
tavolo3.setOrdineAttuale(selezione, menu.get(selezione));
}
System.out.println("It's enough ?");
System.out.println("(1): Y; (2): N");
int ancora = input.nextInt();
if (ancora == 2)
break;
}
break;
case 4:
while (true){
System.out.println("MENU:");
System.out.println("(1) rise $5");
System.out.println("(2) spaghetti $4");
System.out.println("(3) chicken $10");
System.out.println("(4) salmon $12");
System.out.println("(5) Water $1");
System.out.println("(6) coca cola $2");
System.out.println(".");
System.out.println("Insert order (0) to exit:");
int selezione = input.nextInt();
if (selezione == 0)
continue;
else {
tavolo4.setOrdineAttuale(selezione, menu.get(selezione));
}
System.out.println("It's enough ?");
System.out.println("(1): Y; (2): N");
int ancora = input.nextInt();
if (ancora == 2)
break;
}
break;
case 5:
while (true){
System.out.println("MENU:");
System.out.println("(1) rise $5");
System.out.println("(2) spaghetti $4");
System.out.println("(3) chicken $10");
System.out.println("(4) salmon $12");
System.out.println("(5) Water $1");
System.out.println("(6) coca cola $2");
System.out.println(".");
System.out.println("Insert order (0) to exit:");
int selezione = input.nextInt();
if (selezione == 0)
continue;
else {
tavolo5.setOrdineAttuale(selezione, menu.get(selezione));
}
System.out.println("It's enough ?");
System.out.println("(1): Y; (2): N");
int ancora = input.nextInt();
if (ancora == 2)
break;
}
break;
case 6:
while (true){
System.out.println("MENU:");
System.out.println("(1) rise $5");
System.out.println("(2) spaghetti $4");
System.out.println("(3) chicken $10");
System.out.println("(4) salmon $12");
System.out.println("(5) Water $1");
System.out.println("(6) coca cola $2");
System.out.println(".");
System.out.println("Insert order (0) to exit:");
int selezione = input.nextInt();
if (selezione == 0)
continue;
else {
tavolo6.setOrdineAttuale(selezione, menu.get(selezione));
}
System.out.println("It's enough ?");
System.out.println("(1): Y; (2): N");
int ancora = input.nextInt();
if (ancora == 2)
break;
}
break;
default:
System.out.println("Invalid table");
}
break;
// Payment and table available---------------------------
case 3:
System.out.println("Insert number of table:");
int numero = input.nextInt();
System.out.println(".");
//switch on table ID
switch (numero){
case 1:
int conto1 = somma(tavolo1.ordineAttuale);
System.out.println("Amount to pay: $" + conto1);
tavolo1.ordineAttuale = null;
tavolo1.libero = true;
break;
case 2:
int conto2 = somma(tavolo2.ordineAttuale);
System.out.println("Amount to pay: $" + conto2);
tavolo2.ordineAttuale = null;
tavolo2.libero = true;
break;
case 3:
int conto3 = somma(tavolo3.ordineAttuale);
System.out.println("Amount to pay: $" + conto3);
tavolo3.ordineAttuale = null;
tavolo3.libero = true;
break;
case 4:
int conto4 = somma(tavolo4.ordineAttuale);
System.out.println("Amount to pay: $" + conto4);
tavolo4.ordineAttuale = null;
tavolo4.libero = true;
break;
case 5:
int conto5 = somma(tavolo5.ordineAttuale);
System.out.println("Amount to pay: $" + conto5);
tavolo5.ordineAttuale = null;
tavolo5.libero = true;
break;
case 6:
int conto6 = somma(tavolo6.ordineAttuale);
System.out.println("Amount to pay: $" + conto6);
tavolo6.ordineAttuale = null;
tavolo6.libero = true;
break;
default:
System.out.println("Table not available.");
}
break;
default:
System.out.println("Sold out.");
}
}
input.close();
}
// Method to sum amount of order
public static int somma(HashMap<Integer, Integer> conto){
int a = 0;
for (int i : conto.values()){
a = a + i;
}
return a;
}
}
//-----------------------------------------------------------------------
// Table class
//-----------------------------------------------------------------------
class Table {
public boolean libero;
public int tavoloID;
public int coperti;
HashMap<Integer, Integer> ordineAttuale;
public Table (int numero, int posti){
this.libero = true;
this.tavoloID = numero;
this.coperti = posti;
this.ordineAttuale = new HashMap<Integer, Integer>();
}
// table not available
public void setOccupato(){
libero = false;
}
// available table
public void setLibero(){
libero = true;
}
// check the state of the table:
public boolean getStato(){
return this.libero;
}
//
public void setOrdineAttuale(int uno, int due){
ordineAttuale.put(uno, due);
}
}
Σύνταξη BlogInnovazione.it
Η ανάπτυξη λεπτών κινητικών δεξιοτήτων μέσω του χρωματισμού προετοιμάζει τα παιδιά για πιο σύνθετες δεξιότητες όπως η γραφή. Να χρωματίσω…
Ο ναυτικός τομέας είναι μια πραγματική παγκόσμια οικονομική δύναμη, η οποία έχει προσανατολιστεί προς μια αγορά 150 δισεκατομμυρίων...
Την περασμένη Δευτέρα, οι Financial Times ανακοίνωσαν συμφωνία με το OpenAI. Η FT αδειοδοτεί την παγκόσμιας κλάσης δημοσιογραφία της…
Εκατομμύρια άνθρωποι πληρώνουν για υπηρεσίες ροής, πληρώνοντας μηνιαίες συνδρομές. Είναι κοινή γνώμη ότι…