public static void main(String []args) throws IOException {
System.out.println("Avtomobil ehtiyot qismlari do'koniga xush kelibsiz !!!\n");
Scanner sc1 = new Scanner(System.in);
System.out.println("Savdo hodimi bo'lsangiz 1 ni " +
"\n Xaridor bo'lsangiz 0 ni kiriting");
kerakali kerak = new kerakali();
byte kiritraqam = kerak.qiymat();
if (kiritraqam == 1) {
System.out.println("Admin bo'lib kirish uchun loginingizni kiriting");
String login1 = "login";
String login = sc1.nextLine();
System.out.print("Admin bo'lib kirish uchun pin kodni kiriting ==> ");
int parol = 2002;
int parolals = kerak.parol();
if (parolals == parol && login.equals(login1)) {
System.out.println("Login va pin kod tasdiqlandi \n");
String ss = "";
byte k = 0;
StringBuilder topla = new StringBuilder();
while (k == 0) {
System.out.print("Maxsulot nomini kiriting: ");
ss = kerak.oqibol();
topla.append(ss).append("\n");
System.out.print(ss + " ni narxini kiriting: ");
ss = kerak.oqibol2();
topla.append(ss).append("\n");
System.out.print("""
Yana maxsulot qo'shmoqchi bo'lsangiz 0 ni
maxsulot qo'shishni tugatmoqchi bo'lsangiz 1 ni kiriting:\040""");
k = sc1.nextByte();
}
String yozish = topla.toString();
System.out.println(kerak.faylgaYozgin(yozish));
}
}
else if (kiritraqam == 0) {
Scanner s = new Scanner(new File("avto.txt"));
ArrayList ruyxat = new ArrayList();
while (s.hasNext()) {
ruyxat.add(s.next());
}
s.close();
Dictionary lugat = new Hashtable();
String[] massiv1 = ruyxat.toArray(new String[0]);
String[] maxsulot = new String[massiv1.length / 2];
int[] narh = new int[massiv1.length / 2];
int o = 0, q = 0;
for (int i = 0; i < massiv1.length; i++) {
if (i % 2 == 0) {
maxsulot[q] = massiv1[i];
q++;
} else {
narh[o] = Integer.parseInt(massiv1[i]);
o++;
}
}
for (int i = 0; i < massiv1.length / 2; i++) {
lugat.put(maxsulot[i], narh[i]);
}
int summa=0;
StringBuilder topla1 = new StringBuilder();
byte osha = 0;
while (osha == 0) {
System.out.print("Nima sotib olmoqchisiz: ");
String kk = kerak.oqibol3();
if (lugat.get(kk)!=null) {
System.out.print(kk + " ni narhi: " + lugat.get(kk) + " so'm\n");
summa+=(int) lugat.get(kk);
topla1.append(kk).append(",");
}
else {
System.out.println("Bizda bunday maxsulot yo'q");
}
System.out.println("""
Yana maxsulot qidirmoqchi bo'lsangiz 0 ni
ishni tugatmoqchi bo'lsangiz 1 kiriting:\040""");
osha = kerak.qiymat();
}
System.out.println("Siz tanlagan Maxsulotlar"+ topla1 +"\n"+"Siz tanlagan maxsulotlarning umumiy narhi "+summa);
}
else {
System.out.println("Hatolik");
}
}
}
Qo’shimcha class
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class kerakali {
String yozildi="Muvoffaqiyatli qo'shildi";
public String yozilganiniAyt(){
return yozildi;
}
public String faylgaYozgin(String faylgayoz){
try{
FileWriter fw = new FileWriter("avto.txt");
fw.write(faylgayoz);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
return yozilganiniAyt();
}
public String oqibol(){
String ss="";
Scanner sc = new Scanner(System.in);
ss= sc.nextLine();
return ss;
}
public String oqibol2(){
String ss="";
Scanner sc = new Scanner(System.in);
ss= sc.nextLine();
return ss;
}
public String oqibol3(){
String ss="";
Scanner sc = new Scanner(System.in);
ss= sc.nextLine();
return ss;
}
public String oqibol4(){
String ss="";
Scanner sc = new Scanner(System.in);
ss= sc.nextLine();
return ss;
}
public byte qiymat(){
Scanner sc = new Scanner(System.in);
return sc.nextByte();
}
public int parol(){
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
}
Natija
Malumotlar avto.txt fayliga saqlandi va o’qib olindi