Việc đánh số của bài tập thể hiện mức độ khó, từ đơn giản đến phức tạp nhất. Nếu bạn có bất kỳ nhận xét, câu hỏi hoặc đề xuất nào: hãy viết thư cho chúng tôi theo địa chỉ 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();
}
}
Chương trình đọc giá trị được nhập vào trường văn bản tương ứng với nhiệt độ tính bằng độ C. Nếu văn bản đã nhập trống, không có thao tác nào phải được thực hiện; nếu không, văn bản được chuyển đổi thành giá trị kép và giá trị này được tăng lên 273.15 để thu được độ Kelvin, sau đó được ghi vào trường văn bản tương ứng.
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();
}
}
Trong trường hợp đầu tiên, chúng ta sẽ sử dụng đệ quy, trong đó một phương thức tự gọi giải pháp và trong trường hợp thứ hai, chúng ta sẽ sử dụng một vòng lặp như vòng lặp for và while để lấy lần lặp.
Để tạo một giải pháp đệ quy, chúng ta cần một trường hợp cơ sở mà chương trình kết thúc và đệ quy dừng lại. Chúng ta thấy rằng trường hợp cơ sở là giai thừa của 1, là 1, vì vậy khi hàm của bạn gọi giai thừa (1), bạn chỉ có thể trả về 1 mà không cần thực hiện bất kỳ phép tính nào. Và, nếu số lớn hơn 1, chúng ta tiếp tục áp dụng đệ quy công thức giai thừa bằng cách gọi cùng một giai thừa với n - 1. Khi đầu vào trở thành 1, phương thức sẽ hủy lệnh gọi đệ quy và trả về 1. Từ đó, ngăn xếp phương thức có bắt đầu cuộn xuống và cuối cùng giai thừa của một số được tính và trả về.
Thay vào đó, sử dụng phép lặp và sau đó là vòng lặp for để tính giai thừa n! = n(n-1)(n-2)… ..1. Bạn có thể thấy rằng chúng ta bắt đầu với số và nhân nó với giai thừa được khởi tạo với 1, sau đó chúng ta giảm số đó đi một đơn vị cho đến khi số trở thành 1, không là gì khác ngoài 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();
}
}
Có nhiều cách để đảo ngược một chuỗi trong Java. Chúng tôi có thể sử dụng API Java nâng cao để nhanh chóng đảo ngược nội dung của bất kỳ đối tượng Chuỗi nào. Thư viện Java cung cấp các lớp StringBuffer và StringBuilder với phương thức reverse () có thể được sử dụng để đảo ngược String trong Java. Nhưng vì đảo ngược là đệ quy, chúng ta có thể sử dụng đệ quy.
Đây là thuật toán và mã để đảo ngược một chuỗi nhất định trong Java mà không cần sử dụng StringBuffer hoặc các phương thức API khác. Phương pháp dưới đây cho biết cách đảo ngược chuỗi, bạn có thể sử dụng lại chuỗi này để kiểm tra xem chuỗi đã cho có phải là palindrome hay không.
Sau khi xác thực đầu vào ban đầu, Chuỗi được truy cập từng ký tự, bắt đầu từ cuối đến đầu và tạo ra Chuỗi nghịch đảo.
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();
}
}
Để quản lý chỉ mục, chúng ta hãy tiến hành tạo lớp chỉ mục nơi các phương thức được đọc chỉ mục, chèn chỉ mục và loại bỏ chỉ mục. Các phép toán luôn được đặt trước bằng tìm kiếm khóa nhị phân.
Để có tính linh hoạt trong cấu trúc dữ liệu, tốt hơn là sử dụng danh sách mảng.
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;
}
}
Để quản lý chỉ mục, chúng ta hãy tiến hành tạo lớp chỉ mục nơi các phương thức được đọc chỉ mục, chèn chỉ mục và loại bỏ chỉ mục. Các phép toán luôn được đặt trước bằng tìm kiếm khóa nhị phân.
Để có tính linh hoạt trong cấu trúc dữ liệu, tốt hơn là sử dụng danh sách mảng.
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;
}
}
Để quản lý các bảng, chúng tôi đã tạo một lớp bảng với các phương thức và thuộc tính để quản lý. Menu được quản lý bởi 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);
}
}
Soạn thảo BlogInnovazione.it
Một ca phẫu thuật tạo hình mắt bằng cách sử dụng trình xem thương mại Apple Vision Pro đã được thực hiện tại Phòng khám đa khoa Catania…
Phát triển kỹ năng vận động tinh thông qua tô màu giúp trẻ chuẩn bị cho những kỹ năng phức tạp hơn như viết. Để tô màu…
Ngành hải quân là một cường quốc kinh tế toàn cầu thực sự, đang hướng tới thị trường 150 tỷ...
Thứ Hai tuần trước, Financial Times đã công bố một thỏa thuận với OpenAI. FT cấp phép cho hoạt động báo chí đẳng cấp thế giới…