Nummereringen av øvelsen er en indikasjon på vanskelighetsgraden, fra den enkleste til den mest kompliserte. Hvis du har kommentarer, spørsmål eller forslag: skriv til oss på 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();
}
}
Programmet leser verdien som er lagt inn i tekstfeltet som tilsvarer temperaturen i grader Celsius. Hvis den angitte teksten er tom, må ingen operasjon utføres; ellers konverteres teksten til en dobbel verdi og denne verdien økes med 273.15 for å oppnå grader Kelvin, som deretter skrives inn i det tilsvarende tekstfeltet.
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();
}
}
I det første tilfellet vil vi bruke rekursjon, hvor en metode kaller seg for løsningen, og i det andre tilfellet vil vi bruke en løkke som for og while-løkke for å få iterasjonen.
For å lage en rekursiv løsning trenger vi en base case hvor programmet avsluttes og rekursjonen stopper. Vi ser at grunntilfellet er faktoriell på 1, som er 1, så når funksjonen din kaller faktoriell (1) kan du ganske enkelt returnere 1 uten å gjøre noen beregninger. Og hvis tallet er større enn 1, fortsetter vi å bruke faktorialformelen rekursivt ved å kalle den samme faktorialen med n - 1. Når inngangen blir 1, avbryter metoden det rekursive kallet og returnerer 1. Derfra har metodestakken begynte å rulle nedover og til slutt beregnes og returneres faktoren til et tall.
Bruk iterasjonen i stedet, og deretter for-løkken for å beregne den faktorielle n! = n(n-1)(n-2) ... ..1. Du kan se at vi starter med tallet og multipliserer det med faktoren som er initialisert med 1, så reduserer vi tallet med en enhet til tallet blir 1, som ikke er annet enn n(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();
}
}
Det er mange måter å reversere en streng i Java. Vi kan bruke det avanserte Java API for raskt å reversere innholdet i ethvert String-objekt. Java-biblioteket gir StringBuffer- og StringBuilder-klassene den omvendte ()-metoden som kan brukes til å reversere String i Java. Men siden revers er rekursivt, kan vi bruke rekursjon.
Her er algoritmen og kodene for å reversere en bestemt streng i Java uten å bruke StringBuffer eller andre API-metoder. Metoden nedenfor viser hvordan du reverserer strengen, som du kan bruke på nytt for å sjekke om den gitte strengen er palindrom eller ikke.
Etter å ha validert den første inngangen, besøkes strengen tegn for tegn, fra slutten til begynnelsen og genererer en invers streng.
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();
}
}
For å administrere indeksen, la oss fortsette med å lage indeksklassen hvor metodene er lese indeks, sette inn indeks og fjerne indeks. Operasjoner innledes alltid med et binærnøkkelsøk.
For å ha fleksibilitet i datastrukturen er det bedre å bruke en arraylist.
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;
}
}
For å administrere indeksen, la oss fortsette med å lage indeksklassen hvor metodene er lese indeks, sette inn indeks og fjerne indeks. Operasjoner innledes alltid med et binærnøkkelsøk.
For å ha fleksibilitet i datastrukturen er det bedre å bruke en arraylist.
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;
}
}
For å administrere tabellene har vi laget en tabellklasse med metoder og attributter for administrasjon. Menyen ble administrert av 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);
}
}
Drafting BlogInnovazione.it
Sist mandag kunngjorde Financial Times en avtale med OpenAI. FT lisensierer sin journalistikk i verdensklasse...
Millioner av mennesker betaler for strømmetjenester og betaler månedlige abonnementsavgifter. Det er vanlig oppfatning at du...
Coveware by Veeam vil fortsette å tilby responstjenester for cyberutpressing. Coveware vil tilby kriminaltekniske og utbedringsmuligheter...
Prediktivt vedlikehold revolusjonerer olje- og gasssektoren, med en innovativ og proaktiv tilnærming til anleggsledelse...