
Курсач технологии программированияV2
.pdf
listView.setOnItemClickListener((parent, view, position, id) -> { CartItem cartItem = cartItems.get(position); cartItem.setSelected(!cartItem.isSelected()); cartAdapter.notifyDataSetChanged();
}); try {
SQL_Controller sql_con = new SQL_Controller(); Connection connection = sql_con.connectionClass();
String sqlStatement = "SELECT m.NAME, m.PRICE, c.QUANTITY FROM dbo.cart_table c JOIN dbo.menu_table m ON c.POSITION_ID = m.ID WHERE c.USER_ID = ?";
PreparedStatement ps = connection.prepareStatement(sqlStatement); ps.setString(1, String.valueOf(userId));
ResultSet set = ps.executeQuery(); while (set.next()) {
String name = set.getString("NAME"); int price = set.getInt("PRICE");
int quantity = set.getInt("QUANTITY"); cartItems.add(new CartItem(name, price, quantity));
}
set.close();
ps.close();
connection.close();
cartAdapter.notifyDataSetChanged();
}catch (Exception e) { Log.e("Error: ", e.getMessage());
}
Button orderSubmit = findViewById(R.id.orderButton); orderSubmit.setOnClickListener(view -> {
ArrayList<CartItem> selectedItems = new ArrayList<>(); for (CartItem item : cartItems) {
if (item.isSelected()) { selectedItems.add(item);
}
}
int totalPrice = 0;
for (CartItem item : selectedItems) {
totalPrice += item.getPrice() * item.getQuantity();
}
String address = "User's address"; saveOrderToServer(userId, totalPrice, address);
Intent intent = new Intent(CartActivity.this, OrderActivity.class); intent.putExtra("selectedItems", selectedItems); intent.putExtra("totalPrice", totalPrice); intent.putExtra("address", address);
startActivity(intent);
});
}
private void saveOrderToServer(int userId, int totalPrice, String address) { try {
SQL_Controller sql_con = new SQL_Controller(); Connection connection = sql_con.connectionClass();
String sqlStatement = "INSERT INTO dbo.order_table (USER_ID, ORDER_STATUS, ORDER_ADDRESS, TOTAL_PRICE, ORDER_DATE) VALUES (?, ?, ?, ?, GETDATE())";
PreparedStatement ps = connection.prepareStatement(sqlStatement); ps.setInt(1, userId);
ps.setString(2, "Pending"); // You can set the initial order status here ps.setString(3, address);
ps.setInt(4, totalPrice); ps.executeUpdate();
|
|
|
|
|
|
Лист |
|
|
|
|
|
КП-09.03.02-11-638-ПЗ |
|
|
|
|
|
|
42 |
|
Изм. |
Лист |
№ докум. |
Подп. |
Дата |
|
|
|
|

ps.close();
connection.close(); } catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
}
}
package com.example.fooddeliveryapp1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView;
import android.widget.TextView; import java.util.ArrayList;
public class OrderActivity extends AppCompatActivity { @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_order_page);
ArrayList<CartItem> selectedItems = (ArrayList<CartItem>) getIntent().getSerializableExtra("selectedItems"); int totalPrice = getIntent().getIntExtra("totalPrice", 0);
String address = getIntent().getStringExtra("address");
TextView totalPriceTextView = findViewById(R.id.totalPriceTextView); totalPriceTextView.setText("Стоимость: " + totalPrice+" руб.");
TextView addressTextView = findViewById(R.id.addressTextView); addressTextView.setText("Адрес доставки: " + address);
RecyclerView recyclerView = findViewById(R.id.itemsRecyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); OrderAdapter adapter = new OrderAdapter(selectedItems); recyclerView.setAdapter(adapter);
}
}
package com.example.fooddeliveryapp1; import android.content.SharedPreferences; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.util.Log;
import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.Statement; import java.util.ArrayList;
public class PositionInfo extends AppCompatActivity { Connection connection;
@Override
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_position_info);
SharedPreferences settings = getSharedPreferences(LoginActivity.PREFS_NAME, 0); int userId = settings.getInt(LoginActivity.USER_ID_KEY, -1);
TextView nameTextView = findViewById(R.id.nameText);
TextView descriptionTextView = findViewById(R.id.descriptionText); TextView priceTextView = findViewById(R.id.priceText);
|
|
|
|
|
|
Лист |
|
|
|
|
|
КП-09.03.02-11-638-ПЗ |
|
|
|
|
|
|
43 |
|
Изм. |
Лист |
№ докум. |
Подп. |
Дата |
|
|
|
|

Button buttonOrder = findViewById(R.id.addToOrder);
String selectedItemName = getIntent().getStringExtra("selected_item_name");
String selectedItemDescription = getIntent().getStringExtra("selected_item_description"); String selectedItemPrice = getIntent().getStringExtra("selected_item_price");
String selectedItemId = getIntent().getStringExtra("selected_item_id"); nameTextView.setText(selectedItemName); descriptionTextView.setText(selectedItemDescription); priceTextView.setText(selectedItemPrice); buttonOrder.setOnClickListener(view -> {
SQL_Controller sql_con = new SQL_Controller(); connection = sql_con.connectionClass();
try {
String sqlStatement = "Insert into [deliveryDB].[dbo].[cart_table] (POSITION_ID, USER_ID, QUANTITY) VALUES (?,?,?)";
PreparedStatement ps = connection.prepareStatement(sqlStatement); ps.setString(1, selectedItemId);
ps.setString(2, String.valueOf(userId)); ps.setString(3, "1"); ps.executeUpdate();
Toast.makeText(this, "Успешно добавлено", Toast.LENGTH_SHORT).show();
}catch (Exception e) { Log.e("Error: ", e.getMessage());
}
});
}
}
package com.example.fooddeliveryapp1; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.util.Log;
import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.sql.Connection; import java.sql.PreparedStatement;
public class AdminMainActivity extends AppCompatActivity { Connection connection;
@Override
protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_admin_main); SQL_Controller sql_con = new SQL_Controller(); connection = sql_con.connectionClass();
EditText editName = findViewById(R.id.editTextName); EditText editPrice = findViewById(R.id.editTextPrice);
EditText editDescription = findViewById(R.id.editTextDescription); Button button = findViewById(R.id.addButton); button.setOnClickListener(view -> {
if (editName.toString().isEmpty()){
Toast.makeText(AdminMainActivity.this, "Ошибка занесения в базу данных",
Toast.LENGTH_SHORT).show();
}
else {
String Price = editPrice.getText().toString(); String Name = editName.getText().toString();
String Description = editDescription.getText().toString(); try {
|
|
|
|
|
|
Лист |
|
|
|
|
|
КП-09.03.02-11-638-ПЗ |
|
|
|
|
|
|
44 |
|
Изм. |
Лист |
№ докум. |
Подп. |
Дата |
|
|
|
|

String sqlStatement = "Insert into [deliveryDB].[dbo].[menu_table] (NAME, PRICE, DESCRIPTION) VALUES (?,?,?)";
PreparedStatement ps = connection.prepareStatement(sqlStatement); ps.setString(1, Name);
ps.setString(2, Price); ps.setString(3, Description); ps.executeUpdate();
Toast.makeText(AdminMainActivity.this, "Успешно добавлено", Toast.LENGTH_SHORT).show(); connection.close();
}
catch (Exception e){
Log.e("Ошибка: ", e.getMessage());
}
}
});
}
}
package com.example.fooddeliveryapp1; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.util.Log;
import android.widget.ListView; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.Statement; import java.util.ArrayList;
public class DeletePositionActivity extends AppCompatActivity { Connection connection;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delete_pos); ListView listView = findViewById(R.id.pos_list); SQL_Controller sql_con = new SQL_Controller(); connection = sql_con.connectionClass();
try {
String sqlStatement = "Select * from [deliveryDB].[dbo].[menu_table]"; Statement smt = connection.createStatement();
ResultSet set = smt.executeQuery(sqlStatement); ArrayList<String> list = new ArrayList<>(); while (set.next()) {
String str = set.getString("NAME"); list.add(str);
}
PosAdapter adapter = new PosAdapter(DeletePositionActivity.this, list); listView.setAdapter(adapter); findViewById(R.id.deleteButton).setOnClickListener(v -> {
boolean[] checked = adapter.getChecked(); try {
String sqlDelete = "DELETE FROM [deliveryDB].[dbo].[menu_table] WHERE NAME = ?"; PreparedStatement stmt = connection.prepareStatement(sqlDelete);
for (int i = 0; i < checked.length; i++) { if (checked[i]) {
stmt.setString(1, list.get(i)); stmt.addBatch();
}
|
|
|
|
|
|
Лист |
|
|
|
|
|
КП-09.03.02-11-638-ПЗ |
|
|
|
|
|
|
45 |
|
Изм. |
Лист |
№ докум. |
Подп. |
Дата |
|
|
|
|

}
stmt.executeBatch();
connection.close();
finish();
}catch (Exception e) { Log.e("Error: ", e.getMessage());
}
});
}catch (Exception e) { Log.e("Error: ", e.getMessage());
}
}
}
|
|
|
|
|
|
Лист |
|
|
|
|
|
КП-09.03.02-11-638-ПЗ |
|
|
|
|
|
|
46 |
|
Изм. |
Лист |
№ докум. |
Подп. |
Дата |
|
|
|
|