-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
185 lines (166 loc) · 6.98 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json
import sys
import classes
from classes import *
#Nuskaito json faila y lokomatyvus ir vagonus ir sudaro traukiny.
def readJson():
with open(failas) as data_file:
data=json.load(data_file)
lok = []
vag = []
trauk = []
for i in range(len(data["lokomotyvas"])):
lok.append(Lokomotyvas(data['lokomotyvas'][i]['mase'],data['lokomotyvas'][i]['maxMase']))
for i in range(len(data["vagonas"])):
vag.append(Vagonas(data['vagonas'][i]['nr'],data['vagonas'][i]['mase'],data['vagonas'][i]['maxMase'],data['vagonas'][i]['krovinioMase']))
trauk.append(Traukinys(lok[0],vag[0],0))
trauk.append(Traukinys(lok[1],vag[1],1))
trauk.append(Traukinys(lok[2],vag[2],2))
return trauk
#Parodo visus traukinius
def showTrains():
trauk = []
trauk = readJson()
print("----------------------------------------------------")
print("| Spauzdinamai visi traukiniai |")
print("----------------------------------------------------")
for j in range(len(trauk)):
print("Traukinys: ", j+1)
print(trauk[j])
print("----------------------------------------------------")
def sorting():
trauk = []
trauk = readJson()
sortSelect = input("\nPasirinkite rusiavimo tipa:\n1.Laisva vieta traukinyje, 2.Mase: ")
if(sortSelect=="1"):
print("----------------------------------------------------")
print("| Spauzdinama laisva vieta traukinyje |")
print("----------------------------------------------------")
Laisva = []
Laisva.append(((trauk[0]+trauk[1]) - (trauk[0]-trauk[1]),0))
Laisva.append(((trauk[1]+trauk[2]) - (trauk[1]-trauk[2]),1))
Laisva.append(((trauk[2]+trauk[0]) - (trauk[2]-trauk[0]),2))
Laisva = sorted(Laisva,reverse=False)
for i in range(len(Laisva)):
for j in range(len(trauk)):
if (Laisva[i][1]==trauk[j].nr):
print("Laisva vieta traukinyje: ",Laisva[i][0])
print("Traukinys: ",trauk[j])
print("----------------------------------------------------")
elif(sortSelect=="2"):
print("----------------------------------------------------")
print("| Spauzdinama visa traukinio mase |")
print("----------------------------------------------------")
Mase = []
Mase.append((trauk[0]+trauk[1],0))
Mase.append((trauk[1]+trauk[2],1))
Mase.append((trauk[2]+trauk[0],2))
Mase = sorted(Mase,reverse=False)
for i in range(len(Mase)):
for j in range(len(trauk)):
if(Mase[i][1]==trauk[j].nr):
print("Visa traukinio mase: ",Mase[i][0])
print("Traukinys: ",trauk[j])
print("----------------------------------------------------")
else:
print("Yveskite skaiciu 1 arba 2.")
sorting()
def tikrintiInputui(sk):
try:
sk=int(sk)
except ValueError as error:
print('Negalima yvesti simbolio!\n')
print('Error code: ' + repr(error) + '\n')
writeJson()
#Lokomotyvo arba Vagono sukurimas
def writeJson():
#failas = input()
## '/mif/stud3/2014/tami1864/Python/3atsisk/'+ cia failas turi but
with open(failas) as data_file:
data=json.load(data_file)
tipas = input("1. Lokomotyvas, 2. Vagonas, 3. Q - Gryzti y meniu , Parasykite pasirinkima: ")
print("----------------------------------------------------")
if(tipas=="1"):
tipas="lokomotyvas"
mase = input("Yveskite mase: ")
tikrintiInputui(mase)
maxMase = input("Yveskite max mase: ")
tikrintiInputui(maxMase)
isvedimDuom = {'mase':int(mase),'maxMase':int(maxMase)}
data[tipas].append(isvedimDuom)
#/mif/stud3/2014/tami1864/Python/3atsisk/
with open(failas, 'w') as outfile:
json.dump(data,outfile)
print("----------------------------------------------------")
print("Lokomatyvas sukurtas sekmingai!")
print(data)
elif(tipas=="2"):
tipas="vagonas"
mase=input("Yveskite mase: ")
tikrintiInputui(mase)
maxMase=input("Yveskite max mase: ")
tikrintiInputui(maxMase)
krovinioMase=input("Yveskite krovinio mase: ")
tikrintiInputui(krovinioMase)
nr=input("Yveskite krovinio numeri: ")
tikrintiInputui(nr)
isvedimDuom = {'mase':int(mase),'maxMase':int(maxMase),'krovinioMase':int(krovinioMase),'nr':int(nr)}
data[tipas].append(isvedimDuom)
#/mif/stud3/2014/tami1864/Python/3atsisk/
with open(failas, 'w') as outfile:
json.dump(data,outfile)
print("----------------------------------------------------")
print("Vagonas sukurtas sekmingai!")
print(data)
elif(tipas == "q")or(tipas == "Q"):
menu()
else:
print("!! Neteisingas pasirinkimas, privalo buti skaicius !!")
print("!! Galite pasirinkti 1(Lokomatyvas) arba 2(Vagonas) !!")
print("!! Spauskite Q, tam kad grysti y meniu !!")
print("------------------------------------------------------")
writeJson()
print("----------------------------------------------------")
print("| PROGRAMOS NAUDOJIMAS |")
print("| |")
print("| 1. Yveskite failo pavadinima |")
print("| 2. Pasirinkite norima veiksma |")
print("| 3. Skaityti/rasyti JSON, rusiuoti |")
print("| 4. Rusiuokite faila pagal 2 kriterijus |")
print("----------------------------------------------------")
print("\n")
print("----------------------------------------------------")
print("| MENIU |")
print("| |")
print("| Yveskite failo pavadinima |")
print("| 1. Paziurekite esamus traukinius |")
print("| 2. Rusiuokite esamus traukinius |")
print("| 3. Pridekite nauja vagona ar lokomotyva |")
print("| Q. Iseiti is programos |")
print("----------------------------------------------------")
#Menu tam, kad butu lengviau naviguoti
def menu():
menuPick = input("Pasirinkimas(1,2,3,Q): ")
if(menuPick == "1"):
showTrains()
menu()
elif(menuPick == "2"):
sorting()
menu()
elif(menuPick == "3"):
writeJson()
menu()
else:
print("Programa sustabdyta.")
sys.exit(0)
def tikrintiFailui(f):
try:
f = open(f)
except FileNotFoundError as e:
print("----------------------------------------------------")
print("Failas "+ f +" nerastas!\n")
print("Error code: " + repr(e))
sys.exit(0)
failas = input("Yveskite failo pavadinima: ")
tikrintiFailui(failas)
menu()