1 Star 0 Fork 0

Lunter-zst / Java201621123023

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Main.java 5.17 KB
一键复制 编辑 原始数据 按行查看 历史
Lunter-zst 提交于 2017-10-30 17:42 . 接口实现
package ShoppingCart;
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.print("请输入账号和密码:");
User[] use =new User[3];
use[0] = new User("a","a");
use[1] = new User("b","b");
use[2] = new User("c","c");
User user = new User(in.next(),in.next());
int b=0;
for (int i = 0; i < use.length; i++)
{
if(user.getUsername().equals(use[i].getUsername())&&user.getPassword().equals(use[i].getPassword()))
{
Goods[] good=new Goods[3];
good[0] = new Goods(0,"aaa",1.1);
good[1] = new Goods(1,"bbb",2.2);
good[2] = new Goods(2,"ccc",3.3);
System.out.println("商城商品如下:");
for (Goods e : good)
{
System.out.printf("编号:%d 商品名:%s 价格:%.1f",e.getNumber(),e.getName(),e.getPrice());
System.out.println();
}
int a=1;
while(true)
{
System.out.print("1.购买 2.删除 3.结算");
System.out.println();
System.out.print("请输入选择:");
String aaa = in.next();
switch (aaa)
{
case "1":
System.out.print("如需购买请输入商品编号及购买数量:");
int aa = in.nextInt();
good[aa].setSum(in.nextInt());
user.shopping.addGoods(good[aa]);
break;
case "2":
System.out.print("如需删除请输入商品编号:");
int bb = in.nextInt();
user.shopping.deleteGoods(good[bb]);
break;
case "3":
a=2;
break;
default:
break;
}
if (a==2)
break;
}
user.shopping.showCart();
user.shopping.showTotal();
}
else
b++;
}
if (b==use.length)
System.out.println("账号或密码错误!");
}
}
class User
{
private String username;
private String password;
//ShoppingCart shopping;
ShoppingCartArray shopping;
//其他信息
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public User(String username, String password)
{
super();
this.username = username;
this.password = password;
//this.shopping = new ShoppingCart();
this.shopping = new ShoppingCartArray();
}
}
class Goods
{
private String name;
private double price;
private int number;
private int sum;
public Goods(int number, String name, double price)
{
super();
this.number = number;
this.name = name;
this.price = price;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
}
interface ShoppingCartDao
{
public void addGoods(Goods goods);
public void deleteGoods(Goods goods);
public void showTotal();
public void showCart();
}
class ShoppingCartArray implements ShoppingCartDao
{
Goods[] array = new Goods[100];
int b=0;
public void addGoods(Goods goods)
{
int a=0;
for (int i = 0; i < array.length; i++)
{
if(goods.equals(array[i]))
a=1;
}
if(a==0)
array[b++]=goods;
else
System.out.println("商品已在购物车中!");
}
public void deleteGoods(Goods goods)
{
int a=0;
for (int i = 0; i < array.length; i++)
{
if(goods.equals(array[i]))
a=i;
}
if(a!=0)
{
for(int j=a;j<array.length-1;j++)
array[j]=array[j+1];
b--;
}
else
System.out.println("未选购此商品!");
}
public void showTotal()
{
double a=0;
for (int i = 0; i < b; i++)
{
a=a+array[i].getPrice()*array[i].getSum();
}
System.out.print("总价:");
System.out.printf("%.1f",a);
}
public void showCart()
{
if(array.length==0)
System.out.println("未添加商品!");
else
{
System.out.println("购物车内商品如下:");
for (int i = 0; i < b; i++)
{
System.out.printf("商品名:%s 价格:%.1f 购买数:%d",array[i].getName(),array[i].getPrice(),array[i].getSum());
System.out.println();
}
}
}
}
//class ShoppingCart implements ShoppingCartDao
//{
// ArrayList<Goods> goodslist = new ArrayList<Goods>();
// public void addGoods(Goods goods)
// {
// if(!goodslist.contains(goods))
// goodslist.add(goods);
// else
// System.out.println("商品已在购物车中!");
// }
// public void deleteGoods(Goods goods)
// {
// if(goodslist.contains(goods))
// goodslist.remove(goods);
// else
// System.out.println("未选购此商品!");
// }
// public void showTotal()
// {
// double a=0;
// for (Goods e : goodslist)
// {
// a=a+e.getPrice()*e.getSum();
// }
// System.out.print("总价:");
// System.out.printf("%.1f",a);
// }
// public void showCart()
// {
// if(goodslist.size()==0)
// System.out.println("未添加商品!");
// else
// {
// System.out.println("购物车内商品如下:");
// for (Goods e : goodslist)
// {
// System.out.printf("商品名:%s 价格:%.1f 购买数:%d",e.getName(),e.getPrice(),e.getSum());
// System.out.println();
// }
// }
// }
//}
1
https://gitee.com/Lunter-zst/Java201621123023.git
git@gitee.com:Lunter-zst/Java201621123023.git
Lunter-zst
Java201621123023
Java201621123023
master

搜索帮助