Java初学者,利用简单集合知识写了一个小项目(当然四处借鉴了许多);非常基础,没有涉及io以及其他复杂知识
定义类不要钱,所以怼了一堆;
1.测试类;主要是主页面的循环
package shuiguotan;
import java.util.Scanner;
public class LastTest {
public static void main(String[] args) {
System.out.println("欢迎来到华强水果摊");
while(true) {
jiemian.jiemianxianshi();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
switch(n) {
case 1:
shuiguochushihua.chushi();
System.out.println("货物不全,请您谅解");
System.out.println("------------------------------------------------------------------------------------------");
break;
case 2:
BuyFruit bf=new BuyFruit();
bf.buyFruit();
System.out.println("购买成功");
System.out.println("------------------------------------------------------------------------------------------");
break;
case 3:
System.out.println("您已购买如下商品:");
gouwuche.print();
System.out.println("------------------------------------------------------------------------------------------");
break;
case 4:
BuyFruit fq = new BuyFruit();
fq.pay();
System.out.println("------------------------------------------------------------------------------------------");
break;
case 5:
System.out.println("谢谢惠顾!欢迎下次光临");
return;
default:
System.out.println("输入指令错误,请输入正确指令");
break;
}
}
}
}
2.Fruit类,简单的属性和get set方法
package shuiguotan;
public class Fruit {
String name;
double price;
public Fruit() {
super();
}
public Fruit(String name, double price) {
super();
this.name = name;
this.price = price;
}
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;
}
}
3.BuyFruit类,涉及购买水果的细节以及付钱的一个方法
package shuiguotan;
import java.util.Scanner;
public class BuyFruit {
public static double money=0;
public void buyFruit() {
boolean is=true;
shuiguochushihua.chushi();
while(is) {
System.out.println("请选择您想购买的水果并输入对应序号");
Scanner sca=new Scanner(System.in);
int fn1=sca.nextInt();
System.out.println("请输入购买数量");
double fn2=sca.nextDouble();
switch (fn1) {
case 01:
Fruit f1 = new Fruit();
f1.setPrice(7.0);
money = money + f1.getPrice() * fn2;
gouwuche.alist.add(new FruitList("苹果", fn2));
break;
case 02:
Fruit f2 = new Fruit();
f2.setPrice(6.6);
money = money + f2.getPrice() * fn2;
gouwuche.alist.add(new FruitList("西瓜", fn2));
break;
case 03:
Fruit f3 = new Fruit();
f3.setPrice(7.8);
money = money + f3.getPrice() * fn2;
gouwuche.alist.add(new FruitList("芒果", fn2));
break;
case 04:
Fruit f4 = new Fruit();
f4.setPrice(6.7);
money = money + f4.getPrice() * fn2;
gouwuche.alist.add(new FruitList("菠萝", fn2));
break;
default:
System.out.println("错误信息:无法识别");
System.out.println("提示信息:请正确输入水果编号");
System.out.println("------------------------------------------------");
jiemian.jiemianxianshi();
break;
}
System.out.println("如果想要继续购买请输入0,退出按任意数字即可");
int x = sca.nextInt();
if (x == 0) {
} else {
is = false;
}
}
}
public void pay() {
Scanner scan=new Scanner(System.in);
boolean flag=true;
while(flag) {
System.out.println("总金额为:" + money + "元");
System.out.println("请付款");
double qian=scan.nextDouble();
if(qian<money) {
System.out.println("你故意找茬是不是?");
}else if(qian>money) {
System.out.println("付款成功,找零"+String.format("%.2f", (qian-money)));
flag=false;
}else {
System.out.println("付款成功");
flag=false;
}
}
}
public static void isPay() {
if (money == 0) {
System.out.println("您还未购买任何商品");
}
}
}
4.FruitList类,水果清单类以及重写了toString
package shuiguotan;
public class FruitList {
String Fruitname;
double number;
public FruitList() {
super();
}
public FruitList(String fruitname, double number) {
super();
Fruitname = fruitname;
this.number = number;
}
public String getFruitname() {
return Fruitname;
}
public void setFruitname(String fruitname) {
Fruitname = fruitname;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
@Override
public String toString() {
return "水果名称:" + Fruitname + ", 数量:" + number +"斤";
}
}
5.gouwuche类,存储购买商品
package shuiguotan;
import java.util.ArrayList;
import java.util.Iterator;
public class gouwuche {
static ArrayList<FruitList> alist= new ArrayList<FruitList>();
public static void print() {
Iterator<FruitList> iterator = alist.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
6.界面凑数
package shuiguotan;
public class jiemian {
public static void jiemianxianshi() {
System.out.println("请选择您要办理的业务");
System.out.println("1.查看水果列表 2.购买水果 3.查看购物车 4.结账 5.退出");
}
}
7.初始化类,添加水果的名字单价
package shuiguotan;
import java.util.ArrayList;
import java.util.List;
public class shuiguochushihua {
public static void chushi() {
List <Fruit> list = new ArrayList<>();
Fruit f1=new Fruit("苹果",7.0);
list.add(f1);
Fruit f2=new Fruit("西瓜",6.6);
list.add(f2);
Fruit f3=new Fruit("芒果",7.8);
list.add(f3);
Fruit f4=new Fruit("菠萝",6.7);
list.add(f4);
System.out.println("水果1:名称:"+f1.getName()+" 价格:"+f1.getPrice()+"元/斤");
System.out.println("水果2:名称:"+f2.getName()+" 价格:"+f2.getPrice()+"元/斤");
System.out.println("水果3:名称:"+f3.getName()+" 价格:"+f3.getPrice()+"元/斤");
System.out.println("水果4:名称:"+f4.getName()+" 价格:"+f4.getPrice()+"元/斤");
}
}
菜鸡勿喷