diff --git a/flower-shop/.gitignore b/flower-shop/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/flower-shop/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/flower-shop/app/.gitignore b/flower-shop/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/flower-shop/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/flower-shop/app/build.gradle b/flower-shop/app/build.gradle new file mode 100644 index 0000000..96911f8 --- /dev/null +++ b/flower-shop/app/build.gradle @@ -0,0 +1,36 @@ +plugins { + id 'com.android.application' +} + +android { + namespace 'com.example.flowershop' + compileSdk 34 + + defaultConfig { + applicationId "com.example.flowershop" + minSdk 21 + targetSdk 34 + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation 'androidx.appcompat:appcompat:1.3.1' + implementation 'com.google.android.material:material:1.4.0' + + implementation 'com.github.bumptech.glide:glide:4.16.0' + annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0' + implementation "com.github.bumptech.glide:okhttp3-integration:4.16.0" +} \ No newline at end of file diff --git a/flower-shop/app/proguard-rules.pro b/flower-shop/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/flower-shop/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/flower-shop/app/src/main/AndroidManifest.xml b/flower-shop/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2ada20d --- /dev/null +++ b/flower-shop/app/src/main/AndroidManifest.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/MyApplication.java b/flower-shop/app/src/main/java/com/example/flowershop/MyApplication.java new file mode 100644 index 0000000..83d7a17 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/MyApplication.java @@ -0,0 +1,37 @@ +package com.example.flowershop; + +import android.app.Application; + +import com.example.flowershop.entity.Stuff; +import com.example.flowershop.sqlite.DBStuff; +import com.example.flowershop.utils.AppUtils; + +public class MyApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + if (AppUtils.isFirstTimeLaunch()) { + Stuff roseWhite = new Stuff("白玫瑰", "清新素雅的白色玫瑰,花瓣洁白如雪,散发着淡淡的清香", "玫瑰系列", "28"); + DBStuff.add(roseWhite); + + Stuff rosePink = new Stuff("粉玫瑰", "柔美粉色的玫瑰,娇艳欲滴,花语是甜蜜的爱情与温柔的关怀", "玫瑰系列", "22"); + DBStuff.add(rosePink); + + Stuff carnation = new Stuff("康乃馨", "康乃馨象征着母爱与关怀,花语是无尽的爱与感激,花瓣色彩丰富,芬芳宜人", "母亲节活动", "27"); + DBStuff.add(carnation); + + Stuff roseBlue = new Stuff("蓝玫瑰", "神秘而罕见的蓝色玫瑰,象征着奇迹与梦想,独特的色彩令人心驰神往", "玫瑰系列", "26"); + DBStuff.add(roseBlue); + + Stuff sunflower = new Stuff("向日葵", "向日葵是太阳的化身,代表着希望与积极的生命力,金黄色的花朵给人温暖与活力", "热卖款", "30"); + DBStuff.add(sunflower); + + Stuff tulip = new Stuff("郁金香", "郁金香是春天的使者,花语是美丽的爱情与真挚的情感,多姿多彩的花瓣散发着迷人的芳香", "热卖款", "21"); + DBStuff.add(tulip); + + AppUtils.setFirstTimeLaunch(false); + + } + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/AboutActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/AboutActivity.java new file mode 100644 index 0000000..cfa1bda --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/AboutActivity.java @@ -0,0 +1,22 @@ +package com.example.flowershop.activity; + +import android.os.Bundle; +import android.view.View; + +import androidx.appcompat.app.AppCompatActivity; + +import com.example.flowershop.R; + +public class AboutActivity extends AppCompatActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_about); + findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/DetailActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/DetailActivity.java new file mode 100644 index 0000000..81a83bb --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/DetailActivity.java @@ -0,0 +1,93 @@ +package com.example.flowershop.activity; + +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.appcompat.app.AppCompatActivity; + +import com.bumptech.glide.Glide; +import com.example.flowershop.R; +import com.example.flowershop.entity.Record; +import com.example.flowershop.entity.Stuff; +import com.example.flowershop.entity.User; +import com.example.flowershop.sqlite.DBCart; +import com.example.flowershop.sqlite.DBRecord; +import com.example.flowershop.sqlite.DBStuff; +import com.example.flowershop.utils.CurrentUserUtils; + +public class DetailActivity extends AppCompatActivity { + private TextView tvTitle, tvName, tvPrice, tvKind; + private ImageView ivImage, ivBack; + private Button btnBuy, btnAdd; + + private Stuff stuff; + + private User user; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_detail); + Intent intent = getIntent(); + String id = intent.getStringExtra("id"); + stuff = DBStuff.getById(Integer.parseInt(id)); + user = CurrentUserUtils.getCurrentUser(); + bindView(); + initView(); + } + + private void bindView() { + tvTitle = findViewById(R.id.tv_title); + tvName = findViewById(R.id.tv_name); + tvPrice = findViewById(R.id.tv_price); + tvKind = findViewById(R.id.tv_kind); + ivImage = findViewById(R.id.iv_image); + btnBuy = findViewById(R.id.btn_buy); + btnAdd = findViewById(R.id.btn_add); + ivBack = findViewById(R.id.iv_back); + } + + private void initView() { + tvTitle.setText(stuff.getTitle()); + tvName.setText("名称:" + stuff.getName()); + tvPrice.setText("价格:¥" + stuff.getPrice()); + + Glide.with(ivImage).load(stuff.getPic()).into(ivImage); + + tvKind.setText("分类:" + stuff.getKind()); + btnAdd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (DBCart.add(stuff.getId(), user.getName())) { + Toast.makeText(DetailActivity.this, "加入购物车成功", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(DetailActivity.this, "加入购物车失败,或已在购物车中", Toast.LENGTH_SHORT).show(); + } + } + }); + ivBack.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + btnBuy.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Record r = new Record(user.getName(), stuff.getId(), tvName.getText().toString(), stuff.getPrice(), user.getAddress()); + if (DBRecord.add(r)) { + Toast.makeText(DetailActivity.this, "价格" + stuff.getPrice() + "元,购买成功!", Toast.LENGTH_SHORT).show(); + Log.d("-----", r.toString()); + } else { + Toast.makeText(DetailActivity.this, "购买失败!", Toast.LENGTH_SHORT).show(); + } + } + }); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/InfoActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/InfoActivity.java new file mode 100644 index 0000000..d6fbbd6 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/InfoActivity.java @@ -0,0 +1,43 @@ +package com.example.flowershop.activity; + +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; + +import com.example.flowershop.R; +import com.example.flowershop.entity.User; +import com.example.flowershop.utils.CurrentUserUtils; + +public class InfoActivity extends AppCompatActivity { + private TextView tvName, tvAddress; + private ImageView ivBack; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_info); + bindView(); + initView(); + } + + private void bindView() { + tvName = findViewById(R.id.tv_name); + tvAddress = findViewById(R.id.tv_address); + ivBack = findViewById(R.id.iv_back); + } + + private void initView() { + User user = CurrentUserUtils.getCurrentUser(); + tvName.setText(user.getName()); + tvAddress.setText(user.getAddress()); + ivBack.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/LoginActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/LoginActivity.java new file mode 100644 index 0000000..3d1839b --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/LoginActivity.java @@ -0,0 +1,84 @@ +package com.example.flowershop.activity; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.appcompat.app.AppCompatActivity; + +import com.example.flowershop.R; +import com.example.flowershop.entity.User; +import com.example.flowershop.sqlite.DBUser; +import com.example.flowershop.utils.CodeUtils; +import com.example.flowershop.utils.CurrentUserUtils; + +public class LoginActivity extends AppCompatActivity { + + private Button btnLogin; + private Button btnRegister; + private TextView tvCode; + private EditText etName, etPassword, etCode; + + private String code; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_login); + bindView(); + initView(); + } + + + private void bindView() { + btnLogin = findViewById(R.id.btn_login); + etPassword = findViewById(R.id.et_password); + tvCode = findViewById(R.id.tv_code); + etName = findViewById(R.id.et_name); + etCode = findViewById(R.id.et_code); + btnRegister = findViewById(R.id.btn_register); + } + + private void initView() { + User user = CurrentUserUtils.getCurrentUser(); + if (user!=null){ + etName.setText(user.getName()); + } + + btnLogin.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (!etCode.getText().toString().equals(code)) { + Toast.makeText(LoginActivity.this, "验证码错误", Toast.LENGTH_SHORT).show(); + return; + } + String n = etName.getText().toString(); + String p = etPassword.getText().toString(); + if (DBUser.check(n, p)) { + Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); + CurrentUserUtils.setCurrentUser(DBUser.get(n)); + Intent intent = new Intent(); + intent.setClass(LoginActivity.this, MainActivity.class); + startActivity(intent); + } else { + Toast.makeText(LoginActivity.this, "登录失败,用户名或密码错误", Toast.LENGTH_SHORT).show(); + } + } + }); + btnRegister.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(); + intent.setClass(LoginActivity.this, RegisterActivity.class); + startActivity(intent); + } + }); + code = CodeUtils.getCode(); + tvCode.setText(code); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/MainActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/MainActivity.java new file mode 100644 index 0000000..27bd47c --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/MainActivity.java @@ -0,0 +1,60 @@ +package com.example.flowershop.activity; + +import android.os.Bundle; +import android.view.MenuItem; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentTransaction; + +import com.example.flowershop.R; +import com.example.flowershop.fragment.CartFragment; +import com.example.flowershop.fragment.MainFragment; +import com.example.flowershop.fragment.MeFragment; +import com.google.android.material.bottomnavigation.BottomNavigationView; +import com.google.android.material.navigation.NavigationBarView; + +import java.util.ArrayList; +import java.util.List; + +public class MainActivity extends AppCompatActivity { + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + List fragments = new ArrayList<>(); + fragments.add(new MainFragment()); + fragments.add(new CartFragment()); + fragments.add(new MeFragment()); + + BottomNavigationView navigation = findViewById(R.id.navigation); + navigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { + @Override + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + int itemId = item.getItemId(); + if (itemId == R.id.home) { + FragmentTransaction tran3 = getSupportFragmentManager().beginTransaction(); + tran3.replace(R.id.main, fragments.get(0)); + tran3.commit(); + return true; + } else if (itemId == R.id.cart) { + FragmentTransaction tran2 = getSupportFragmentManager().beginTransaction(); + tran2.replace(R.id.main, fragments.get(1)); + tran2.commit(); + + return true; + } else if (itemId == R.id.me) { + FragmentTransaction tran1 = getSupportFragmentManager().beginTransaction(); + tran1.replace(R.id.main, fragments.get(2)); + tran1.commit(); + return true; + } + return false; + } + }); + navigation.setSelectedItemId(R.id.home); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/RecordActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/RecordActivity.java new file mode 100644 index 0000000..900c107 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/RecordActivity.java @@ -0,0 +1,56 @@ +package com.example.flowershop.activity; + +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +import androidx.appcompat.app.AppCompatActivity; + +import com.example.flowershop.R; +import com.example.flowershop.adapter.RecordAdapter; +import com.example.flowershop.entity.Record; +import com.example.flowershop.entity.User; +import com.example.flowershop.sqlite.DBRecord; +import com.example.flowershop.utils.CurrentUserUtils; + +import java.util.List; + +public class RecordActivity extends AppCompatActivity { + private ListView lvRecord; + private TextView tvEmpty; + private ImageView ivBack; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_record); + bindView(); + initView(); + } + + private void bindView() { + lvRecord = findViewById(R.id.lv_record); + tvEmpty = findViewById(R.id.tv_empty); + ivBack = findViewById(R.id.iv_back); + } + + private void initView() { + ivBack.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + User user = CurrentUserUtils.getCurrentUser(); + List array = DBRecord.getAll(user.getName()); + if (array.isEmpty()) { + tvEmpty.setVisibility(View.VISIBLE); + } else { + tvEmpty.setVisibility(View.GONE); + } + RecordAdapter adapter = new RecordAdapter(this, R.layout.item_record, array); + lvRecord.setAdapter(adapter); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/activity/RegisterActivity.java b/flower-shop/app/src/main/java/com/example/flowershop/activity/RegisterActivity.java new file mode 100644 index 0000000..cdc9b5d --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/activity/RegisterActivity.java @@ -0,0 +1,77 @@ +package com.example.flowershop.activity; + +import android.os.Bundle; +import android.text.TextUtils; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.Toast; + +import androidx.appcompat.app.AppCompatActivity; + +import com.example.flowershop.R; +import com.example.flowershop.entity.User; +import com.example.flowershop.sqlite.DBUser; + + +public class RegisterActivity extends AppCompatActivity { + private ImageView ivBack; + private EditText etName, etAddress; + private EditText etPassword; + private EditText etPasswordConfirm; + private Button btnRegister; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_register); + bindView(); + initView(); + } + + private void bindView() { + etAddress = findViewById(R.id.et_address); + ivBack = findViewById(R.id.iv_back); + etName = findViewById(R.id.et_name); + etPassword = findViewById(R.id.et_password); + etPasswordConfirm = findViewById(R.id.et_password_confirm); + btnRegister = findViewById(R.id.btn_register); + } + + private void initView() { + btnRegister.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (etPassword.getText().toString().isEmpty() || etPasswordConfirm.getText().toString().isEmpty() || etName.getText().toString().isEmpty() || etAddress.getText().toString().isEmpty()) { + Toast.makeText(RegisterActivity.this, "数据不能为空", Toast.LENGTH_SHORT).show(); + return; + } + if (!etPassword.getText().toString().equals(etPasswordConfirm.getText().toString())) { + Toast.makeText(RegisterActivity.this, "两次密码不一样", Toast.LENGTH_SHORT).show(); + return; + } + User u = new User(etName.getText().toString(), etPassword.getText().toString(), etAddress.getText().toString()); + if (!TextUtils.isEmpty(DBUser.get(u.getName()).getName())) { + Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show(); + return; + } + if (DBUser.add(u)) { + Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); + finish(); + } else { + Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show(); + } + + + } + }); + ivBack.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + } + }); + } + +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/adapter/CartAdapter.java b/flower-shop/app/src/main/java/com/example/flowershop/adapter/CartAdapter.java new file mode 100644 index 0000000..d6a687f --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/adapter/CartAdapter.java @@ -0,0 +1,92 @@ +package com.example.flowershop.adapter; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; + +import com.bumptech.glide.Glide; +import com.example.flowershop.R; +import com.example.flowershop.entity.Record; +import com.example.flowershop.entity.Stuff; +import com.example.flowershop.entity.User; +import com.example.flowershop.fragment.CartFragment; +import com.example.flowershop.utils.CurrentUserUtils; + +import java.util.List; + +public class CartAdapter extends ArrayAdapter { + + private final User user; + + public CartAdapter(@NonNull Context context, int resource, @NonNull List objects) { + super(context, resource, objects); + user = CurrentUserUtils.getCurrentUser(); + } + + //每个子项被滚动到屏幕内的时候会被调用 + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + Stuff s = getItem(position);//得到当前项的 Stuff 实例 + + View view = LayoutInflater.from(getContext()).inflate(R.layout.item_cart, parent, false); + + TextView title = view.findViewById(R.id.tv_title); + TextView price = view.findViewById(R.id.tv_price); + ImageView pic = view.findViewById(R.id.iv_pic); + CheckBox box = view.findViewById(R.id.box); + title.setText(s.getTitle()); + price.setText("¥" + s.getPrice()); + + Glide.with(pic).load(s.getPic()).into(pic); + + pic.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Toast.makeText(getContext(), s.getName(), Toast.LENGTH_SHORT).show(); + } + }); + /* + 点击多选框,把此商品的价格加去或减去,显示在购物车页面的总价total中 + 添加或删除,商品到购买记录中。 + */ + box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean b) { + double total = Double.parseDouble(CartFragment._tvTotal.getText().toString()); + if (box.isChecked()) { + //Toast.makeText(getContext(), "选中了", Toast.LENGTH_SHORT).show(); + Record r = new Record(user.getName(), s.getId(), s.getName(), s.getPrice(), user.getAddress()); + CartFragment.record.add(r); + total += Double.parseDouble(r.getPrice()); + } else { + int i = 0; + for (Record r : CartFragment.record) { + if (r.getId().equals(s.getId())) { + total -= Double.parseDouble(r.getPrice()); + CartFragment.record.remove(i); + break; + } + i++; + } + } + CartFragment._tvTotal.setText(String.format("%.2f", total)); + } + }); + return view; + } + +} + diff --git a/flower-shop/app/src/main/java/com/example/flowershop/adapter/RecordAdapter.java b/flower-shop/app/src/main/java/com/example/flowershop/adapter/RecordAdapter.java new file mode 100644 index 0000000..355cb62 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/adapter/RecordAdapter.java @@ -0,0 +1,55 @@ +package com.example.flowershop.adapter; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; + +import com.bumptech.glide.Glide; +import com.example.flowershop.R; +import com.example.flowershop.sqlite.DBStuff; +import com.example.flowershop.entity.Record; +import com.example.flowershop.entity.Stuff; + + +import java.util.ArrayList; +import java.util.List; + +public class RecordAdapter extends ArrayAdapter { + public RecordAdapter(@NonNull Context context, int resource, @NonNull List objects) { + super(context, resource, objects); + } + //每个子项被滚动到屏幕内的时候会被调用 + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + Record r=getItem(position);//得到当前项的 Fruit 实例 + + View view= LayoutInflater.from(getContext()).inflate(R.layout.item_record,parent,false); + + String id=r.getId(); + Stuff stuff=DBStuff.getById(Integer.valueOf(id)); + + TextView title =view.findViewById(R.id.tv_title); + TextView address =view.findViewById(R.id.et_address); + ImageView pic=view.findViewById(R.id.iv_pic); + TextView price =view.findViewById(R.id.tv_price); + + price.setText("¥"+r.getPrice()); + title.setText(stuff.getTitle()); + address.setText("收货地址:"+r.getAddress()); + + Glide.with(pic).load(stuff.getPic()).into(pic); + + return view; + } + +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/adapter/StuffAdapter.java b/flower-shop/app/src/main/java/com/example/flowershop/adapter/StuffAdapter.java new file mode 100644 index 0000000..258dbac --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/adapter/StuffAdapter.java @@ -0,0 +1,64 @@ +package com.example.flowershop.adapter; + + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; + +import com.bumptech.glide.Glide; +import com.example.flowershop.R; +import com.example.flowershop.entity.Stuff; + +import java.util.List; + +public class StuffAdapter extends ArrayAdapter { + public StuffAdapter(@NonNull Context context, int resource, @NonNull List objects) { + super(context, resource, objects); + } + + //每个子项被滚动到屏幕内的时候会被调用 + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + Stuff s = getItem(position);//得到当前项的 Fruit 实例 + + View view = LayoutInflater.from(getContext()).inflate(R.layout.item_stuff, parent, false); + + TextView kind = view.findViewById(R.id.tv_kind); + TextView title = view.findViewById(R.id.tv_title); + TextView name = view.findViewById(R.id.et_name); + ImageView pic = view.findViewById(R.id.iv_pic); + TextView price = view.findViewById(R.id.tv_price); + + price.setText("¥" + s.getPrice()); + kind.setText("分类:" + s.getKind()); + title.setText(s.getTitle()); + name.setText("名称:" + s.getName()); + + Glide.with(pic).load(s.getPic()).into(pic); + + pic.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Toast.makeText(getContext(), s.getName(), Toast.LENGTH_SHORT).show(); + } + }); + return view; + } + + public void setData(List stuffList) { + clear(); + addAll(stuffList); + notifyDataSetChanged(); + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/entity/Record.java b/flower-shop/app/src/main/java/com/example/flowershop/entity/Record.java new file mode 100644 index 0000000..b202ecf --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/entity/Record.java @@ -0,0 +1,68 @@ +package com.example.flowershop.entity; + +public class Record { + private String username; + private String id;//商品id + private String name;//商品名称 + private String price; + private String address; + + @Override + public String toString() { + return "Record{" + + "username='" + username + '\'' + + ", id='" + id + '\'' + + ", name='" + name + '\'' + + ", price='" + price + '\'' + + ", address='" + address + '\'' + + '}'; + } + + public Record(String username, String id, String name, String price, String address) { + this.username = username; + this.id = id; + this.name = name; + this.price = price; + this.address = address; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/entity/Stuff.java b/flower-shop/app/src/main/java/com/example/flowershop/entity/Stuff.java new file mode 100644 index 0000000..7f04614 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/entity/Stuff.java @@ -0,0 +1,117 @@ +package com.example.flowershop.entity; + +import com.example.flowershop.R; + +public class Stuff { + private String id;//id + private String name;//名称 + private String title;//标题 + private String kind;//分类 + private String price;//价格 + private int pic;//图片 + + public Stuff() { + this.id="-1"; + } + + public Stuff( String name, String title, String kind, String price ) { + this.id = "id"; + this.name = name; + this.title = title; + this.kind = kind; + this.price = price; + this.pic = 0; + } + + + public Stuff(String id, String name, String title, String kind, String price) { + this.id = id; + this.name = name; + this.title = title; + this.kind = kind; + this.price = price; + switch (name){ + case "白玫瑰": + this.pic= R.drawable.stuff_bmg; + break; + case "粉玫瑰": + this.pic=R.drawable.stuff_fmg; + break; + case "康乃馨": + this.pic=R.drawable.stuff_knx; + break; + case "蓝玫瑰": + this.pic=R.drawable.stuff_lmg; + break; + case "向日葵": + this.pic=R.drawable.stuff_xrk; + break; + case "郁金香": + this.pic=R.drawable.stuff_yjx; + break; + default: + this.pic=R.drawable.ic_about; + break; + } + } + + @Override + public String toString() { + return "Stuff{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", title='" + title + '\'' + + ", kind='" + kind + '\'' + + ", price='" + price + '\'' + + ", pic=" + pic + + '}'; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getKind() { + return kind; + } + + public void setKind(String kind) { + this.kind = kind; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public int getPic() { + return pic; + } + + public void setPic(int pic) { + this.pic = pic; + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/entity/User.java b/flower-shop/app/src/main/java/com/example/flowershop/entity/User.java new file mode 100644 index 0000000..d1d4534 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/entity/User.java @@ -0,0 +1,40 @@ +package com.example.flowershop.entity; + +public class User { + private String name; + private String psw; + private String address; + + public User() { + } + + public User(String name, String psw, String address) { + this.name = name; + this.psw = psw; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPsw() { + return psw; + } + + public void setPsw(String psw) { + this.psw = psw; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/fragment/CartFragment.java b/flower-shop/app/src/main/java/com/example/flowershop/fragment/CartFragment.java new file mode 100644 index 0000000..248fd20 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/fragment/CartFragment.java @@ -0,0 +1,118 @@ +package com.example.flowershop.fragment; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.fragment.app.Fragment; + +import com.example.flowershop.R; +import com.example.flowershop.adapter.CartAdapter; +import com.example.flowershop.entity.Record; +import com.example.flowershop.entity.Stuff; +import com.example.flowershop.entity.User; +import com.example.flowershop.sqlite.DBCart; +import com.example.flowershop.sqlite.DBRecord; +import com.example.flowershop.sqlite.DBStuff; +import com.example.flowershop.utils.CurrentUserUtils; + +import java.util.ArrayList; + + +public class CartFragment extends Fragment { + + public static ArrayList record = new ArrayList<>(); + public static TextView _tvTotal; + private ListView lvCart; + private Button btnBuy; + private TextView etEmpty; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_cart, container, false); + bindView(v); + initView(); + return v; + } + + private void bindView(View v) { + lvCart = v.findViewById(R.id.lv_cart); + etEmpty = v.findViewById(R.id.tv_empty); + _tvTotal = v.findViewById(R.id.tv_total); + btnBuy = v.findViewById(R.id.btn_buy); + } + + private void initView() { + _tvTotal.setText("0.00"); + //点击购买 + btnBuy.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (CartFragment.record.size() == 0) { + Toast.makeText(getContext(), "你还未选择任何商品", Toast.LENGTH_SHORT).show(); + return; + } + double total = 0; + for (Record r : CartFragment.record) { + //添加购买记录到数据库 + if (DBRecord.add(r)) { + total += Double.valueOf(r.getPrice()); + + Log.d("成功添加购买记录-----", r.toString()); + //从购物车数据库中删除已经购买的商品 + if (DBCart.del(r.getId(), r.getUsername())) { + Log.d("成功从购物车中删除-----", r.toString()); + } + } else { + Log.d("失败添加购买记录-----", r.toString()); + } + } + record = new ArrayList<>(); + _tvTotal.setText("0.00"); + Toast.makeText(getContext(), "价格总计" + String.format("%.2f", total) + "元,购买成功!", Toast.LENGTH_SHORT).show(); + set(); + + } + }); + set(); + } + + /** + * 从数据库中读取加入购物车的商品 + */ + private void set() { + User user = CurrentUserUtils.getCurrentUser(); + ArrayList array = new ArrayList<>(); + //获取购物车中商品的所有id + ArrayList allId = DBCart.getLikesTitle(user.getName()); + if (allId.size() == 0) { + etEmpty.setVisibility(View.VISIBLE); + } else { + etEmpty.setVisibility(View.GONE); + } + //通过id依次获取商品信息 + for (String s : allId) { + Stuff stuff = DBStuff.getById(Integer.parseInt(s)); + array.add(stuff); + } + for (Stuff t : array) { + Log.d("--------", t.toString()); + } + CartAdapter adapter = new CartAdapter(getContext(), R.layout.item_cart, array); + lvCart.setAdapter(adapter); + lvCart.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int i, long l) { + String s = (array.get(i)).getName(); + Toast.makeText(getContext(), "name:" + s, Toast.LENGTH_SHORT).show(); + } + }); + } +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/fragment/MainFragment.java b/flower-shop/app/src/main/java/com/example/flowershop/fragment/MainFragment.java new file mode 100644 index 0000000..c0cf899 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/fragment/MainFragment.java @@ -0,0 +1,94 @@ +package com.example.flowershop.fragment; + +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.Toast; + +import androidx.fragment.app.Fragment; + +import com.example.flowershop.R; +import com.example.flowershop.activity.DetailActivity; +import com.example.flowershop.adapter.StuffAdapter; +import com.example.flowershop.entity.Stuff; +import com.example.flowershop.sqlite.DBStuff; + +import java.util.ArrayList; +import java.util.List; + + +public class MainFragment extends Fragment { + + private ListView lvStuff; + private EditText etSearch; + private ImageView ivSearch; + + private List stuffList; + + private StuffAdapter adapter; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_main, container, false); + bindView(v); + initView(); + ivSearch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String str = etSearch.getText().toString(); + setSearch(str); + } + }); + return v; + } + + private void bindView(View v) { + lvStuff = v.findViewById(R.id.lv_stuff); + etSearch = v.findViewById(R.id.et_search); + ivSearch = v.findViewById(R.id.iv_search); + } + + private void initView() { + stuffList = DBStuff.getAll(); + adapter = new StuffAdapter(getContext(), R.layout.item_stuff, stuffList); + lvStuff.setAdapter(adapter); + lvStuff.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int i, long l) { + Intent intent = new Intent(); + intent.putExtra("id", (stuffList.get(i)).getId()); + intent.setClass(getContext(), DetailActivity.class); + startActivity(intent); + } + }); + } + + private void setSearch(String str) { + List arrayTemp = DBStuff.getAll(); + List array = new ArrayList<>(); + for (Stuff t : arrayTemp) { + if (t.getName().contains(str) || t.getTitle().contains(str)) { + array.add(t); + } + } + if (array.size() == 0) { + Toast.makeText(getContext(), "未搜索到关键字商品", Toast.LENGTH_SHORT).show(); + array = arrayTemp; + } + stuffList = array; + adapter.setData(stuffList); + } + +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/fragment/MeFragment.java b/flower-shop/app/src/main/java/com/example/flowershop/fragment/MeFragment.java new file mode 100644 index 0000000..ad3256c --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/fragment/MeFragment.java @@ -0,0 +1,120 @@ +package com.example.flowershop.fragment; + +import android.app.AlertDialog; +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.fragment.app.Fragment; + +import com.example.flowershop.R; +import com.example.flowershop.activity.AboutActivity; +import com.example.flowershop.activity.InfoActivity; +import com.example.flowershop.activity.RecordActivity; +import com.example.flowershop.entity.User; +import com.example.flowershop.sqlite.DBUser; +import com.example.flowershop.utils.CurrentUserUtils; + +public class MeFragment extends Fragment { + + private TextView tvName; + private LinearLayout llRecord, llChangeAddress, llInfo, llAbout, llContact; + + private User user; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + user = CurrentUserUtils.getCurrentUser(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_me, container, false); + bindView(v); + initView(); + return v; + } + + private void bindView(View v) { + llRecord = v.findViewById(R.id.myrecord); + tvName = v.findViewById(R.id.tv_name); + llChangeAddress = v.findViewById(R.id.ll_change_address); + llInfo = v.findViewById(R.id.ll_info); + llAbout = v.findViewById(R.id.ll_about); + llContact = v.findViewById(R.id.ll_contact); + } + + private void initView() { + tvName.setText("欢迎您,亲爱的" + user.getName()); + llAbout.setOnClickListener(this::onClick); + llChangeAddress.setOnClickListener(this::onClick); + llRecord.setOnClickListener(this::onClick); + llContact.setOnClickListener(this::onClick); + llInfo.setOnClickListener(this::onClick); + } + + void onClick(View v) { + int id = v.getId(); + if (id == R.id.myrecord) {//Toast.makeText(getContext(), "record", Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(getActivity(), RecordActivity.class); + startActivity(intent); + } else if (id == R.id.ll_contact) { + joinPhone(); + } else if (id == R.id.ll_info) { + Intent intent1 = new Intent(getActivity(), InfoActivity.class); + startActivity(intent1); + } else if (id == R.id.ll_change_address) { + changeAddress(); + } else if (id == R.id.ll_about) { + Intent intent3 = new Intent(getActivity(), AboutActivity.class); + startActivity(intent3); + } + } + + //修改收货地址 + void changeAddress() { + View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_change_address, null); + AlertDialog.Builder customBuilder = new AlertDialog.Builder(getContext()).setTitle("修改收货地址").setView(view); + AlertDialog dialog = customBuilder.create(); + dialog.show(); + //自定义按钮并注册监听器 + EditText ed_number = view.findViewById(R.id.et_address); + + ed_number.setHint(user.getAddress()); + Button btn_ok = view.findViewById(R.id.btn_confirm); + Button btn_cancel = view.findViewById(R.id.btn_cancel); + btn_ok.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //Toast.makeText(getActivity(), ed_number.getText().toString(), Toast.LENGTH_LONG).show(); + user.setAddress(ed_number.getText().toString()); + if (DBUser.change(user)) { + CurrentUserUtils.setCurrentUser(user); + Toast.makeText(getContext(), "修改成功!", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(getContext(), "修改失败!", Toast.LENGTH_SHORT).show(); + } + dialog.dismiss(); + } + }); + btn_cancel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dialog.dismiss(); + } + }); + } + + void joinPhone() { + Toast.makeText(getContext(), "请联系:18888888888", Toast.LENGTH_SHORT).show(); + } + +} \ No newline at end of file diff --git a/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBCart.java b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBCart.java new file mode 100644 index 0000000..8742e30 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBCart.java @@ -0,0 +1,52 @@ +package com.example.flowershop.sqlite; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +import androidx.annotation.Nullable; + +import com.example.flowershop.utils.SqliteUtils; + +import java.util.ArrayList; + +public class DBCart { + + public static boolean add(String id,String username){ + ContentValues values=new ContentValues(); + values.put("id",id); + values.put("username",username); + long i= SqliteUtils.getInstance().getWritableDatabase().insert("cart",null,values); + if(i>0){ + Log.d("","插入成功"); + return true; + } + return false; + } + public static boolean del(String id,String username){ + + long i=SqliteUtils.getInstance().getWritableDatabase().delete("cart","id=? and username=?",new String[]{id,username}); + if(i>0){ + Log.d("","删除成功"); + return true; + } + return false; + } + public static ArrayList getLikesTitle(String username){ + ArrayList array=new ArrayList(); + + Cursor cursor=SqliteUtils.getInstance().getReadableDatabase().query("cart",null,null,null,null,null,null); + while(cursor.moveToNext()){ + @SuppressLint("Range") String id=cursor.getString( cursor.getColumnIndex("id")); + @SuppressLint("Range") String name=cursor.getString( cursor.getColumnIndex("username")); + if(name.equals(username)){ + array.add(id ); + } + } + return array; + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBRecord.java b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBRecord.java new file mode 100644 index 0000000..aef41a4 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBRecord.java @@ -0,0 +1,54 @@ +package com.example.flowershop.sqlite; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +import androidx.annotation.Nullable; + +import com.example.flowershop.entity.Record; +import com.example.flowershop.utils.SqliteUtils; + +import java.util.ArrayList; +import java.util.List; + +public class DBRecord { + + public static boolean add(Record s){ + ContentValues values=new ContentValues(); + values.put("username",s.getUsername()); + values.put("id",s.getId()); + values.put("name",s.getName()); + values.put("price",s.getPrice()); + values.put("address",s.getAddress()); + long i= SqliteUtils.getInstance().getWritableDatabase().insert("record",null,values); + if(i>0){ + Log.d("","插入成功"); + return true; + } + Log.d("","插入失败"); + return false; + } + + public static List getAll(String user){ + List array=new ArrayList<>(); + Cursor cursor=SqliteUtils.getInstance().getReadableDatabase().query("record",null,null,null,null,null,null); + while(cursor.moveToNext()){ + @SuppressLint("Range") String username=cursor.getString( cursor.getColumnIndex("username")); + @SuppressLint("Range") String id=cursor.getString( cursor.getColumnIndex("id")); + @SuppressLint("Range") String name=cursor.getString( cursor.getColumnIndex("name")); + @SuppressLint("Range") String address=cursor.getString( cursor.getColumnIndex("address")); + @SuppressLint("Range") String price=cursor.getString( cursor.getColumnIndex("price")); + if(user.equals(username)){ + Record u=new Record(username,id,name ,price,address); + array.add(u); + } + } + return array; + } +} + diff --git a/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBStuff.java b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBStuff.java new file mode 100644 index 0000000..a18b463 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBStuff.java @@ -0,0 +1,75 @@ +package com.example.flowershop.sqlite; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.database.Cursor; +import android.util.Log; + +import com.example.flowershop.entity.Stuff; +import com.example.flowershop.utils.SqliteUtils; + +import java.util.ArrayList; +import java.util.List; + +public class DBStuff { + + public static boolean add(Stuff s) { + ContentValues values = new ContentValues(); + Log.d("", "插入 "); + values.put("name", s.getName()); + values.put("title", s.getTitle()); + values.put("kind", s.getKind()); + values.put("price", s.getPrice()); + long i = SqliteUtils.getInstance().getWritableDatabase().insert("stuff", null, values); + if (i > 0) { + Log.d("", "插入成功"); + return true; + } + Log.d("", "插入失败"); + return false; + } + + public static void deleteAll() { + List all = getAll(); + for (int i = 0; i < all.size(); i++) { + String sqlStr = "id" + "=?"; + String[] contentValuesArray = new String[]{all.get(i).getId()}; + SqliteUtils.getInstance().getWritableDatabase().delete("stuff", sqlStr, contentValuesArray); + } + } + + public static List getAll() { + List array = new ArrayList<>(); + Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("stuff", null, null, null, null, null, null); + while (cursor.moveToNext()) { + @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id")); + @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name")); + @SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex("title")); + @SuppressLint("Range") String kind = cursor.getString(cursor.getColumnIndex("kind")); + @SuppressLint("Range") String price = cursor.getString(cursor.getColumnIndex("price")); + + Stuff u = new Stuff(String.valueOf(id), name, title, kind, price); + array.add(u); + + } + return array; + } + + public static Stuff getById(int _id) { + Stuff s = new Stuff(); + Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("stuff", null, null, null, null, null, null); + while (cursor.moveToNext()) { + @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id")); + @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name")); + @SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex("title")); + @SuppressLint("Range") String kind = cursor.getString(cursor.getColumnIndex("kind")); + @SuppressLint("Range") String price = cursor.getString(cursor.getColumnIndex("price")); + if (id == _id) { + s = new Stuff(String.valueOf(id), name, title, kind, price); + break; + } + } + return s; + } +} + diff --git a/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBUser.java b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBUser.java new file mode 100644 index 0000000..0219a47 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/sqlite/DBUser.java @@ -0,0 +1,64 @@ +package com.example.flowershop.sqlite; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.database.Cursor; +import android.text.TextUtils; +import android.util.Log; + +import com.example.flowershop.entity.User; +import com.example.flowershop.utils.SqliteUtils; + +public class DBUser { + + public static boolean add(User u) { + ContentValues values = new ContentValues(); + values.put("name", u.getName()); + values.put("psw", u.getPsw()); + values.put("address", u.getAddress()); + long i = SqliteUtils.getInstance().getWritableDatabase().insert("user", null, values); + if (i > 0) { + Log.d("", "插入成功"); + return true; + } + return false; + } + + public static boolean change(User u) { + ContentValues values = new ContentValues(); + values.put("address", u.getAddress()); + long i = SqliteUtils.getInstance().getWritableDatabase().update("user", values, "name=?", new String[]{u.getName()}); + if (i > 0) { + Log.d("", "修改成功"); + return true; + } + return false; + } + + public static boolean check(String n, String p) { + Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("user", null, null, null, null, null, null); + while (cursor.moveToNext()) { + @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name")); + @SuppressLint("Range") String psw = cursor.getString(cursor.getColumnIndex("psw")); + if (n.equals(name) && p.equals(psw)) { + return true; + } + } + return false; + } + + public static User get(String n) { + User u = new User(); + Cursor cursor = SqliteUtils.getInstance().getReadableDatabase().query("user", null, null, null, null, null, null); + while (cursor.moveToNext()) { + @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name")); + @SuppressLint("Range") String psw = cursor.getString(cursor.getColumnIndex("psw")); + @SuppressLint("Range") String address = cursor.getString(cursor.getColumnIndex("address")); + if (n.equals(name)) { + u = new User(name, psw, address); + break; + } + } + return u; + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/utils/AppUtils.java b/flower-shop/app/src/main/java/com/example/flowershop/utils/AppUtils.java new file mode 100644 index 0000000..e781e06 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/utils/AppUtils.java @@ -0,0 +1,58 @@ +package com.example.flowershop.utils; + +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; + +public class AppUtils { + + private static final String PREF_NAME = "app_pref"; + private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch"; + + private final SharedPreferences pref; + private final SharedPreferences.Editor editor; + + private Application application; + + public AppUtils() { + try { + Class activityThreadClass = Class.forName("android.app.ActivityThread"); + Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null); + application = (Application) activityThreadClass.getMethod("getApplication").invoke(activityThread); + } catch (Exception e) { + e.printStackTrace(); + } + pref = application.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + editor = pref.edit(); + } + + /** + * 创建并获取单例 + */ + private static AppUtils getInstance() { + return InstanceHolder.instance; + } + + public static boolean isFirstTimeLaunch() { + return getInstance().pref.getBoolean(IS_FIRST_TIME_LAUNCH, true); + } + + public static void setFirstTimeLaunch(boolean isFirstTime) { + getInstance().editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime); + getInstance().editor.commit(); + } + + /** + * 反射获取Application + */ + public static Application getApplication() { + return getInstance().application; + } + + private static final class InstanceHolder { + /** + * 单例 + */ + static final AppUtils instance = new AppUtils(); + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/utils/CodeUtils.java b/flower-shop/app/src/main/java/com/example/flowershop/utils/CodeUtils.java new file mode 100644 index 0000000..8509272 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/utils/CodeUtils.java @@ -0,0 +1,16 @@ +package com.example.flowershop.utils; + +import java.util.Random; + +public class CodeUtils { + public static String getCode(){ + String str="abcdefghijklmnopqrstuvwxyz0123456789"; + Random random=new Random(); + StringBuffer sb=new StringBuffer(); + for(int i=0;i<4;i++){ + int number=random.nextInt(36); + sb.append(str.charAt(number)); + } + return sb.toString(); + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/utils/CurrentUserUtils.java b/flower-shop/app/src/main/java/com/example/flowershop/utils/CurrentUserUtils.java new file mode 100644 index 0000000..03c7ec0 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/utils/CurrentUserUtils.java @@ -0,0 +1,49 @@ +package com.example.flowershop.utils; + +import android.content.Context; +import android.content.SharedPreferences; + +import com.example.flowershop.entity.User; + +public class CurrentUserUtils { + + private static final String CURRENT_USER = "CURRENT_USER"; + + private final SharedPreferences sp; + /** + * 创建并获取单例 + */ + public static CurrentUserUtils getInstance() { + return CurrentUserUtils.InstanceHolder.instance; + } + + public CurrentUserUtils() { + sp = AppUtils.getApplication().getSharedPreferences(CURRENT_USER, Context.MODE_PRIVATE); + } + + public static User getCurrentUser() { + String username = getInstance().sp.getString("username", null); + String address = getInstance().sp.getString("address", null); + if (username == null) { + return null; + } + User user = new User(); + user.setName(username); + user.setAddress(address); + return user; + } + + public static void setCurrentUser(User user) { + SharedPreferences.Editor editor = getInstance().sp.edit(); + editor.putString("username", user.getName()); + editor.putString("address", user.getAddress()); + editor.apply(); + } + + private static final class InstanceHolder { + /** + * 单例 + */ + static final CurrentUserUtils instance = new CurrentUserUtils(); + } +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/utils/MyGlideModule.java b/flower-shop/app/src/main/java/com/example/flowershop/utils/MyGlideModule.java new file mode 100644 index 0000000..df8d3a6 --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/utils/MyGlideModule.java @@ -0,0 +1,24 @@ +package com.example.flowershop.utils; + +import android.content.Context; + +import androidx.annotation.NonNull; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.Registry; +import com.bumptech.glide.annotation.GlideModule; +import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader; +import com.bumptech.glide.load.model.GlideUrl; +import com.bumptech.glide.module.AppGlideModule; + +import java.io.InputStream; + +@GlideModule +public class MyGlideModule extends AppGlideModule { + + @Override + public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { + registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory()); + } + +} diff --git a/flower-shop/app/src/main/java/com/example/flowershop/utils/SqliteUtils.java b/flower-shop/app/src/main/java/com/example/flowershop/utils/SqliteUtils.java new file mode 100644 index 0000000..8950c7e --- /dev/null +++ b/flower-shop/app/src/main/java/com/example/flowershop/utils/SqliteUtils.java @@ -0,0 +1,46 @@ +package com.example.flowershop.utils; + +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class SqliteUtils extends SQLiteOpenHelper { + + public SqliteUtils() { + super(AppUtils.getApplication(), "flower_shop.db", null, 1); + } + + + /** + * 创建并获取单例 + */ + public static SqliteUtils getInstance() { + return InstanceHolder.instance; + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("create table user(name text primary key,psw text not null,address text not null)"); + db.execSQL("create table stuff(id INTEGER primary key AUTOINCREMENT,name text not null,title text not null,kind text not null,price text not null)"); + db.execSQL("create table record(username text not null,id text not null,name text not null,price text not null,address text not null)"); + db.execSQL("create table cart(id text not null,username text not null,PRIMARY KEY(id,username))"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int i, int i1) { + //删除表 + db.execSQL("DROP TABLE IF EXISTS user"); + db.execSQL("DROP TABLE IF EXISTS stuff"); + db.execSQL("DROP TABLE IF EXISTS record"); + db.execSQL("DROP TABLE IF EXISTS cart"); + //重新创建表 + onCreate(db); + } + + private static final class InstanceHolder { + /** + * 单例 + */ + static final SqliteUtils instance = new SqliteUtils(); + } + +} diff --git a/flower-shop/app/src/main/res/drawable-v1/bg_common.jpg b/flower-shop/app/src/main/res/drawable-v1/bg_common.jpg new file mode 100644 index 0000000..68526e7 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/bg_common.jpg differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_about.png b/flower-shop/app/src/main/res/drawable-v1/ic_about.png new file mode 100644 index 0000000..7cca405 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_about.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_back.png b/flower-shop/app/src/main/res/drawable-v1/ic_back.png new file mode 100644 index 0000000..8414b33 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_back.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_cart.png b/flower-shop/app/src/main/res/drawable-v1/ic_cart.png new file mode 100644 index 0000000..a5fd50f Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_cart.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_change.png b/flower-shop/app/src/main/res/drawable-v1/ic_change.png new file mode 100644 index 0000000..5816597 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_change.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_contact.png b/flower-shop/app/src/main/res/drawable-v1/ic_contact.png new file mode 100644 index 0000000..2071db1 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_contact.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_home.png b/flower-shop/app/src/main/res/drawable-v1/ic_home.png new file mode 100644 index 0000000..c561ad3 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_home.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_me.png b/flower-shop/app/src/main/res/drawable-v1/ic_me.png new file mode 100644 index 0000000..45d9236 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_me.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_order.png b/flower-shop/app/src/main/res/drawable-v1/ic_order.png new file mode 100644 index 0000000..2b345bc Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_order.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_right.png b/flower-shop/app/src/main/res/drawable-v1/ic_right.png new file mode 100644 index 0000000..fcb095e Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_right.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/ic_search.png b/flower-shop/app/src/main/res/drawable-v1/ic_search.png new file mode 100644 index 0000000..e2535ad Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/ic_search.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/icon.png b/flower-shop/app/src/main/res/drawable-v1/icon.png new file mode 100644 index 0000000..1ada268 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/icon.png differ diff --git a/flower-shop/app/src/main/res/drawable-v1/stuff_bmg.jpg b/flower-shop/app/src/main/res/drawable-v1/stuff_bmg.jpg new file mode 100644 index 0000000..18bfc18 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/stuff_bmg.jpg differ diff --git a/flower-shop/app/src/main/res/drawable-v1/stuff_fmg.jpg b/flower-shop/app/src/main/res/drawable-v1/stuff_fmg.jpg new file mode 100644 index 0000000..061e5bb Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/stuff_fmg.jpg differ diff --git a/flower-shop/app/src/main/res/drawable-v1/stuff_knx.jpg b/flower-shop/app/src/main/res/drawable-v1/stuff_knx.jpg new file mode 100644 index 0000000..62dcb79 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/stuff_knx.jpg differ diff --git a/flower-shop/app/src/main/res/drawable-v1/stuff_lmg.jpg b/flower-shop/app/src/main/res/drawable-v1/stuff_lmg.jpg new file mode 100644 index 0000000..a4a37d6 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/stuff_lmg.jpg differ diff --git a/flower-shop/app/src/main/res/drawable-v1/stuff_xrk.jpg b/flower-shop/app/src/main/res/drawable-v1/stuff_xrk.jpg new file mode 100644 index 0000000..64336c3 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/stuff_xrk.jpg differ diff --git a/flower-shop/app/src/main/res/drawable-v1/stuff_yjx.jpg b/flower-shop/app/src/main/res/drawable-v1/stuff_yjx.jpg new file mode 100644 index 0000000..7ae6e70 Binary files /dev/null and b/flower-shop/app/src/main/res/drawable-v1/stuff_yjx.jpg differ diff --git a/flower-shop/app/src/main/res/drawable/bg_border.xml b/flower-shop/app/src/main/res/drawable/bg_border.xml new file mode 100644 index 0000000..73cc684 --- /dev/null +++ b/flower-shop/app/src/main/res/drawable/bg_border.xml @@ -0,0 +1,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/drawable/bg_btn_common.xml b/flower-shop/app/src/main/res/drawable/bg_btn_common.xml new file mode 100644 index 0000000..b67264c --- /dev/null +++ b/flower-shop/app/src/main/res/drawable/bg_btn_common.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/drawable/bg_et_border.xml b/flower-shop/app/src/main/res/drawable/bg_et_border.xml new file mode 100644 index 0000000..000d194 --- /dev/null +++ b/flower-shop/app/src/main/res/drawable/bg_et_border.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_about.xml b/flower-shop/app/src/main/res/layout/activity_about.xml new file mode 100644 index 0000000..a422aef --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_about.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_detail.xml b/flower-shop/app/src/main/res/layout/activity_detail.xml new file mode 100644 index 0000000..9a5f7d5 --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_detail.xml @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_info.xml b/flower-shop/app/src/main/res/layout/activity_info.xml new file mode 100644 index 0000000..29535f8 --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_info.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_login.xml b/flower-shop/app/src/main/res/layout/activity_login.xml new file mode 100644 index 0000000..bcba9aa --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_login.xml @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_main.xml b/flower-shop/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..c84ba46 --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_record.xml b/flower-shop/app/src/main/res/layout/activity_record.xml new file mode 100644 index 0000000..2b41a9a --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_record.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/activity_register.xml b/flower-shop/app/src/main/res/layout/activity_register.xml new file mode 100644 index 0000000..7901202 --- /dev/null +++ b/flower-shop/app/src/main/res/layout/activity_register.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flower-shop/app/src/main/res/layout/dialog_change_address.xml b/flower-shop/app/src/main/res/layout/dialog_change_address.xml new file mode 100644 index 0000000..ca624a4 --- /dev/null +++ b/flower-shop/app/src/main/res/layout/dialog_change_address.xml @@ -0,0 +1,47 @@ + + + + + + + + + +